Skip to content

Module 2: Control Flow Statements

Rishabh Malviya edited this page Feb 7, 2023 · 1 revision

Control Flow Statements

Control Flow Statements are statements that decide the flow of execution of the program. There are four types of Control Flow Statements.

  • For Loop
  • While Loop
  • Do-While Loop
  • Switch Statement

For Loop

  • When we know the number of iterations in advance, we use for loop.
  • It is a concise way of writing initialization, condition, and increment/decrement statements.
  • It is entry controlled loop means that the condition is checked before the execution of the loop.
// for loop
// for(initialization; condition; increment/decrement) {
//     // statements
// }
for (int i = 0; i < 10; i++) {
    System.out.println(i);
}
// Output: 0 1 2 3 4 5 6 7 8 9

Enhanced For Loop

  • In Java, for-each loop is used to iterate over the elements of an array or collections.
  • This works similar to for loop, but it is more concise and easy to use.
// enhanced for loop
// for(type var : array/collection) {
//     // statements
// }
int[] arr = {1, 2, 3, 4, 5};
for (int i : arr) {
    System.out.println(i);
}

While Loop

  • When we don't know the number of iterations in advance but known the terminating condition, we use while loop.
  • In while we can apply conditional increment/decrement statements.
  • It is entry controlled loop means that the condition is checked before the execution of the loop.
// while loop
// while(condition) {
//     // statements
// }
 while(i < 10) {
    System.out.println(i);
    i++;
}
 // Output: 0 1 2 3 4 5 6 7 8 9

Do-While Loop

  • When we don't know the number of iterations in advance, we use do-while loop.
  • In do-while we can apply conditional increment/decrement statements.
  • It is exit controlled loop means that the condition is checked after the execution of the loop.
  • The do-while loop is executed at least once even if the condition is false.
// do-while loop
// do {
//     // statements
// } while(condition);
int i = 0;
do {
    System.out.println(i);
    i++;
} while(i < 10);
// Output: 0 1 2 3 4 5 6 7 8 9

Switch Statement

  • When we have to select a one of multiple conditions.
  • The value of expression in switch statement is compared with the value of each case. If there is a match, the associated block of code is executed.
  • The break statement is used to terminate the execution of the switch statement.
  • The default statement is used to execute a block of code if there is no match in the switch statement.
// switch statement
// switch(expression) {
//     case value1:
//         // statements
//         break;
//     case value2:
//         // statements
//         break;
//     default:
//         // statements
// }
int i = 2;
switch(i) {
    case 1:
        System.out.println("One");
        break;
    case 2:
        System.out.println("Two");
        break;
    default:
        System.out.println("Invalid");
}
// Output: Two

Continue Statement/Break Statement

  • The continue statement is used to skip the current iteration of the loop.
  • The break statement is used to terminate the execution of the loop.
// continue statement
for (int i = 0; i < 10; i++) {
    if (i == 5) {
        continue;
    }
    System.out.println(i);
}
// Output: 0 1 2 3 4 6 7 8 9
// break statement
for (int i = 0; i < 10; i++) {
    if (i == 5) {
        break;
    }
    System.out.println(i);
}
// Output: 0 1 2 3 4

Labelled Statement / Unlabelled Statement

  • The break statement can be used to terminate the execution of the loop. But there are two forms of break statement.
  • Break Unlabel
    • The break statement without label is used to terminate the execution of the loop.
    • This is the basic break statement. It is used with for, while, switch, and do-while loops.
  • Break Label
    • The break statement with label is used to terminate the execution of the loop with the specified label.
    • This is useful when we have nested loops. and we want to terminate the execution of the outer loop.
    • The label is specified before the loop.
      // break statement with label
      // label:
      //     for (initialization; condition; increment/decrement) {
      //         // statements
      //         break label;
      //     }
      outer:
      for (int i = 0; i < 10; i++) {
          inner:
          for (int j = 0; j < 10; j++) {
              if (j == 5) {
                  break outer;
              }
              System.out.println(j);
          }
      }
      // Output: 0 1 2 3 4

Clone this wiki locally