- Get link
- X
- Other Apps
Features of loops
In the previous posts, we have learnt for loop, while loop and do while loop. In this post, we will learn interesting features of for loop.
The for loop
We will discuss some additional features of for loop here,
- The initialization expression can be used for initializing more than one variable where each variable initialization is separated by commas. Similarly, the increment/decrement expression may increment or decrement more than one variables simultaneously.For example: for(i=1 , j=n ; i<=n ;i++ , ,j--)
- The initialization expression of for loop can also be a local variable declaration statement. For example: If i and j are not declared before using then in the for loop you can rewrite the above example as: for(int i=1 ,int j=n, i<=n ; i++ , j--)
- Although more than one initialization and increment/decrement expressions is allowed in the loop but only one conditional expression is allowed in the for loop. This conditional expression may contain compound relational expression and also testing need not be limited to the loop control variable. For example:
int sum=0;
for(int i=5 ; i>30 || sum<1000 ; i+=3) {..........}
- All the three expressions in the for loop are optional. In other words, it is not necessary that all three loop expressions must be present in the for loop statement, though semicolon are imperative.
- The initialization expression can be omitted if loop control variable is already initialized. For example: int i=1, for( ; i<=15;i++){....}, here as we have already initialized variable i so we can omit in for loop but semicolon is mandatory.
- If the loop condition is omitted , then Java assumes that condition is true thus creating an infinite loop. In such case ,loop can be terminated using break statement or throwing an exception.For example, for(i=1; ;i++){...}. loop will work infinitely.
- The increment /decrement expression can be omitted if it is evaluated in the body of the loop or if not required.For example, for( i=5; i<=15; ){..i++;..}, The increment/decrement expression in the body of loop works in same way as in syntax statement.
- Placing an semicolon at the end of loop header results in an logical error. It create a for loop without loop body and executes infinitely.For example, for(i=1;i<5;i++);
- The initialization expression is executed only once before the evaluation of test condition for the first time in for loop.
- The increment/decrement expression is evaluated each time after the execution of the body of loop and before checking the condition for next time.
- The for loop is expressively equivalent to while and do-while loop. In other words, you can use loop in any of these three forms.
Nested for loop
Like nested if-else , the for loop can also be nested. In other words, we can have a for loop in the body of other for loop.There is no restrictions on the level of nesting, you can write as many as nested for loops.But one should take care while nesting at multiple levels otherwise unexpected result may occur.
Lets understand nested for loop with an example,
Example: Write a program to print a pyramid pattern of numbers.
In the above example,
- First, we declare variable "num", which is used to get number of lines to be printed form the user.
- In the first for loop, we have initialized a loop control variable "i" with 1, and test condition make sure the loop works until the i is greater than num.
- In the body of first for loop, we wrote another nested for loop which have loop control variable "j"assigned 1 to it. This loop will work until j is less than or equal to loop variable "i".
- Lets understand the output, user inputs 5, so now num=5, loop1 will check if i is less than or equal to 5.If i is less than 5, control will move to second loop which checks if j is less than or equal to i. If this condition is true only then the value of i is printed .
- When the nested loop's condition becomes false, the control jumps out and goes to the empty print statement which is used to move next output to new line just like enter.
- This process goes on until i is less than or equal to 5.Analyse the output.
1
2 3
4 5 6
7 8 9 10
Keep Learning ! Happy Coding!
Comments
Post a Comment
If you have any doubt, ask here