Presentation is loading. Please wait.

Presentation is loading. Please wait.

Branching Statement Condition Statement Condition list 1 list

Similar presentations


Presentation on theme: "Branching Statement Condition Statement Condition list 1 list"— Presentation transcript:

1 Branching Statement Condition Statement Condition list 1 list
F F Statement list 2 5/7/2019 ITK 168

2 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. 5/7/2019 ITK 168

3 Nested if/else statement
if ( condition 1 ) { if ( condition 2 ) statement list; } else }; Indentation indicates the level of statements. 5/7/2019 ITK 168

4 while Statement Condition list while (Condition) { Statement list }
F 5/7/2019 ITK 168

5 Disney Ride in Program – OK
, 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 as if (IsBoy == true) System.out.println(“Take a Mickey”); else System.out.println(“Take a Mini.”); System.out.println(“You can ride!!”); System.out.println(“Go to the next stop.”); 5/7/2019 ITK 168

6 Ambiguity I saw the little girl with a binocular on the mountain top.
5/7/2019 ITK 168

7 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 5/7/2019 ITK 168

8 Boy Scout: Is a kid? Parent? Is a boy? Give a guidebook Give a badge
Yes Is a kid? No Parent? Is a boy? No No Yes Yes Give a guidebook Give a badge Welcome 5/7/2019 ITK 168

9 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!.”); 5/7/2019 ITK 168

10 Boy Scout: wrong 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!.”); 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!.”); 5/7/2019 ITK 168

11 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; else if (condition_3) statement_3; else if (condition_4) statement_4; else if (condition_5) statement_5; V.S. 5/7/2019 ITK 168

12 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”); Points Grade 650 -- A B C D 0--299 F 5/7/2019 ITK 168

13 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; = 5/7/2019 ITK 168

14 Switch vs. 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; case 3: statement_3; case 4: statement_4; case 5: statement_5; case 6: statement_6; } 5/7/2019 ITK 168

15 == > < <= >= !=
Operators Arithmetic operators: / * % Relational operators: == > < <= >= != Logical operators: || && ! 5/7/2019 ITK 168

16 Logical Operators || && ! Assume x = 10 false true true false
|| && ! 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 5/7/2019 ITK 168

17 De Morgan’s law !(A && B) is same as !A || !B
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 5/7/2019 ITK 168

18 Extend City to a special one
import becker.robots.*; public class 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); } 5/7/2019 ITK 168

19 Extend City to a special one that takes parameters
import becker.robots.*; public class 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); } 5/7/2019 ITK 168

20 What kind of actions (services) do we need?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 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? 5/7/2019 ITK 168

21 The TriangleRobot needs 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 5/7/2019 ITK 168

22 The Robot needs to detect the environment
+ public # protected - private Robot - int street - int avenue - Direction direction - ThingBag backbag + Robot(City aCity, int aStreet, int anAvenue, Direction aDir) + void move() + void turnLeft() + void pickThing() + void putThing() + boolean canPickThing() + int countThingsInBackpack() + boolean frontIsClear() + int getAvenue() + Direction getDirection() + String getLabel() + double getSpeed() + int getStreet() Accesors 5/7/2019 ITK 168

23 Definite Loop In programming a definite loop is more welcome.
I.e., number of iterations is known 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 5/7/2019 ITK 168

24 while loop for loop Statement Condition Statement list list
repeat n times Statement list < n F = n while (Condition) { Statement list } for (???;???;???) { Statement list } 5/7/2019 ITK 168

25 The general format for a for loop
for (Initialization_action; Condition; Condition_update) { statement_list; } 1 2 3 Summation (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); 5/7/2019 ITK 168

26 Factorial of n is n(n-1)(n-2)...21
for (Initialization_action; Condition; Condition_update) { statement_list; } 1 2 3 Factorial of n is n(n-1)(n-2)...21 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); 5/7/2019 ITK 168

27 Compare for and while int i; i=2; 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++; } 5/7/2019 ITK 168

28 A special scope for the index variable of the for loop
for (int i=0; i<n; i++) { .... } scope int i scope 5/7/2019 ITK 168


Download ppt "Branching Statement Condition Statement Condition list 1 list"

Similar presentations


Ads by Google