For loops in java || with examples

loops are used to execute a set of statement repetedly until a particullar statement

 

there is 3 types of loops

  1. for loops

  2. while loop

  3. do-while loop

For loops :

Syntex>

for(statement1; stat.2;state3;){

  //code block to be executed

                                 } 

statement1 = initialisation(execute once)

statement2 = condition cheaking 

statement3 = reinitialisation

ex. 1.

for(int i = 0; i<100; i=i+1) {

      system.out.println(i);

  }

2. let get sum of number from 0 to n

package ……;
import java.util.Scanner;
public class ………{

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int x = sc.nextInt();
int n = x;
int sum = 0;

for(int i=0; i<=n; i=i+1) {
sum = sum + i;
}
System.out.println(sum);
}
}

               

Rishabh

I want to change the world with the help of tech 🙃

Leave a Reply