development of diagnostic enzyme assay to detect leuser virus
Java Repetiotion Statements
1. University of Kufa
Collage of Computer Science and
Mathematics
lecture5
م
.
م
االمين الحسن عبد هدى
2. Repetition Statements
Repetition statements allow us to execute a statement multiple times
Often they are referred to as loops
Like conditional statements, they are controlled by boolean
expressions
Java has three kinds of repetition statements: while, do while,
and for loops
3. The while Statement
A while statement has the following syntax:
while ( condition )
statement;
If the condition is true, the statement is executed
Then the condition is evaluated again, and if it is still true, the statement is
executed again
The statement is executed repeatedly until the condition becomes false
4.
5. The while Statement
An example of a while statement:
If the condition of a while loop is false initially, the statement is never executed
Therefore, the body of a while loop will execute zero or more times
int count = 1;
while (count <= 5)
{
System.out.println (count);
count++;
}
Output :
1
2
3
4
5
6. Nested Loops
Similar to nested if statements, loops can be nested as well
That is, the body of a loop can contain another loop
For each iteration of the outer loop, the inner loop iterates
completely
7. How many times will the string "Here" be printed?
count1 = 1;
while (count1 <= 10)
{
count2 = 1;
while (count2 < 20)
{
System.out.println ("Here");
count2++;
}
count1++;
}
8. How many times will the string "Here" be printed?
count1 = 1;
while (count1 <= 10)
{
count2 = 1;
while (count2 < 20)
{
System.out.println ("Here");
count2++;
}
count1++;
}
10 * 19 = 190
9. The Do while Statement
Do-while loop is similar to while loop, however there is a
difference between them: In while loop, condition is
evaluated before the execution of loop’s body but in do-
while loop condition is evaluated after the execution of
loop’s body.
10. The Do while Statement
Syntax of do-while loop:
Do
{ statement (S);
} while (condition);
11. How do-while loop works?
First, the statements inside loop execute and then the
condition gets evaluated, if the condition returns true
then the control gets transferred to the “do” else it jumps
to the next statement after do-while.
15. For Loop
A For statement has the following syntax:
for(initialization; condition ; increment/decrement)
{ statement (S) ;
}
16. Flow of Execution of the for Loop
As a program executes, the interpreter always keeps track of which
statement is about to be executed. We call this the control flow, or the flow
of execution of the program.
17.
18. For Loop
First step: In for loop, initialization happens first and only one time, which
means that the initialization part of for loop only executes once.
Second step: Condition in for loop is evaluated on each iteration, if the
condition is true then the statements inside for loop body gets executed.
Once the condition returns false, the statements in for loop does not execute
and the control gets transferred to the next statement in the program after
for loop
Third step: After every execution of for loop’s body, the
increment/decrement part of for loop executes that updates the loop
counter.
Fourth step: After third step, the control jumps to second step and condition
is re-evaluated.