Professional Documents
Culture Documents
Presentation
Presentation
1. While Statement
2. Do while Statement
3. For statement
Importance & uses
Execution of a repetition statement
Process
REPETITION STATEMENT
Repetition statement (or loop) a block of code to be executed for a fixed number of times or
until a certain condition met.
OR
Repetitive statements, also known as loops or iteration structures, are programming constructs
that allow executing a block of code repeatedly based on a condition or a fixed number of
iterations.
Repetition statements are called loops, and are used to repeat the same code mulitple times in
succession. The number of repetitions is based on criteria defined in the loop structure, usually
a true/false expression.
TYPES
Here are three types of a repetition statement as below;
1. While statement
2. Do while statement
3. For statement
WHILE
The while statement evaluates expression/condition, which
must return a boolean value. If the expression/condition is
true, statement(s) in the while block is/are executed.
Syntax:
while (expression/condition)Statement(s)
It continues testing the expression/condition and executing
its block until the expression/condition evaluates to false.
Example
int i=1;
while (i<5) {
System.out.print(i + “”); i++; }
Output? 1234
DO WHILE STATEMENT
It is similar to while loops except it first executes the statement(s)
inside the loop, and then evaluates the expression/condition. If the
expression is true, the statement(s) in the do loop is/are executed
again.
Syntax
Before the loop begins, any necessary variables are initialized. - This step prepares the loop for execution.
Condition checking
The loop checks a condition before each iteration to determine whether to continue or exit. - If the condition
is true, the loop body executes. If false, the loop terminates.
Execution of loop body
The block of code within the loop body is executed. - This block may contain various instructions,
calculations, or operations.
Update variable
Inside the loop body, variables may be updated to control the loop's behavior. - This step ensures progress
towards the loop's termination condition.
CONTINUE..
Repeat
After executing the loop body, the loop returns to the condition checking phase. - If the
condition is still true, the process repeats. Otherwise, the loop exits.
Termination
The loop terminates when the condition evaluates to false. - At this point, control passes to the
next statement following the loop.