forked from hotwax/training-assignment
-
Notifications
You must be signed in to change notification settings - Fork 0
Module 2: Control Flow Statements
Rishabh Malviya edited this page Feb 7, 2023
·
1 revision
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
- 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- 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);
}- When we don't know the number of iterations in advance but known the terminating condition, we use while loop.
- In
whilewe 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- When we don't know the number of iterations in advance, we use do-while loop.
- In
do-whilewe 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-whileloop 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- When we have to select a one of multiple conditions.
- The value of expression in
switchstatement is compared with the value of each case. If there is a match, the associated block of code is executed. - The
breakstatement is used to terminate the execution of theswitchstatement. - The
defaultstatement is used to execute a block of code if there is no match in theswitchstatement.
// 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- The
continuestatement is used to skip the current iteration of the loop. - The
breakstatement 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- The break statement can be used to terminate the execution of the loop. But there are two forms of break statement.
- Break Unlabel
- The
breakstatement without label is used to terminate the execution of the loop. - This is the basic break statement. It is used with
for,while,switch, anddo-whileloops.
- The
- Break Label
- The
breakstatement 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
- The