Lec 6 Logical Operators String comparison. Review – if statement syntax OR.

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

Variables – the Key to Programming. ICS-3M1 - Mr. Martens - Variables Part 1 What is a Variable? A storage location that is given a “name” You can store.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
05 Simple selection1June Simple selection CE : Fundamental Programming Techniques.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Boolean expressions Conditional statements and expressions.
Java Data Types. Primitive Data Types Java has 8 primitive data types: – char: used to store a single character eg. G – boolean: used to store true or.
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?
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Java Software Solutions Lewis and Loftus Chapter 5 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. More Programming Constructs.
Chapter 5 Loops.
Flow of Control Part 1: Selection
A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
5. Conditionals & Loops Based on Java Software Development, 5 th Ed. By Lewis &Loftus.
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");
Logic Our programs will have to make decisions in terms of what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if.
5. Conditionals & Loops Based on Java Software Development, 5 th Ed. By Lewis &Loftus.
CSE 1341 Honors Note Set 05 Professor Mark Fontenot Southern Methodist University.
Building java programs, chapter 5 Program logic and indefinite loops.
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.
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
CSCI 1226 FALL 2015 MIDTERM #1 REVIEWS.  Types of computers:  Personal computers  Embedded systems  Servers  Hardware:  I/O devices: mice, keyboards,
Java Review if Online Time For loop Quiz on Thursday.
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
A: A: double “4” A: “34” 4.
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
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.
Java Fundamentals 4. Java Programming: From Problem Analysis to Program Design, Second Edition2 Parsing Numeric Strings  Integer, Float, and Double are.
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.
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
Chapter 2 Basic Computation
INC 161 , CPE 100 Computer Programming
String class.
Other Kinds of Arrays Chapter 11
Chapter 2 Basic Computation
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.
Compound Assignment Operators in C++
SELECTION STATEMENTS (2)
Building Java Programs
Lec 5 Nested Control Structures
Lec 4: while loop and do-while loop
SELECTIONS STATEMENTS
Building Java Programs
Building Java Programs
Chapter 2 Programming Basics.
Building Java Programs
Building Java Programs
The keyboard is the standard input device.
Building Java Programs
CS2011 Introduction to Programming I Selections (I)
Building Java Programs
Building Java Programs
Building Java Programs
Repetition Statements
Lec 6 Logical Operators String comparison
Unit 3: Variables in Java
Building Java Programs
Welcome back! October 11, 2018.
Data Types and Maths Programming Guides.
Building Java Programs
Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement.
Presentation transcript:

Lec 6 Logical Operators String comparison

Review – if statement syntax OR

3 Logical expressions 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)

6 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”; }

7 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 example in English &&and a 10 a is less than 3 and b is greater than 10 ||or a 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

9 Examples 1. T/F ( 3 7) 2. T/F ! ( 2 > 3 ) 3. T/F (25 = = 25 ) || ( 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

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 = $ EITHER: truncate (chop) to two decimal places double d = ; int i = (int)(d*100); System.out.println(i / 100.0); OR format (round) to two decimal places for output d = ; System.out.printf("%1.2f", d); System.out.println(); //need this since printf doesn't //produce a newline character

Calculating the sum or average 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); sumnumkk < 10 Console