190 likes | 205 Views
Lecture 12. Review (If-else Statement). if-else statement has the following syntax:. The condition must be a boolean expression . It must evaluate to either true or false. if ( condition ) { statement1 ; } else { statement2; }.
E N D
Review (If-else Statement) • if-else statement has the following syntax: The condition must be a boolean expression. It must evaluate to either true or false. if ( condition ){ statement1;} else { statement2; } If the condition is true, the statement1 is executed. If it is false, the statement2 is executed.
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
Example 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!!
Today’s topic • Repetition statement • while loop • Syntax • Infinite loop • Nested loop • for loop
Repetition Statements (Loops) • Repetition statements allow us to executea statement or a block of statements multiple times • Often we call them as loops • Like conditional statements, they are controlled by boolean expressions
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; }
F 3 <= 2 T 2 <= 2 T 1 <= 2 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 5 2 8 3 6 1 2 2 3 7 4 9 Printed out 1 2
Infinite Loops Be careful!!! • 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
Infinite Loops Be careful!!! • 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); }
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
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
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;
Make it double variableto calculate average of double type 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 );
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
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
Sample 1 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 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 2 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 ) { } …