Chapter 6 Conditionals.

Slides:



Advertisements
Similar presentations
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Advertisements

Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University.
Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 5.2)
true (any other value but zero) false (zero) expression Statement 2
If Statement if (amount
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
C++ for Engineers and Scientists Third Edition
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
Decision Structures and Boolean Logic
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 7 Decision Making : selection statements.
Chapter 4: Control Structures I
Chapter 8 Conditionals. Learning Java through Alice © Daly and Wrigley Objectives List relational operators. List logical operators. Use the hierarchy.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Flow of Control Part 1: Selection
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
MS3304: Week 6 Conditional statements. Overview The flow of control through a script Boolean Logic Conditional & logical operators Basic decision making.
Decision Making Selection structures (if....else and switch/case)
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
Control Structures 1. Control Structures Java Programming: From Problem Analysis to Program Design, D.S. Malik 2.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
1 2. Program Construction in Java. 2.4 Selection (decisions)
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
4.1 Object Operations operators Dot (. ) and new operate on objects The assignment operator ( = ) Arithmetic operators + - * / % Unary operators.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.
Decisions. Three Forms of Decision Making in Java if statements (test a boolean expression) switch statements (test an integer expression) conditional.
ICT Introduction to Programming Chapter 4 – Control Structures I.
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
Chapter 6 Conditionals. Learning Java through Alice © Daly and Wrigley Objectives List relational operators. List logical operators. Use the hierarchy.
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
Conditional Control Structures Chapter 5 Review. The If Statement The if statement is a conditional control structure, also called a decision structure,
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Chapter 3 Selection Statements
Chapter 4: Control Structures I
Chapter 3 Control Statements
Chapter 3: Decisions and Loops
Chapter 4: Control Structures I
Selections Java.
The switch Statement, and Introduction to Looping
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Decisions Chapter 4.
Chapter 4: Making Decisions.
Selection—Making Decisions
Introduction to programming in java
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
SELECTION STATEMENTS (1)
Decisions Given hours worked and pay rate, calculate total pay
JavaScript conditional
Chapter 4: Control Structures I
Chapter 4 Conditionals.
IF if (condition) { Process… }
Control Structures: Selection Statement
Chapter 7 Conditional Statements
Summary Two basic concepts: variables and assignments Basic types:
Introduction to Decision Structures and Boolean Variables
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Control Structure Chapter 3.
Lecture Notes – Week 2 Lecture-2
Boolean Expressions to Make Comparisons
PROGRAM FLOWCHART Selection Statements.
CHAPTER 5: Control Flow Tools (if statement)
Control Structures: Selection Statement
Control Structure.
Conditionals.
Chapter 6 Conditionals.
Presentation transcript:

Chapter 6 Conditionals

Objectives List relational operators. List logical operators. Use the hierarchy of operators chart to properly construct if/else statements. Construct switch statements. Use nested if statements.

Decisions True and false values are also known as Boolean values, named after the 19th century English mathematician George Boole.

Relational Operators A Boolean test compares primitives, constants, and variables and or objects using the following relational operators: Operator Meaning Example == Equal to x == 3 ! = Not equal to x != 3 < Less than x < 3 > Greater than x > 3 <= Less than or equal to x <= 3 >= Greater than or equal to x >= 3 Note: These relational operators must be typed exactly as above. You can’t type a space between the two equal signs and you can’t put =< to mean less than or equal.

Alice Example of an if statement :

Logical Operators Logical Operator Description && Short-curcuiting AND  && Short-curcuiting AND   || Short-circuiting OR    & AND   | OR   ! NOT   ^ Exclusive OR 

If Statements The syntax contains the keyword if, followed by a condition (boolean test) in parenthesis, followed by either a single statement or a block statement to execute if the test is true. An optional else keyword provides the alternative statement to execute if the test is false. if (condition) { statements to do when conditional is true } else { statements to do when conditional is false }

If Statements Examples if (x < y) { System.out.println ( "x is smaller than y" ); } if (x < y) { System.out.println ("x is smaller than y" ); } else { System.out.println ("x is not smaller than y" ); }

If Statements Examples Continued… if ( guess == correctAnswer ) { score = score + 10; System.out.println ( "You are right. You get 10 points."); } else { score = score - 5; System.out.println ( "Sorry, that is wrong. You lose 5 points."); }

Else If if (x < 0) { System.out.println ( "X is negative." ); } else if (x == 0) { System.out.println ( "X is equal to 0." ); } else { System.out.println ( "X is positive." ); }

Using Logical Operators The following only prints if x is less than y and x is less than z.  The && means if x is NOT less than y it won't even bother to test to see if x is less than z. if ( x < y  &&  x < z)               { System.out.println ("x is less than both y and z" ); } The following calculates grossPay if either condition is true.   The || means that if weeklyHours is less 40, it won't bother to test to see if employeeType is 'P'. if (weeklyHours < 40  ||  employeeType == 'P'  )       { grossPay =  weeklyHours * hourlyRate; }

Switch Statements Done with if statements: Done with switch statements: if (grade == 'A') {     System.out.println("Great!");  } else if (grade == 'B') {      System.out.println("Good!");  } else if (grade == 'C') {     System.out.println("Nice.");  } else {      System.out.println("Not Good."); }         switch (grade) {  case 'A':     System.out.println("Great!");     break; case 'B':     System.out.println("Good!");     break;  case 'C':      System.out.println("Nice.");      break; default:       System.out.println("Not Good.");  }

Complex If Statements vs. Switch Switch Statements: if  (month == 2) {        days =28; } else if ((month == 4) || (month == 6)  ||  (month == 9)  || (month == 11)) {         days=30; } else {      days=31; }         switch (month) {    case 2:             days = 28;              break;    case 4:  case 6:  case 9:  case 11:             days = 30;              break;    default:             days = 31;   }

Comparing Strings using if/else

Comparing Strings using the Switch