A for loop may contain multiple initialization and/or multiple update expression.
for(i=1,sum=0;i<=n;sum+=i,++i)
System.out.println(i);
All the three expressions of the for loop are optional.
for(;test-expression;update-expression(s))
OR
for(initailization;test-expression;)
OR
for(;test-expression;)
An infinite loop is an endless loop which can be created by omitting the test-expression.
for(j=25; ;--i)
System.out println("coderzpy!");
This is also known as a time delay loop, which is often used in programs
for(j=20;k>=0;--i)
A loop may contain another loop inside its body. This form of a loop is known as nested loop
Note: The inner loop must terminate before the outer loop.
class Demo {
public static void main(String[] args) {int i,j;
for(i=1;i<=5;i++){
for(j=1;j<=i;j++){
System.out.print("*");
}
System.out.println(" ");
}
}
}
Output:
*
**
***
****
*****
Though Java loops can be used in almost all situations, yet there are some situations where one loop fits better than the other.
Note: also read about the Iteration Statements(Loops)
If you like my post please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…