+92 332 4229 857 99ProjectIdeas@Gmail.com

Loops In Java


For loop:
 
Syntax:
          for(initialization ; Condition/Boolean Expression ; increment) {
                        //do work
}

Example:

for(int i = 1 ; i =< 10 ; i++) {
System.out.println(i); //printing 1 to 10 on console
}

while loop:
 
      Syntax:

                  while(Condition/Boolean Expression) {
                              //do work
           //increment
}

     Example:

                int i = 1;
while(i<=10) {
                System.out.println(i); //printing 1 to 10 on console
                i++;//i = i+1;
             }

do while
loop:
 
             Syntax:
                       
                  do {
                          //do work
      } while (condition);

             Example:

int i = 1;
                do {
                   System.out.println(i); //printing 1 to 10 on console
                   i++;//i = i+1;
        } while(i<=10);

foreach loop:
 
            Syntax:

for( initialization/declarations : expression ) {
            //do work
}

           Example:

String[] fruits = {"Apple","Mango","Banana"};
for(String everyfruit : fruits){
              System.out.println(everyfruit); //printing fruits on console
             }

0 comments: