Download presentation
Presentation is loading. Please wait.
1
Chapter 5 Selection Statements
2
Topics Controlling program flow selection –if –switch Boolean expressions –boolean primitive type –comparison operators –boolean operators
3
Control Statements Statements in programs are normally executed in sequence. Method calls transfer control to another part of the code (and back again) A statement that alters the control flow is called a control statement. –Selection (branching) allows program to decide whether to do a part of the code –Repetition (Chapter 6) allows sections of code to be repeated Exceptions (Chapter 8) also alter the flow of a program
4
The if Statement The if statement is one type of selection statement. BufferedReader kbd = new BufferedReader( new InputStreamReader( System.in)); System.out.println(“Enter test score:”); String input = kbd.readLine(); int testScore = Integer.parseInt( input); if (testScore < 70) System.out.println(“You did not pass”); else System.out.println(“You passed”);
5
The if Statement The if statement specifies which block of code to execute, depending on the result of evaluating a test condition called a boolean expression. if ( ) else –The is an expression that evaluates to either true or false.
6
Syntax of an if statement Mapping of the sample if statement to the general format.
7
Boolean Expressions and Variables A boolean expression is either true or false. –true and false are reserved words There is a primitive data type called boolean which can take only these two values. We can declare a variable of data type boolean and assign a boolean value to it. boolean pass, done; pass = 70 < x;// compute the value done = true;
8
Boolean Expressions The six relational operators we can use in conditional expressions are: <less than <=less than or equal to ==equal to !=not equal to > greater than >=greater than or equal to These operators are used with primitive operands
9
Flow chart for if Statement
10
if Without else The if statement does not have to contain an else condition. –In the if-then structure, no special instructions are executed if the boolean expression evaluates to false. if (testScore >=95){ System.out.println( “You are an honor student”); } behaves the same as if (testScore >=95){ System.out.println( “You are an honor student”); } else { }
11
Compound Statements What if you need to do multiple statements in the if and else parts of an if statement? –Make a compound statement by putting curly braces around all the statements to be done
12
Flow Chart: if without else
13
Boolean Operators A boolean operator takes boolean values as its operands and returns a boolean value. The three boolean operators are AND&&binary OR||binary NOT!unary
14
Truth Tables Boolean operators and their meanings: Short-circuit evaluation DeMorgan's Laws pq!pp&&qp||q true falsetrue false true falsetrue falsetrue false truefalse
15
Precedence rules OrderOperatorRule first()innermost first left to right -, !single operand *, /, %left to right +, -left to right =, >left to right == !=left to right &&left to right ||left to right last=right to left
16
Nested-if Statements An if statement that contains another if statements in either its then or else block is called a nested-if statement. if (testScore >= 70){ if (studentAge < 10){ System.out.println(“You did a great job”); } else { System.out.println(“You did pass”); //test score >=70 and age >=10 } } else { //test score < 70 System.out.println(“You did not pass”); }
17
Nested if flow chart
18
Dangling else CAUTION: Be careful with nested if statements that don't have the else part. if (x < y) if (x < z) System.out.println(“Hello”); else System.out.println(“Goodbye”); An else belongs to the nearest if unless curly braces are used to indicate otherwise. Compiler doesn't look at indentation.
19
Comparing Objects When two variables are compared, we are comparing their contents. In the case of objects, the content is the address where the object is stored. –using == and != on objects compares their addresses To compare the content of objects you need to define comparison methods for the class. –equals (inherited from Object) –compareTo (Chapter 9)
20
Equality of Objects How the equality == testing works with the objects. To test the equality of the data, the equals method needs to be defined for the weight class
21
Comparing Objects : Example Class Ch5Weight{... public boolean equals(Ch5Weight wgt) { boolean result; double thisGram = this.getGram(); double otherGram = wgt.getGram(); if (thisGram == otherGram){ result = true; } else { result = false; } return result; }... }
22
The switch Statement The switch statement provides an efficient way to evaluate and process multiple options. switch (gradeLevel){ case 1: System.out.println(“Go to the gymnasium”); break; case 2: System.out.println(“Go to the Science Auditorium”); break; case 3: System.out.println(“Go to Halligan Hall Room 104”); break; case 4: System.out.println(“Go to Root Hall Room 101”); break; }
23
The switch Statement The syntax for the switch statement is switch ( ){ :... : default: }
24
The switch Statement The data type of must be char, byte, short, or int. The value of is compared against the constant i of. If there is a matching case, its case body is executed. Otherwise, the execution continues to the statement following the switch statement.
25
The switch Statement The break statement causes execution to skip the remaining portion of the switch statement and resume execution following the switch statement. The break statement is necessary to execute statements in one and only one case.
26
Effect of break on switch Without breakWith break
27
5.5 The switch Statement It is good practice to implement a default case in a switch statement. The default case will be executed if there is no matching case.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.