Basic Java Programming
Sasidhara Marrapu
Execution Flow Control
 Control Flow  Lab Problem
 Home Work
Execution Control
To do or not to do
that is the question.
If
if( ) { } else { }
if( ) { } else { }
// Do this stuff
if (a <= b && x != 4) {
// Do this if true
}
else {
// Do this if false
}
// Do this stuff
Any expression with
a boolean result.
if( ) { } else { }
Example
// Do this stuff
if (a <= b && x != 4) {
x = x * a;
z = b * 2;
}
else {
b = z / 2;
a = 3;
}
// Do this stuff
if( ) { } else { }
Brackets not always Required
// Do this stuff
if (a <= b && x != 4) {
// Do this if true
}
else {
// Do this if false
}
// Do this stuff
{ } not needed if there
is only a single statement.
if( ) { } else { }
Example
// Do this stuff
if (a <= b && x != 4)
x = x * a;
else
b = z / 2;
// Do this stuff
if() {} else {}
Optional else
// Do this stuff
if (a <= b && x != 4) {
// Do this if true
}
else {
// Do this if false
}
// Do this stuff The else clause
is optional.
if( ) { } else { }
Example
// Do this stuff
if (a <= b && x != 4) {
x = x * a;
z = b * 2;
}
// Do this stuff
if( ) { } else { }
Example with Single Statement
// Do this stuff
If (a <= b && x != 4)
x = x * a;
// Do this stuff
// Do this stuff
if (a <= b && x != 4) x = x * a;
// Do this stuff
if( ) { } else { }
A string of ifs
// Do this stuff
if (a <= b) {
// Do this if a <= b
}
else if ( r > 5 ) {
// Do this if !(a <= b) && r > 5
}
else {
// Do this if !(a <= b) && !(r > 5)
}
// Do this stuff
if( ) { } else { }
Nesting ifs
// Do this stuff
if (a <= b) {
// Do this if a <= b
if ( r > 5 ) {
// Do this if a <= b && r > 5
} else {
// Do this if !(a <= b) && !(r >
5)
}
}
// Do this stuff
The Looping Statements
while ( ) { }
do { } while ( )
for ( ; ; ) { }
while ( ) { }
// Do this stuff
while (a <= b) {
// Do this while a <= b
}
// Do this stuff
Any expression with a boolean
result. The contents of the loop
are only executed while true.
while ( ) { }
while ( ) { }
Example
// Do this stuff
i = 1;
while (i <= 5) {
System.out.println("i = " + i);
i++;
}
// Do this stuff i = 1
i = 2
i = 3
i = 4
i = 5
>
while ( ) { }
Example
// Do this stuff
i = 1;
while (i <= 5) {
System.out.println("i = " + i);
i++;
}
// Do this stuff
// Do this stuff
i = 1;
while (i <= 5) {
System.out.println("i = " + i++);
}
// Do this stuff
// Do this stuff
i = 1;
while (i <= 5) System.out.println("i = " + i++);
// Do this stuff
i = 1
i = 2
i = 3
i = 4
i = 5
>
do { } while ( )
do { } while ( )
// Do this stuff
do {
// Do this while a <= b
} while (a <= b)
// Do this stuff
Any expression with a boolean
result. The contents of the loop
are always executed once and
then only executed if true.
do { } while ( )
Example
// Do this stuff
i = 6;
do {
System.out.println("i = " + i);
i--;
} while (i <= 5 && i > 0)
// Do this stuff
i = 6
i = 5
i = 4
i = 3
i = 2
i = 1
>
for loop
for ( ; ; ) { }
// . . .
for (i = 1; i < 5; i++) {
System.out.println("i = " + i);
}
// . . .
Initialization(s) executed once
before the for loop is entered.
Any expression with a boolean
result. The contents of the loop
are only executed if true.
Statement(s) that
increment variables
used in the for loop.
for ( ; ; ) { }
// . . .
for (i = 1; i < 5; i++) {
System.out.println("i = " + i);
}
// . . .
i = 1
i = 2
i = 3
i = 4
>
for ( ; ; ) { }
// . . .
for (i = 1, j = 4; i < 5; i++, j--) {
System.out.println("i, j = " + i + ", "
+ j);
}
// . . .
i, j = 1, 4
i, j = 2, 3
i, j = 3, 2
i, j = 4, 1
>
Initializations separated by a comma.
Statements separated by a commas.
for each { }
// . . .
for(data_type item : collection) {
...
}
Data type of Item in the collection
Collection of Items.
break and continue
// . . .
int i = 0;
while (true) {
i++;
System.out.println("i = " + i);
if (i == 2) continue;
System.out.println("No continue " +
i);
if (i >= 4) break;
System.out.println("No break " + i);
}
// . . .
break and continue Example
// . . .
int i = 0;
while (true) {
i++;
System.out.println("i = " + i);
if (i == 2) continue;
System.out.println("No continue " +
i);
if (i >= 3) break;
System.out.println("No break " + i);
}
// . . .
i = 1
No continue 1
No break 1
i = 2
i = 3
No continue 3
>
Labeled break and continue
// . . .
int i = 0;
outer:for (int k = 10; k < 99; k++) {
while (true) {
i++;
System.out.println("i, k = " + i", " + k);
if (i == 2) continue outer;
System.out.println("No continue " + i);
if (i >= 3) break outer;
System.out.println("No break " + i);
}
}
// . . .
The label must immediately precede the loop.
i, k = 1, 10
No continue 1
No break 1
i, k = 1, 10
No continue 1
No break 1
i, k = 2, 10
i, k = 1, 10
No continue 1
No break 1
i, k = 2, 10
i, k = 3, 11
No continue 3
>
Labeled break and continue
Example
// . . .
int i = 0;
outer:for (int k = 10; k < 20; k++) {
while (true) {
i++;
System.out.println("i, k = " + i", " + k);
if (i == 2) continue outer;
System.out.println("No continue " + i);
if (i >= 3) break outer;
System.out.println("No break " + i);
}
}
// . . .
return
• In a method the return statement "breaks" out of the method.
• It can be used to break out of a loop and the method.
The switch Statement
switch ( ) { }
Must be an expression that evaluates
to an int, byte, short or char.
Must be an constant expression that evaluates
at compile time. No variables allowed.
default is a keyword.
switch is a keyword.
case is a keyword.
switch ( ) { }
// . . .
switch ( i ){
case 1 : System.out.println("one"); break;
case 2 : System.out.println("two"); break;
case 4 : System.out.println("four"); break;
default: System.out.println("default");
}
// . . .
default
default
one
default
one
two
default
one
two
default
switch ( ) { }
Execution Flow
// . . .
for (int i = 0, i < 5, i++) {
switch ( i ){
case 1 : System.out.println("one"); break;
case 2 : System.out.println("two"); break;
case 4 : System.out.println("four"); break;
default: System.out.println("default");
}
}
// . . .
default
one
two
default
four
>
switch ( ) { }
The default case is optional
// . . .
for (int i = 0, i < 5, i++) {
switch ( i ){
case 1 : System.out.println("one"); break;
case 2 : System.out.println("two"); break;
case 4 : System.out.println("four"); break;
}
}
// . . .
one
two
four
>
switch ( ) { }
With some missing breaks
// . . .
for (int i = 0, i < 5, i++) {
switch ( i ){
case 1 : System.out.println("one"); break;
case 2 : System.out.println("two"); break;
case 4 : System.out.println("four"); break;
default: System.out.println("default");
}
}
// . . .
// . . .
for (int i = 0, i < 5, i++) {
switch ( i ){
case 1 : System.out.println("one");
case 2 : System.out.println("two");
case 4 : System.out.println("four"); break;
default: System.out.println("default");
}
}
// . . .
default
default
one
two
four
default
one
two
four
two
four
default
one
two
four
two
four
default
default
one
two
four
two
four
default
four
default
one
two
four
two
four
default
four
>
switch ( ) { }
Using missing breaks
char c;
// . . .
switch ( c ){
case 'a':
case 'e':
case 'i':
case 'o':
case 'u': System.out.println("vowel"); break;
default: System.out.println("consonant");
}
// . . .
switch ( ) { }
Format Alternatives
// . . .
for (int i = 0, i < 5, i++) {
switch ( i ){
case 1 : System.out.println("one"); break;
case 2 : System.out.println("two"); break;
case 4 : System.out.println("four"); break;
default: System.out.println("default");
}
}
// . . .
// . . .
for (int i = 0, i < 5, i++) {
switch ( i ){
case 1 : System.out.println("one");
break;
case 2 : System.out.println("two");
break;
case 4 : System.out.println("four");
break;
default: System.out.println("default");
}
}
// . . .
// . . .
for (int i = 0, i < 5, i++) {
switch ( i ){
case 1 : System.out.println("one"); break;
case 2 : {
System.out.print("t");
System.out.print("w");
System.out.println("o");
} break;
case 4 : System.out.println("four"); break;
default: System.out.println("default");
}
}
// . . .
Summary
•if ( ) { } else { }
•while ( ) { }
•do { } while ( )
•for ( ; ; ) { }
•break and continue
•return
•switch ( ) { }
Home Work

