1 / 19

Lecture 12

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; }.

kathlyn
Télécharger la présentation

Lecture 12

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 12

  2. 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.

  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. 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!!

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

  6. 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

  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. 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

  9. 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

  10. 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); }

  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. 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 );

  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. 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

  18. 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

  19. 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 ) { } …

More Related