Lec 6 Logical Operators String comparison

Slides:



Advertisements
Similar presentations
05 Simple selection1June Simple selection CE : Fundamental Programming Techniques.
Advertisements

Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
1 Text Processing. 2 Type char char : A primitive type representing single characters. –A String is stored internally as an array of char String s = "Ali.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Lec 6 Logical Operators String comparison. Review – if statement syntax OR.
Flow of Control Part 1: Selection
Lec 4: while loop and do-while loop. Review from last week if Statements if (num < 0.5){...println("heads"); } if –else Statements if (num < 0.5){...println("heads");
5. Conditionals & Loops Based on Java Software Development, 5 th Ed. By Lewis &Loftus.
COMP Flow of Control: Branching 1 Yi Hong May 19, 2015.
CSE 1341 Honors Note Set 05 Professor Mark Fontenot Southern Methodist University.
Primitive variables Android Club types of variables: Primitive single value (starts with uppercase letter) Complex multiple value (starts with.
1 SELECTION using IF and IF-ELSE Chapter 4. 2 Agenda Background  One Way selection (if) Two Way selection (if-else) Compound Statements Nested if-else.
REVIEW No curveballs this time …. PROBLEM TYPE #1: EVALUATIONS.
COMP Primitive and Class Types Yi Hong May 14, 2015.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
COMP Loop Statements Yi Hong May 21, 2015.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Primitive Data Types. int This is the type you are familiar with and have been using Stores an integer value (whole number) between -2,147,483,648 (-2.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
1 float Data Type Data type that can hold numbers with decimal values – e.g. 3.14, 98.6 Floats can be used to represent many values: –Money (but see warning.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
CSE 110 Midterm Review Hans and Carmine. If Statements If statements use decision logic In order to properly use if statements relational operators must.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
CompSci 230 S Programming Techniques
Chapter 3 Selection Statements
Chapter 2 Basic Computation
Data Types and Expressions
Lecture 6: While Loops and the Math Class
INC 161 , CPE 100 Computer Programming
Line Continuation, Output Formatting, and Decision Structures
Programming Mehdi Bukhari.
Primitive Data, Variables, Loops (Maybe)
The Selection Structure
C# and the .NET Framework
Repetition-Sentinel,Flag Loop/Do_While
CMSC201 Computer Science I for Majors Lecture 03 – Operators
SELECTION STATEMENTS (1)
Topic 11 Scanner object, conditional execution
Control Statement Examples
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Loops October 10, 2017.
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Line Continuation, Output Formatting, and Decision Structures
Compound Assignment Operators in C++
SELECTION STATEMENTS (2)
Building Java Programs
Algorithm Discovery and Design
Lec 5 Nested Control Structures
Lec 4: while loop and do-while loop
If Statements.
SELECTIONS STATEMENTS
Building Java Programs
Building Java Programs
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Building Java Programs
Building Java Programs
Chapter 2 Programming Basics.
Boolean Expressions to Make Comparisons
SSEA Computer Science: Track A
Building Java Programs
CS2011 Introduction to Programming I Selections (I)
Building Java Programs
Repetition Statements
Java LESSON 3 Loops.
Building Java Programs
Building Java Programs
Presentation transcript:

Lec 6 Logical Operators String comparison

Review – if statement syntax OR

Logical expressions <boolean expression> is logical expression score == 100 x+y>10 ans!='y' (ans is a char) isValid (isValid is a boolean )

The 6 Comparison Operators

Sometimes we need more complicated logic example: age is between 18 and 55 months > 3 OR miles>5000 word is not "yes" (word is String)

Application: How to tell if n is in the correct range ...println("Enter a number from 1 to 10"); n = myInput.nextInt(); if (n>=1){ if (n<=10){ cout<<“OK, n is between 1 and 10!”; } else { cout<<“n is too big”; } } else{ cout<<“n is too small”;

Flowchart of previous slide

A better way using && ...println("Enter a number from 1 to 10"); n = myInput.nextInt(); if (n>=1 && n <=10){ cout<<“OK, n is between 1 and 10!”; } else{ cout<<“illegal value of n”;

The 3 Logical Operators logical operator meaning && and || or ! not example in English && and a < 3 && b > 10 a is less than 3 and b is greater than 10 || or a < 3 || b > 10 a is less than 3 or b is greater than 10 ! not !(a < 3) it is not the case that a is less than 3

Examples 1. T/F ( 3 < 2 ) && (5 > 7) 2. T/F ! ( 2 > 3 )   4. the expression: ( number > 10 && number < 40 )  is TRUE, when a)      number is larger than 40 b)      number is smaller than 10 c)      number is between 10 and 40 d) Never

Oil Change

In groups, flowchart previous slide

Primitives vs Objects Recall the primitives: int x = 3; double num = 3.42; char letter = 'Y'; boolean found = true; And objects (of Classes we've seen) Scanner myUserInput = new Scanner(System.in); String greeting = "hello";

Some differences between Primitives and Objects Objects have methods: greeting.trim(); // remove blanks before/after greeting.toUpperCase(); // convert to CAPS myUserInput.nextInt(); // get an integer Primitives just store values: int x = 3; double num = 3.42; char letter = 'Y'; boolean found = true;

Comparing Strings Normally, Strings (and objects in general) should not use the comparison operators to check values: WRONG: if ( greeting == "hello" ){ .... Use .equals( ) instead RIGHT: if (greeting.equals( "hello") { ... For strings, this is even better: if ( greeting.equalsIgnoreCase("hello"){ ...

True or false? String s1 = "hello", s2 = "HeLLo", s3 = " hello "; s1.equals("hello"); s2.equals("hello"); s3.equals("hello"); s2.equalsIgnoreCase("Hello"); s3.trim().equals("hello");

Lab 6 Department store Checkout.java Ask how many items to ring up must be 1-10 use do loop to verify correct numbers logical operators to check for too high or too low While loop repeats for as many items get item cost add to total Ask if a bag is wanted, $ 0.05 extra if only 1-3 items Add 8.25% sales tax ( total = total*1.0825)

How to avoid: total = $5.66666668 EITHER: truncate (chop) to two decimal places double d = 33.2482; int i = (int)(d*100); System.out.println(i / 100.0);   OR format (round) to two decimal places for output d = 33.2482; System.out.printf("%1.2f\n", d); // means print d with two digits after decimal // %1.2f is format, \n means go to new line after

Calculating the sum or average num k k < 10 Console int sum = 0; int k = 0, num; while ( k < 5) { num = scan.nextInt(); sum = sum + num; k = k + 1; } ...println("sum is " + sum); ...println("avg is " + sum/5.0);