Java | Loops: Loops also conditional statements. There are two loops one is while loop and another one is for loop. Loops are executes the statements multiple times until the condition of loop become false.
Syntax: while( condition )
{
// statements...
}
for(variable assignment; condition ; modification of variable)
{
// statements...
}
Here is the Code:
- import java.io.*;
- class Main {
- public static void main (String[] args) {
- int i = 1;
- while(i <= 5)
- {
- System.out.print(i+" ");
- i++;
- }
- System.out.println();
- int sum = 0;
- for(int j=1;j<=5;j++)
- {
- sum+=j;
- }
- System.out.println(sum);
- }
- }
Output:
1 2 3 4 5
15
Here is the Video: