Skip to main content

Conditional Statements: Loops

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:

  1. import java.io.*;

  2. class Main {
  3. public static void main (String[] args) {
  4.     int i = 1;
  5.     while(i <= 5)
  6.     {
  7.         System.out.print(i+" ");
  8.         i++;
  9.     }
  10.     System.out.println();
  11.     int sum = 0;
  12.     for(int j=1;j<=5;j++)
  13.     {
  14.         sum+=j;
  15.     }
  16.     System.out.println(sum);
  17. }
  18. }

Output:
1 2 3 4 5 
15                                          

Here is the Video: