Program Control Statement Sahar Mosleh California State University

  • Slides: 13
Download presentation
Program Control Statement Sahar Mosleh California State University San Marcos Page 1

Program Control Statement Sahar Mosleh California State University San Marcos Page 1

Example of simple loop class Grade{ public static void main(String args[]) { } public

Example of simple loop class Grade{ public static void main(String args[]) { } public static void Find. Letter. Grade(int Score) { char Letter. Grade; if ( Score <=100 && Score >=90 ) { Letter. Grade = 'A'; System. out. println ("The letter Grade is " + Letter. Grade); } else if ( Score <=89 && Score >=80 ) { Letter. Grade = 'B'; System. out. println ("The letter Grade is " + Letter. Grade); } else if (Score <=79 && Score >=70) { Letter. Grade = 'C'; System. out. println ("The letter Grade is " + Letter. Grade); } else if (Score <=69 && Score >=60) { Letter. Grade = 'D'; System. out. println ("Thr letter Grade is " + Letter. Grade); } else { Letter. Grade = 'F'; System. out. println ("The letter Grade is " + Letter. Grade); } } } Sahar Mosleh California State University San Marcos Page 2

Nested loop • Sometimes, the body of a loop is again a loop. We

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: [] [] [] [] Sahar Mosleh California State University San Marcos Page 3

 • The basic idea is simple. We generate a sequence of rows: for

• 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”; Sahar Mosleh California State University San Marcos Page 4

 • Putting both loops together yields two nested loops: String r= “”; for

• 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 ); } Sahar Mosleh California State University San Marcos Page 5

The while loop • The general form of the while loop is: while (condition)

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. Sahar Mosleh California State University San Marcos Page 6

class While. Demo { public static void main(String args[]) { char ch; } }

class While. Demo { 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 Sahar Mosleh California State University San Marcos Page 7

Break statement • break statement within the body of the loop forces an immediate

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. Sahar Mosleh California State University San Marcos Page 8

class Break. Demo { public static void main(String args[]) { int num = 100;

class Break. Demo { 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. Sahar Mosleh California State University San Marcos Page 9

Continue statement • This is the complement of break statement. Continue forces the next

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

The Switch statement • Switch provides for a multiway branch • Although, the nested-if

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) { case condition 1: statement sequence; break case condition 2: statement sequence; break; case condition 3: statement sequence; break. . . … default: statement sequence; } Sahar Mosleh California State University San Marcos Page 11

Rules of switch block • The switch expression must be of type char, byte,

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. Sahar Mosleh California State University San Marcos Page 12

class Switch. Demo { public static void main(String args[]) { int I; I =

class Switch. Demo { 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”); } } } Sahar Mosleh California State University San Marcos Page 13