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 using boolean datatype in java

while loop using boolean datatype in java

In this tutorial, we will learn about while loop using boolean datatype in java

we can make an infinite loop in java using while keyword

Example

public class WhileExample {

	  public static void main(String[] args) {
      boolean isTrue = true;
      while (isTrue) {
       System.out.println("Inside while loop");
      }
  }

}
Reference

https://beginnersbug.com/while-loop-in-java/

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/

Categories
java

If else condition in java

If else condition in java

In this tutorial, we will learn about If else condition in java

if else is used to check the logical condition in java

Syntax of If condition

if(condition){
// your logic can be written here
}

Syntax for if else

if(condition){
// your logic can be written here
}
else{
// else logic
}

Syntax for else if 

if(condition){
     // your if logic can be written here
}
else if {
     // your else if logic can be written here
}
else{
     // else logic
}

Example

public class IfConditonExample {
	  
	public static void main(String[] args) {
       int i = 1;
       if (i > 1) {
           System.out.println("I is greater than 1");
       } else if (i < 1) {
          System.out.println("I value less than 1");
       } else {
          System.out.println("I value is 1");
       }
    }
}

Output

I value is 1
Reference

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

Categories
java

Exception Handling in java

Exception Handling in java

In this tutorial we will learn about Exception Handling in java


What is Exception

Any Error occurred while compiling or run time is called exception  

What is exception handling

Grace fully handling that exception in our application is called exception handling 

Why we need exception handling

The users should not get affected for our code bug or our environment issue,so we can handle it while developing itself

How to handle a exception

Java has strong exception handling mechanism, It has so many pre defined exception class and also it is allowing us to create our own custom exception class

How to achieve it in java

Using try, catch,throws,throw keywords we can handle exception in java

Example

public class ExceptionHandling {

	public static void main(String[] args) {
		try {
			System.out.println(5 / 0);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

Output

java.lang.ArithmeticException: / by zero
     at com.geeks.overloading.ExceptionHandling.main(ExceptionHandling.java:7)

In the above code we intentionally divide a number by 0 which thrown an arithmetic exception which is caught by catch block

Finally keyword

finally is a keyword which will execute as a last part of execution, even any exception occurred also the finally method will execute

Example

public class ExceptionHandling {

	public static void main(String[] args) {
		try {
			System.out.println(5 / 0);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			System.out.println("The Program will exit now ");
		}
	}

}

Output

java.lang.ArithmeticException: / by zero
     at com.geeks.overloading.ExceptionHandling.main(ExceptionHandling.java:7)
 The Program will exit now 
Reference

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