Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 12. 2 Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.

Similar presentations


Presentation on theme: "Lecture 12. 2 Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition."— Presentation transcript:

1 Lecture 12

2 2 Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition must be a boolean expression. It must evaluate to either true or false. If the condition is true, the statement1 is executed. If it is false, the statement2 is executed.

3 3 Review (If-elseif Statement) if ( condition1 ) { statement1; } else if ( condition2 ) { statement2; } else { statement3; } The condition1 and condition2 must be a boolean expression. It must evaluate to either true or false. If the condition1 is true, the statement1 is executed. If the condition1 is false and condition2 is true, the statement2 is executed. Otherwise statement3 is executed

4 int MAX = 10; int num = 15; if ( num < MAX ) { System.out.println(“num is smaller?”); } else { System.out.println(“num is bigger!!”); } Print as num is bigger!! Example

5 Today’s topic Repetition statement –while loop Syntax Infinite loop Nested loop –for loop

6 Repetition Statements (Loops) Repetition statements allow us to execute a statement or a block of statements multiple times Often we call them as loops Like conditional statements, they are controlled by boolean expressions

7 7 The while Statement A while statement has the following syntax: If the condition is true, the statement is executed Then the condition is evaluated again, and if it is still true, the statement is executed again The statement is executed repeatedly until the condition becomes false while ( condition ) { statement; }

8 Example of while Statement If the condition of a while loop is false initially, the statement is never executed int count = 1; while (count <= 2) { System.out.println (count); count = count + 1; } 1 2 3 4 1  2 5 6 7 2  3 1 <= 2 T 2 <= 2 T 3 <= 2 F 8 9 Printed out 1 2

9 9 Infinite Loops Executing the statements in while loop must eventually make the condition false If not, it never gets out of the loop –this is called an infinite loop, which will execute until the user interrupts the program You should always double check the logic of a program and boolean expression to ensure that your loops will terminate Be careful!!!

10 Infinite Loops An example of an infinite loop: –There is no update for the value of count!! –This loop will continue executing until the user externally interrupts the program int count = 1; while (count <= 25) { System.out.println (count); } Be careful!!!

11 Nested Loops The body of a loop can contain another loop For each iteration of the outer loop, the inner loop iterates completely Similarly, you can have nested if statements

12 Nested Loops How many times will the string "Here" be printed? count1 = 1; while (count1 <= 2) { count2 = 1; while (count2 <= 3) { System.out.println ("Here"); count2 = count2 + 1; } count1 = count1 + 1; } 2 * 3 = 12

13 Increment/decrement operator When you have counter in loop, you can use increment/decrement operator ++ –e.g. count++;  count = count + 1; –– –e.g. count--;  count = count – 1;

14 Example int count = 0; double total = 0.0; while (count < 5) { int num = console.readInt(“Enter num > “); total = total + num; count++; } double average = total / count; console.println( “Average is ” + average ); Make it double variable to calculate average of double type

15 Exercise! Let’s update MyConverter –Change for loop to while loop –When users enter ‘0’, print out a message and exit The converter will ask users to enter one of conversions they want to perform 0. Quit 1. Mile to Kilometer conversion 2. Fahrenheit to Celsius conversion Other numbers: Error messages

16 public class MyConverter { public static void main(String[] argv) { JFrame frame = new JFrame(“My Calculator"); IOConsole console = new IOConsole(); frame.getContentPane().add(BorderLayout.CENTER, console); frame.setSize(500, 300); frame.setVisible(true); console.println( “********** MENU ***********”); console.println( “ 0. Quit ” ); console.println( “ 1. Distance (Mile->Kilo)” ); console.println( “ 2. Temperature (F->C) ” ); console.println( “******************************”); next page

17 int option; while( ) { option = console.readInt( “Which conversion ? > “ ); if ( option == 1 ) { } else if ( option == 2 ) { } else { } Write statements to convert Mile to Kilometer Write statements to convert F to C Print out error messages for users ? Sample 1

18 int option = -1; while( option > 0 ) { option = console.readInt( “Which conversion ? > “ ); if ( option == 1 ) { } else if ( option == 2 ) { } else { } Write statements to convert Mile to Kilometer Write statements to convert F to C Print out error messages for users Sample 1

19 int option; boolean quit = false; while( quit == false ) { option = console.readInt( “Which conversion ? > “ ); if ( option == 0 ) { console.println( “Thank you for using!!!”); quit = true; } else if ( option == 1 ) { } Sample 2 …


Download ppt "Lecture 12. 2 Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition."

Similar presentations


Ads by Google