160 likes | 414 Views
int a,b,s. Flowchart for Asg3. Generate 2 random integers for a, b. Show question and get answer s. no. yes. s=a+b?. Draw a crying face. Draw a smiling face. End. if -- else. if (s == a+b) { // // draw a smiling face // } else { //
E N D
int a,b,s Flowchart for Asg3 Generate 2 random integers for a, b Show question and get answer s no yes s=a+b? Draw a crying face Draw a smiling face End
if -- else if (s == a+b) { // // draw a smiling face // } else { // // draw a crying face // } // The rest of the // program Statement list 1 Condition Statement list 1 T Could have another if F Statement list 2 Statement list 2
Disney Roller Coaster Ride Measure the kid’s height, h h < 4 No Yes Ride the roller coaster Boy or Girl? boy girl Give a Mickey Give a Mini Next Stop
Disney Ride in Program h = 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.”); } } // end true part of if (h < 4) else // else part of if (h < 4) { System.out.println(“You can ride!!”); } // end else part of if (h < 4) System.out.println(“Go to the next stop.”);
, 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.”);
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.
Boy Scout: Yes Is a kid? No Parent? Is a boy? No No Yes Yes Give a guidebook Give a badge Welcome
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!.”);
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!.”);
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.
Ternary Operator (?:) if ( condition ) { return X; } else { return Y; } A = (condition ? X : Y) System.out.println(score >= 60 ? “passed” : “fail”);
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”);
Another form of Nested if/else statement(Cascaded) 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; else if(condition_6) statement_6; if (condition_1) if(condition_2) if(condition_3) if(condition_4) if(condition_5) if(condition_6) statement_1; else statement_2; if ( condition_1 && condition_2 && condition_3 && condition_4 && condition_5 && condition_6 && ) statement_1; else statement_2;
Example: (x,y) (20,40) (40,52) (50,60) (70,75)