Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sahar Mosleh California State University San MarcosPage 1 Program Control Statement.

Similar presentations


Presentation on theme: "Sahar Mosleh California State University San MarcosPage 1 Program Control Statement."— Presentation transcript:

1 Sahar Mosleh California State University San MarcosPage 1 Program Control Statement

2 Sahar Mosleh California State University San MarcosPage 2 Example of simple loop class Grade{ public static void main(String args[]) { } public static void FindLetterGrade(int Score) { char LetterGrade; if ( Score =90 ) { LetterGrade = 'A'; System.out.println ("The letter Grade is " + LetterGrade); } else if ( Score =80 ) { LetterGrade = 'B'; System.out.println ("The letter Grade is " + LetterGrade); } else if (Score =70) { LetterGrade = 'C'; System.out.println ("The letter Grade is " + LetterGrade); } else if (Score =60) { LetterGrade = 'D'; System.out.println ("Thr letter Grade is " + LetterGrade); } else { LetterGrade = 'F'; System.out.println ("The letter Grade is " + LetterGrade); }

3 Sahar Mosleh California State University San MarcosPage 3 Nested loop Sometimes, the body of a loop is again a loop. We say that the inner loop is nested inside an outer loop. This happens often when you process two- dimensional structures, such as tables. Lets look at an example that looks a bit more interesting than a table of numbers. We want to generate the following triangular shape: [] [] [] [] [] [] [] [] [] [] []

4 Sahar Mosleh California State University San MarcosPage 4 The basic idea is simple. We generate a sequence of rows: for ( int i=1; i<= width; i++ ) { // Make triangle row … } How you make a triangle row? Use another loop to concatenate the squares [] for that row. Then add a newline character at the end of the row. The ith row has i symbols, so the loop counter goes from 1 to i. For ( int j=1; j<= i; j++) r = r + “[]”; r = r + “\n”;

5 Sahar Mosleh California State University San MarcosPage 5 Putting both loops together yields two nested loops: String r= “”; for ( int i=1; i<= width; i++ ) { // Make triangle row For ( int j=1; j<= i; j++) r = r + “[]”; r = r + “\n”; System.out.println( r ); }

6 Sahar Mosleh California State University San MarcosPage 6 The while loop The general form of the while loop is: while (condition) statement; where statement can be a single statement or a block of statements, and condition that controls the loop, and it may be any valid boolean expression. The loop repeats while the condition is true When the condition becomes false, program control passes to the line immediately following the loop.

7 Sahar Mosleh California State University San MarcosPage 7 class WhileDemo { public static void main(String args[]) { char ch; // print the alphabet using a while loop ch = ‘a’; while (ch <=‘z’) { System.out.print(ch); ch++; } Output: abcdefghijklmnopqrstuvwxyz

8 Sahar Mosleh California State University San MarcosPage 8 Break statement break statement within the body of the loop forces an immediate exit from a loop, bypassing any remaining code in the body of the loop and the loop’s conditional test. If the break statement in the loop is executed, the loop is terminated and the control resumes at the next statement following the loop.

9 Sahar Mosleh California State University San MarcosPage 9 class BreakDemo { public static void main(String args[]) { int num = 100; // loop while i-squared is less than num for (int i=0; i < num; i++) { //terminate the loop if i*i < num if (i*i >=num) break; System.out.print(i+ “ ”); } System.out.println(“Loop complete.”); } Output: 0 1 2 3 4 5 6 7 8 9 Loop complete.

10 Sahar Mosleh California State University San MarcosPage 10 Continue statement This is the complement of break statement. Continue forces the next iteration of a loop. class ContDemo { public static void main(String args[]) { int i; // print even numbers between 0 and 100 for (i=0;i<100; i++) { if ((i%2) != 0) continue; System.out.println(i); }

11 Sahar Mosleh California State University San MarcosPage 11 The Switch statement Switch provides for a multiway branch Although, the nested-if ladder can do the same thing in some situations, it is more feasible to use switch block The general form of switch block is: switch(expression) { casecondition1: statement sequence; break casecondition2: statement sequence; break; casecondition3: statement sequence; break... … default: statement sequence; }

12 Sahar Mosleh California State University San MarcosPage 12 Rules of switch block The switch expression must be of type char, byte, short, or int. (floating point expressions are not allowed) The case constants must be literals of the type compatible with the expression No two case constant must be identical The default statement is optional in the switch block and is executed if all other options fails.

13 Sahar Mosleh California State University San MarcosPage 13 class SwitchDemo { public static void main(String args[]) { int I; I = 3; switch (I) { case 0: System.out.println(“I is zero”);break; case 1: System.out.println(“I is one”);break; case 2: System.out.println(“I is two”);break; case 3: System.out.println(“I is three”);break; default: System.out.println(“probably, I is four or more”); }


Download ppt "Sahar Mosleh California State University San MarcosPage 1 Program Control Statement."

Similar presentations


Ads by Google