290 likes | 405 Views
Learn about if, if-else, nested if-else, and logical expressions in Java programming. Understand how to apply branching and conditional statements effectively.
E N D
Branching Condition Statement list 1 T Condition Statement list T F F Statement list 2 ITK 168
The Syntax of if and if-else statements A Boolean expression (logical expression). In Java, there are two possible values: true and false. if ( condition ) { statement list; } Reserved words A reserved word can’t be used as an identifier if ( condition ) { statement list1; } else { statement list2; } Indentation indicates that the statements in the statement list are at the level next to the if-else statement. ITK 168
Nested if/else statement if ( condition 1 ) { if ( condition 2 ) { statement list; } else { statement list; }; statement list; } else { statement list; } Indentation indicates the level of statements. ITK 168
while while (Condition) { Statement list } Condition Statement list T while (Condition); { Statement list } F ITK 168
, but not really Disney Ride in Program – OK n = Integer.parseInt( JOptionPane.showInputDialog(“How tall is the kid?”)); // IsBoy = true or false; if (h < 4) if (IsBoy)// Same asif (IsBoy == true) System.out.println(“Take a Mickey”); else System.out.println(“Take a Mini.”); else System.out.println(“You can ride!!”); System.out.println(“Go to the next stop.”); ITK 168
Ambiguity I saw the little girl with a binocular on the mountain top. Isawthe little girlwith a binocularon the mountain top. Isawthe little girl with a binocularon the mountain top. Isawthe little girl with a binocular on the mountain top. ITK 168
Rule: else belongs to the nearest if that not yet has an else if (condition1) if (condition2) { .....; .....; } else // not a kid { .....; .....; } Indentation can’t help compiler ITK 168
Boy Scout: Yes Is a kid? No Parent? Is a boy? No No Yes Yes Give a guidebook Give a badge Welcome ITK 168
Boy Scout: in program if (IsKid) { if (IsBoy) { System.out.println(“Take a Badge”); } } else // not a kid { if (IsParent) { System.out.println(“Take a Guidebook”); } } System.out.println(“Welcome, everybody!.”); ITK 168
if (IsKid) if (IsBoy) System.out.println(“Take a Badge”); else // not a kid if (IsParent) System.out.println(“Take a Guidebook”); System.out.println(“Welcome, everybody!.”); Boy Scout: wrong program if (IsKid) { if (IsBoy) { System.out.println(“Take a Badge”); } else // not a boy { if (IsParent) System.out.println(“Take a Guidebook”); } } System.out.println(“Welcome, everybody!.”); ITK 168
Other forms of Nested if/else statements (Cascaded) I if (condition_1) statement_1; if (condition_2) statement_2; if (condition_3) statement_3; if (condition_4) statement_4; if (condition_5) statement_5; if (condition_1) statement_1; else if(condition_2) statement_2; elseif(condition_3) statement_3; elseif(condition_4) statement_4; else if (condition_5) statement_5; V.S. ITK 168
Another form of Nested if/else statement(Cascaded) if (points >= 640) System.out.println(“A”); else if(points >= 500) System.out.println(“B”); else if(points >= 400) System.out.println(“C”); else if(points >= 300) System.out.println(“D”); else System.out.println(“F”); ITK 168
Other forms of Nested if/else statements (Cascaded) II Logical and if (condition_1) if (condition_2) if (condition_3) if (condition_4) statement_1; else statement_2; if ( condition_1 && condition_2 && condition_3 && condition_4 ) statement_1; else statement_2; = ITK 168
Switchvs.Cascaded if/else if (i == 1) statement_1; else if (i == 2) statement_2; else if (i == 3) statement_3; else if (i == 4) statement_4; else if (i == 5) statement_5; else if (i == 6) statement_6; switch (i) { case 1: statement_1; break; case 2: statement_2; break; case 3: statement_3; break; case 4: statement_4; break; case 5: statement_5; break; case 6: statement_6; break; } ITK 168
Operators • Arithmetic operators: • + - / * % • Relational operators: • == > < <= >= != • Logical operators: • || && ! ITK 168
|| && ! Logical Operators Assume x = 10 ((18 <= x) && (x <= 50)) ((18 <= x) || (x <= 50)) !(x < 5) is same as (x >= 5) (((x % 2) == 0) && ((x % 3) == 0)) false true true false ITK 168
De Morgan’s law I am not a female student. I am not female or I am not a student. I will not be in my office or in the lab. I will not be in my office and will not be in the lab. !(A && B) is same as !A || !B !(A || B) is same as !A && !B ITK 168
Extend City to a special one import becker.robots.*; publicclass City1 extends City { public City1 () { super(); new Wall(this,1,2,Direction.EAST); new Wall(this,1,2,Direction.WEST); new Wall(,1,2,Direction.NORTH); TriangleRobot mark = new TriangleRobot(this,0,0,Direction.NORTH); mark.drawTriangle(11, 4, 7); } } ITK 168
Extend City to a special one that takes parameters import becker.robots.*; publicclass TriangleCity extends City { public TriangleCity (int strNo, int aveNo, int height) { super(); new Wall(this,1,2,Direction.EAST); new Wall(this,1,2,Direction.WEST); new Wall(,1,2,Direction.NORTH); TriangleRobot mark = new TriangleRobot(this,0,0,Direction.NORTH); mark.drawTriangle(strNo, aveNo, height); } } ITK 168
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 0 1 2 3 4 5 6 7 8 9 10 11 TriangleRobot mark = new TriangleRobot(ny,0,0,Direction.NORTH); mark.drawTriangle(11, 4, 7); What kind of actions (services) do we need? ITK 168
TheTriangleRobotneeds to: • Know how to move to the right position (the left corner of the base). • Know how many things to put in one street (base). • Know how to move up • Know how to turn to the right direction • Know how to reduce the base for an upper street • Know when to stop ITK 168
The Robot needs to detect the environment Accesors ITK 168
Definite Loop • In programming a definite loop is more welcome. • I.e., number of iterationsisknown before the loop begins, at least the upper bound is known. • I.e., repeat the loop 100 times. • Precisely speaking, there is no definite loop in Java • We will talk about it in Chapter 5 ITK 168
for loop while loop Condition Statement list T repeat n times Statement list < n F = n while (Condition) { Statement list } for (???;???;???) { Statement list } ITK 168
The general format for a for loop for(Initialization_action;Condition;Condition_update) { statement_list; } 1 2 3 Summation1+2+3+...(n-1)+(n+1) int i, n, sum = 0; n = Integer.parseInt( JOptionPane.showInputDialog(“input n”)); for (i=0; i<=n; i++) { sum = sum + i; } System.out.println(“This answer is ” + sum); ITK 168
for(Initialization_action;Condition;Condition_update) { statement_list; } 1 2 3 Factorial of n isn(n-1)(n-2)...21 int n,f=1; n = Integer.parseInt( JOptionPane.showInputDialog(“input n”) ); if (n < 0) System.out.println(“No can do!!”); else for (i=2; i<=n; i++) { f = f*i; } System.out.println(“This answer is ” + f); ITK 168
Compare for and while int i; for (i=2; i<=n; i++) { f *= i; } i=2; while (i<=n) { f *= i; i++; } int i; for (i=0; i<n; i++) { .... .... .... .... } i=0; while (i<n) { .... .... i++; .... .... } ITK 168
A special scope for the index variable of the for loop for (int i=0; i<n; i++) { .... .... .... .... } scope int i scope ITK 168