Categories
java

do while in java with example

do while in java with example

In this tutorial, we will learn do while in java with example

do while is little bit different from other looping statements, because other looping statements while execute only when condition satisfy

But do while will execute without condition once, after that it will check condition to execute next statement

Syntax

do{  
//code
}while(condition);  

Example

public class DoWhileExample {
	  
	public static void main(String[] args) {
       int i = 1;
       do {
          System.out.println("Loop Count " + i);
          i++;
      } while (i <= 5);
   }
}

Output

Loop Count 1
Loop Count 2
Loop Count 3
Loop Count 4
Loop Count 5
Download

https://github.com/rkumar9090/BeginnersBug/blob/master/BegineersBug/src/com/geeks/example/DoWhileExample.java

Related Articles

https://beginnersbug.com/while-loop-using-boolean-datatype-in-java/

Categories
java

for loop in java

for loop used to iterate an array or used to repeat a set of code for predefined times

Syntax

for(;;){
    // your logic
}

Example

   for (int i = 0; i < 10; i++) {
             System.out.println("Loop count " + i);
         }

Ouput

Loop count 0
Loop count 1
Loop count 2
Loop count 3
Loop count 4
Loop count 5
Loop count 6
Loop count 7
Loop count 8
Loop count 9
Categories
java

while loop in java

 

In this post, we will learn about while loop in java with an example

while is a keyword in java used to loop in condition-based

while(condition){ 
  // your logic here
}

Example

public class WhileExample {

	public static void main(String[] args) {
		int i = 0;
		while (i < 5) {
			System.out.println("Loop Number " + i);
			i++;
		}
	}
}

Output

Loop Number 0
Loop Number 1
Loop Number 2
Loop Number 3
Loop Number 4

while loop using boolean datatype in java

https://beginnersbug.com/while-loop-using-boolean-datatype-in-java/