Page 182 - Cs_withBlue_J_C11_Flipbook
P. 182
2. Null Loop: A loop that does not contain any statement inside the body of the loop is called Null Loop. It is also called
Empty Loop as it has an empty loop body.
For example:
for(i=1; i<=100;i=i+1);
The above loop does not contain any statement and is terminated by a semicolon(;).
Similarly, we can also use while and do-while loops as a null loop.
3. Omitting Expression: In a loop, we can omit one or two parameters of the loop.
For example:
for(i=1; i<=10;)
{
System.out.println(i++);
}
Here, the step value (i.e., increment or decrement) is omitted.
4. Delay Loop: A delay loop is used to increase the time of the execution of the program. It pauses the execution of
the program for some finite amount of time. It has an empty loop body.
For example:
int i=1;
while(i<=1000)
{
i++;
}
Here, the while loop only increments the value of i to 1000 and does nothing else.
Difference between for loop and while loop:
For Loop While Loop
In the for loop, all the parameters (i.e., initialisation, In the while loop, the parameters (i.e., initialisation,
test condition and increment or decrement) are in test condition and increment or decrement) are
the same line. written in separate lines.
This loop is used to execute a set of statements This loop is used to execute a set of statements
repeatedly when the number of iterations is known. repeatedly when the number of iterations is not known.
This loop is more concise and suitable for simple This loop provide more flexibility when dealing with
iteration. complex loop conditions.
Nested Loop
When a loop is present inside the body of another loop, it is called a Nested loop. It is also called loop inside loop.
The syntax of a nested loop:
Outer loop structure
{
Inner loop structure
{
Statements inside inner loop
}
Statements inside outer loop
}
Since all types of loops can have nested loop, the three types of nested loops are as follows:
• Nested for loop: The for loop inside another for loop is called a nested for loop.
For example: Output:
2
for(i=1; i<=3; i++)
{ 44
for(j=1; j<=i; j++) 666
180180 Touchpad Computer Science-XI

