Chapter 6 Conditionals. Learning Java through Alice © Daly and Wrigley Objectives List relational operators. List logical operators. Use the hierarchy.

Slides:



Advertisements
Similar presentations
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Advertisements

Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
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
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
If Statement if (amount
CS 117 Spring 2002 Decision Making Hanly Chapter 3 Friedman-Koffman Chapter 4.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
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,
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)
Control Structures 1. Control Structures Java Programming: From Problem Analysis to Program Design, D.S. Malik 2.
Chapter 4: Control Structures SELECTION STATEMENTS.
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
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Boolean Expressions and if-else Statements Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006.
1 CS1001 Lecture Overview Java Programming Java Programming Arrays Arrays.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
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 statements Mostafa Abdallah
Chapter 9 Control Structures.
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
Control Statements: Part1  if, if…else, switch 1.
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.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Chapter 3 Selection Statements
Java Programming Fifth Edition
Chapter 3: Decisions and Loops
Chapter 4: Control Structures I
Chapter 6 Conditionals.
Selections Java.
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.
Selection—Making Decisions
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
SELECTION STATEMENTS (1)
JavaScript conditional
Chapter 4: Control Structures I
Chapter 4 Conditionals.
IF if (condition) { Process… }
Control Structures: Selection Statement
Introduction to Decision Structures and Boolean Variables
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Control Structure Chapter 3.
Chapter 2 Programming Basics.
PROGRAM FLOWCHART Selection Statements.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Control Structures: Selection Statement
Control Structure.
Conditionals.
Chapter 6 Conditionals.
Presentation transcript:

Chapter 6 Conditionals

Learning Java through Alice © Daly and Wrigley 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. 2

Learning Java through Alice © Daly and Wrigley Decisions True and false values are also known as Boolean values, named after the 19th century English mathematician George Boole. 3

Learning Java through Alice © Daly and Wrigley Relational Operators A Boolean test compares primitives, constants, and variables and or objects using the following relational operators: OperatorMeaningExample ==Equal tox == 3 ! =Not equal tox != 3 <Less thanx < 3 >Greater thanx > 3 <=Less than or equal tox <= 3 >=Greater than or equal tox >= 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. 4

Learning Java through Alice © Daly and Wrigley Alice Example of an if statement : 5

Learning Java through Alice © Daly and Wrigley Logical Operators Logical OperatorDescription &&Short-curcuiting AND ||Short-circuiting OR &AND |OR !NOT ^Exclusive OR 6

Learning Java through Alice © Daly and Wrigley 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 } 7

Learning Java through Alice © Daly and Wrigley If Statements Examples if (x < y) { System.out.println ( "x is smaller than y" ); } else { System.out.println ("x is not smaller than y" ); } 8

Learning Java through Alice © Daly and Wrigley 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."); } 9

Learning Java through Alice © Daly and Wrigley 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." ); } 10

Learning Java through Alice © Daly and Wrigley 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; } 11

Learning Java through Alice © Daly and Wrigley 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."); } 12

Learning Java through Alice © Daly and Wrigley Complex If Statements vs. Switch Complex If Statements: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; } 13

Learning Java through Alice © Daly and Wrigley Comparing Strings using if/else 14

Learning Java through Alice © Daly and Wrigley Comparing Strings using the Switch 15