Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 11

REPETITION

STATEMENT AND ITS


EXECUTION
Submitted by : Group no. 1
Sallal jaffar 1014 M.Sharjeel 1034
Zainab 5017 Azeema Hareem 1010
OUTLINE
 Repetition statements
 Types

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

do statement (s) ; While (expression) ;


 It continues executing the statement until the expression/condition
becomes false.
 Note that the statement within the do loop is always executed at
least once.
Example
int i=0; do { System.out.print(i + “” i++; } while(i<=3);
Output 0 1 2 3 4
FOR STATEMENT
 Usually used when a loop will be executed a set number of
times.
 Syntax

for (initial statement; loop condition; update statement)


statement(s);
 The for loop executes as follow:

1. The initial statement is executed.


2. The loop expression/condition is evaluated. If it is TRUE,
the loop statement is executed followed by the execution
of the update statement
3. Repeat step 2, until the loop condition evaluates to FALSE
Example
for (i=1; i<5; i++)System.out.print(i);
IMPORTANCE & USES
Importance
1. Allow automating repetitive tasks.
2. Improve code readability and Maintainability.
3. Reduce redundancy in code.
Uses
4. Processing elements of a list or array.
5. Iterating over database records.
6. Implementing game loops
7. Performing calculations until a condition is met.
Consideration
8. Ensure proper termination conditions to prevent infinite loops.
9. Optimize loop structures for performance, especially in large-scale applications
Repetitive statements are fundamental to programming and enable efficient automation of tasks requiring repeated execution.
EXECUTION OF REPETITIVE
STATEMENT
Repetitive statements such as loops follow a specific execution process as below;
 Initialization

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.

You might also like