Loops allow you to execute a singe statement or a block of statements multiple times. They are widely used when you want to display a series of numbers and accept repetitive input.
- while loop
- for loop
- do-while Loop
The while loop executes a block of code as long as the given condition remains true. The while loop begins with the while keyword, which is followed by a parentheses containing a boolean condition. If the condition returns true, the block of statements within the while loop are executed. After every iteration, the program control is transferred back to the wile statement, where the condition is again checked for another round of execution. This process is continued till the specified condition becomes false. Once the condition becomes false, the while statement stops the execution of loop and transfer the control to next statement appearing after the block.
The syntax demonstrates how to use the while loop.
Condition: Is a boolean expression.
The for loop is similar to the while loop in functionality. It executes the statements within the loop as long as the given condition is true. Unlike the while loop, the for loop specifies the loop control statements at the top instead of the body of the loop.
The for loop begins with the for keyword, which is followed by a parentheses containing three expressions, each of which are separated by a semicolon. The three expressions are referred as initialization expression, condition expression, and increment/decrement expression respectively. These three expressions are optional.
1. initialization: Initializes the variables(s) that will be used in the condition.
2. condition: Comprises of the condition that is checked before the statements in the loop are executed.
3. Comprises of the statement that changes the value of the variables(s) on every successful execution of the loop to ensure that the condition specified in the condition section is reached. The increment and decrement operators like ++, -- and shortcut operators like += or -= are used in this section.
The syntax demonstrates how to use the for loop:
for(initialization; condition; inc/dec)
The do-while loop is similar to the while loop. This is because both do-while and while loops execute until the condition becomes false. However, the do-while loop differs by executing the body of the loop at least once before evaluating the condition. Thus, even if the condition is false, the do-while executes at least once.
The do-while loop starts with do keyword and is followed by a block of statements. At the end of the block, the while keyword is specified that is followed a parentheses containing the condition. When the specified condition returns false, the block of statements after the do keyword are ignored and the next statement following the while statement is executed.
The syntax demonstrates how to use the do-while loop:
} while (condition);
Condition: Is a boolean expression.
The break statement can be used with decision-making such as switch-case and loop constructs such as for and while loops. The break statement is denoted by the break keyword. It is used to exit the loop without evaluating the specified condition. The control is then passed to the next statement immediately after the loop.
The continue statement is mostly used in the loop constructs. The continue statement is denoted by the continue keyword. It is used to terminate the current execution of the loop and continue with the next repetition by returning the control to the beginning of the loop. This means, the continue statement will not terminate the loop entirely but terminates the current execution.