Pj01 5-exceution control flow

  • 1.
  • 2.
    Execution Flow Control Control Flow  Lab Problem  Home Work
  • 3.
    Execution Control To door not to do that is the question.
  • 4.
    If if( ) {} else { }
  • 5.
    if( ) {} else { } // Do this stuff if (a <= b && x != 4) { // Do this if true } else { // Do this if false } // Do this stuff Any expression with a boolean result.
  • 6.
    if( ) {} else { } Example // Do this stuff if (a <= b && x != 4) { x = x * a; z = b * 2; } else { b = z / 2; a = 3; } // Do this stuff
  • 7.
    if( ) {} else { } Brackets not always Required // Do this stuff if (a <= b && x != 4) { // Do this if true } else { // Do this if false } // Do this stuff { } not needed if there is only a single statement.
  • 8.
    if( ) {} else { } Example // Do this stuff if (a <= b && x != 4) x = x * a; else b = z / 2; // Do this stuff
  • 9.
    if() {} else{} Optional else // Do this stuff if (a <= b && x != 4) { // Do this if true } else { // Do this if false } // Do this stuff The else clause is optional.
  • 10.
    if( ) {} else { } Example // Do this stuff if (a <= b && x != 4) { x = x * a; z = b * 2; } // Do this stuff
  • 11.
    if( ) {} else { } Example with Single Statement // Do this stuff If (a <= b && x != 4) x = x * a; // Do this stuff // Do this stuff if (a <= b && x != 4) x = x * a; // Do this stuff
  • 12.
    if( ) {} else { } A string of ifs // Do this stuff if (a <= b) { // Do this if a <= b } else if ( r > 5 ) { // Do this if !(a <= b) && r > 5 } else { // Do this if !(a <= b) && !(r > 5) } // Do this stuff
  • 13.
    if( ) {} else { } Nesting ifs // Do this stuff if (a <= b) { // Do this if a <= b if ( r > 5 ) { // Do this if a <= b && r > 5 } else { // Do this if !(a <= b) && !(r > 5) } } // Do this stuff
  • 14.
    The Looping Statements while( ) { } do { } while ( ) for ( ; ; ) { }
  • 15.
    while ( ){ } // Do this stuff while (a <= b) { // Do this while a <= b } // Do this stuff Any expression with a boolean result. The contents of the loop are only executed while true.
  • 16.
  • 17.
    while ( ){ } Example // Do this stuff i = 1; while (i <= 5) { System.out.println("i = " + i); i++; } // Do this stuff i = 1 i = 2 i = 3 i = 4 i = 5 >
  • 18.
    while ( ){ } Example // Do this stuff i = 1; while (i <= 5) { System.out.println("i = " + i); i++; } // Do this stuff // Do this stuff i = 1; while (i <= 5) { System.out.println("i = " + i++); } // Do this stuff // Do this stuff i = 1; while (i <= 5) System.out.println("i = " + i++); // Do this stuff i = 1 i = 2 i = 3 i = 4 i = 5 >
  • 19.
    do { }while ( )
  • 20.
    do { }while ( ) // Do this stuff do { // Do this while a <= b } while (a <= b) // Do this stuff Any expression with a boolean result. The contents of the loop are always executed once and then only executed if true.
  • 21.
    do { }while ( ) Example // Do this stuff i = 6; do { System.out.println("i = " + i); i--; } while (i <= 5 && i > 0) // Do this stuff i = 6 i = 5 i = 4 i = 3 i = 2 i = 1 >
  • 22.
  • 23.
    for ( ;; ) { } // . . . for (i = 1; i < 5; i++) { System.out.println("i = " + i); } // . . . Initialization(s) executed once before the for loop is entered. Any expression with a boolean result. The contents of the loop are only executed if true. Statement(s) that increment variables used in the for loop.
  • 24.
    for ( ;; ) { } // . . . for (i = 1; i < 5; i++) { System.out.println("i = " + i); } // . . . i = 1 i = 2 i = 3 i = 4 >
  • 25.
    for ( ;; ) { } // . . . for (i = 1, j = 4; i < 5; i++, j--) { System.out.println("i, j = " + i + ", " + j); } // . . . i, j = 1, 4 i, j = 2, 3 i, j = 3, 2 i, j = 4, 1 > Initializations separated by a comma. Statements separated by a commas.
  • 26.
    for each {} // . . . for(data_type item : collection) { ... } Data type of Item in the collection Collection of Items.
  • 27.
    break and continue //. . . int i = 0; while (true) { i++; System.out.println("i = " + i); if (i == 2) continue; System.out.println("No continue " + i); if (i >= 4) break; System.out.println("No break " + i); } // . . .
  • 28.
    break and continueExample // . . . int i = 0; while (true) { i++; System.out.println("i = " + i); if (i == 2) continue; System.out.println("No continue " + i); if (i >= 3) break; System.out.println("No break " + i); } // . . . i = 1 No continue 1 No break 1 i = 2 i = 3 No continue 3 >
  • 29.
    Labeled break andcontinue // . . . int i = 0; outer:for (int k = 10; k < 99; k++) { while (true) { i++; System.out.println("i, k = " + i", " + k); if (i == 2) continue outer; System.out.println("No continue " + i); if (i >= 3) break outer; System.out.println("No break " + i); } } // . . . The label must immediately precede the loop.
  • 30.
    i, k =1, 10 No continue 1 No break 1 i, k = 1, 10 No continue 1 No break 1 i, k = 2, 10 i, k = 1, 10 No continue 1 No break 1 i, k = 2, 10 i, k = 3, 11 No continue 3 > Labeled break and continue Example // . . . int i = 0; outer:for (int k = 10; k < 20; k++) { while (true) { i++; System.out.println("i, k = " + i", " + k); if (i == 2) continue outer; System.out.println("No continue " + i); if (i >= 3) break outer; System.out.println("No break " + i); } } // . . .
  • 31.
    return • In amethod the return statement "breaks" out of the method. • It can be used to break out of a loop and the method.
  • 32.
  • 33.
    Must be anexpression that evaluates to an int, byte, short or char. Must be an constant expression that evaluates at compile time. No variables allowed. default is a keyword. switch is a keyword. case is a keyword. switch ( ) { } // . . . switch ( i ){ case 1 : System.out.println("one"); break; case 2 : System.out.println("two"); break; case 4 : System.out.println("four"); break; default: System.out.println("default"); } // . . .
  • 34.
    default default one default one two default one two default switch ( ){ } Execution Flow // . . . for (int i = 0, i < 5, i++) { switch ( i ){ case 1 : System.out.println("one"); break; case 2 : System.out.println("two"); break; case 4 : System.out.println("four"); break; default: System.out.println("default"); } } // . . . default one two default four >
  • 35.
    switch ( ){ } The default case is optional // . . . for (int i = 0, i < 5, i++) { switch ( i ){ case 1 : System.out.println("one"); break; case 2 : System.out.println("two"); break; case 4 : System.out.println("four"); break; } } // . . . one two four >
  • 36.
    switch ( ){ } With some missing breaks // . . . for (int i = 0, i < 5, i++) { switch ( i ){ case 1 : System.out.println("one"); break; case 2 : System.out.println("two"); break; case 4 : System.out.println("four"); break; default: System.out.println("default"); } } // . . . // . . . for (int i = 0, i < 5, i++) { switch ( i ){ case 1 : System.out.println("one"); case 2 : System.out.println("two"); case 4 : System.out.println("four"); break; default: System.out.println("default"); } } // . . . default default one two four default one two four two four default one two four two four default default one two four two four default four default one two four two four default four >
  • 37.
    switch ( ){ } Using missing breaks char c; // . . . switch ( c ){ case 'a': case 'e': case 'i': case 'o': case 'u': System.out.println("vowel"); break; default: System.out.println("consonant"); } // . . .
  • 38.
    switch ( ){ } Format Alternatives // . . . for (int i = 0, i < 5, i++) { switch ( i ){ case 1 : System.out.println("one"); break; case 2 : System.out.println("two"); break; case 4 : System.out.println("four"); break; default: System.out.println("default"); } } // . . . // . . . for (int i = 0, i < 5, i++) { switch ( i ){ case 1 : System.out.println("one"); break; case 2 : System.out.println("two"); break; case 4 : System.out.println("four"); break; default: System.out.println("default"); } } // . . . // . . . for (int i = 0, i < 5, i++) { switch ( i ){ case 1 : System.out.println("one"); break; case 2 : { System.out.print("t"); System.out.print("w"); System.out.println("o"); } break; case 4 : System.out.println("four"); break; default: System.out.println("default"); } } // . . .
  • 39.
    Summary •if ( ){ } else { } •while ( ) { } •do { } while ( ) •for ( ; ; ) { } •break and continue •return •switch ( ) { }
  • 40.