Posted By:
3 Feb, 2014 9:42 am PST

Using if - else statements in Java

 Using if statements

Many times, we would like to execute some code statement only if some condition holds true. In java, we can achieve this using the if statement. Consider the following program:

OUTPUT:

Above, for loop executes 5 times with i varying from 1 to 5. However, the display is generated only for even numbers.

Basic Anatomy of the if statement

The anatomy of the if statement is as follows:

The if...else Statement:

An if statement can be followed by an optional else statement, which executes when the Boolean expression is false.

Syntax:

The syntax of an if...else is:

if(Boolean_expression){

   //Executes when the Boolean expression is true

}else{

   //Executes when the Boolean expression is false

}

Example:

public class Test{

   public static void main(String args[]){

      int x =30;

      if( x <20){

         System.out.print("This is if statement");

      }else{

         System.out.print("This is else statement");

      }

   }

}

This would produce the following result:

 

The if...else if...Statement:

Many times we want to check for multiple conditions. If condition 1 is satisfied do a certain block of code statements, else if condition 2 is satisfied do a second block of code statements, else if condition 3 is satisfied do another block of statements, and so on. We can extend the if statement with an else if.

To understand this, consider the following program:

class IfElseIf{

public static void main(String[] args)

{

int i;

int remainder;

for(i = 1; i <=5 ; i++)

{

remainder = i % 2;

if(remainder == 0)

{

System.out.print(i);

System.out.println(" is even");

}

else if (remainder == 1)

{

System.out.print(i);

System.out.println(" is odd");

}

}

}

}

Output:

 The anatomy of the extended if …else  if statement

There is no practical limit on the number of else if statements. You may have 0,1, or more than one.

 Example:

 

class Ex0510{

public static void main(String [] args)

{

int i;

int remainder;

for ( i = 1; i <=5; i++)

{

remainder = i % 3;

if (remainder == 0)

{

System.out.println("Remainder is 0");

}

else if(remainder == 1)

{

System.out.println("Remainder is 1");

}

else if (remainder == 2)

{

System.out.println("Remainder is 2");

}

}

}

}

OUTPUT:

Using if … else … else if … else statements

The final extension of the if statement uses if, else if, and a final else as well. The final else is used to catch any condition not considered in the if and else if conditions.

An if statement can have one if, zero or more else if, and zero or one else clauses.

EXAMPLE:

                                                 class Ex0511{

public static void main(String [] args)

{

int i;

int remainder;

for(i=1; i <=5; i++)

{

remainder = i % 5;

if (remainder == 0)

{

System.out.println("Remainder is 0");

}

else if (remainder == 1)

{

System.out.println("Remainder is 1");

}

else if (remainder == 2)

{

System.out.println("Remainder is 2");

}

else if (remainder > 2)

{

System.out.println("Remainder > 2");

}

}

}

}

 OUTPUT:

Are the {} required in if statements?

In java, the curly braces are used whenever we need to group together more than one statement. Consider the following program:

class Ex0512{

public static void main(String [] args)

{

int time = 17;

if (time < 12)

System.out.print("Good");

System.out.print("Morning");

}

}

OUTPUT:

  

The output above is not correct. If the time is before 12, we want both Good and Morning to be displayed. If the time > = 12,nothing should be  displayed. What has happened above?

Since the time is >=12, the condition in the if statement evaluates to a false. The next statement is not executed. The statement after that gets executed. If we want to put more than one statement to be executed/not-executed, we need to put these statements inside the curly braces {} as shown below:

class Ex0513{

public static void main(String [] args)

{

int time = 17;

if (time < 12)

{

System.out.print("Good");

System.out.print("Morning");

}

}

}

  OUTPUT:

The same concept of curly braces {} applies to for loops as well. Create a file Ex0514.java as shown below:

                                                 class Ex0514{

public static void main(String[] args)

{

int i;

for(i = 1; i <=3; i ++)

System.out.println(i);

System.out.println("The end");

}

}

OUTPUT:

 

Java assumes that without {}, only the next statement is a part of the forloop.

Good programming practice suggests you use the curly braces {} even if you have single statement. This can prevent logic errors should you modify your program later on.

Nesting if statements

The statement just after the if statement is what gets executed if the condition is true. In case we have curly braces {} after the if statement, then all the statements inside the {} will get executed if the condition is true.

The statement following the if statement can be any java statement, including another if statement.

Example:

                                                 class Ex0515{

public static void main(String[] args)

{

int time = 17;

if( time >12)

if(time <16)

System.out.println("Good afternoon");

else

System.out.println("Good Evening");

}

}

OUTPUT:

 Using break to exit from loops

Create a file Ex0516.java as shown below:

                                                 class Ex0516{

public static void main(String [] args)

{

int i;

System.out.println("Starting the for loop");

for ( i = 1; i <=10; i++)

{

System.out.println(i);

if(i % 4 ==0)

{

break;

}

}

System.out.println("Ending the for loop");

}

}

OUTPUT:

The use of break takes you out of the loop. Then, the statement after the end of the loop executes. See the pictorial below:

 

 

 

 

 

 

 

 

 

 

 

 

Attachment: Ifastatement.zip


IF ELSE STATEMENT

Java if else program uses if else to execute statement(s) when a condition is fulfilled.

Syntax:

if(condition)

{

//Statement

}

else if(condition)

{

//Statement

}

else

//Statement

 

Example:To determine whether a student has passed an exam.

 

publicclass if_else {

publicstaticvoid main(String[] args) {

int marksObtained = 50;

int passingMarks = 40;

if (marksObtained >= passingMarks) {

System.out.println("You passed the exam.");

}

else {

System.out.println("Unfortunately you failed to pass the exam.");

}

}

}

 

Was it useful?  



How to write simple  if-else loop in Java?

If statement can be used to make decision in Java.Syntax for if statement is :

Syntax:

if (condition)

{

    statements;

}

else

{

   statements;

}

For example suppose if you have declared an integer variable named var and you want to print a message when the value is 10,then you can do as follows:

 if(var==10)

      System.out.println(“The value of variable var is 10”);

 

//To find the greatest of two numbers

public class Greater{

public static void main(String args[]){

                        int a=4,b=8;

                        if(a>b)

                            System.out.println("Value of a="+a+ "is greater than b="+b );

                        else

                            System.out.println("Value of b="+b+ "is greater than a="+a);

                        }

            }

 

Output of the above program is:

Value of b=8 is greater than a=4


Was it useful?  



Using if statements

 Many times, we would like to execute some code statement only if some condition holds true. In java, we can achieve this using the if statement. Consider the following program:

OUTPUT:

Above, for loop executes 5 times with varying from 1 to 5. However, the display is generated only for even numbers.

 

Basic Anatomy of the if statement

The anatomy of the if statement is as follows:

 

The if...else Statement:

An if statement can be followed by an optional else statement, which executes when the Boolean expression is false.

Syntax:

The syntax of an if...else is:

if(Boolean_expression){

   //Executes when the Boolean expression is true

}else{

   //Executes when the Boolean expression is false

}

Example:

public class Test{

   public static void main(String args[]){

      int x =30;

      if( x <20){

         System.out.print("This is if statement");

      }else{

         System.out.print("This is else statement");

      }

   }

}

This would produce the following result:

 

 

The if...else if...Statement:

Many times we want to check for multiple conditions. If condition 1 is satisfied do a certain block of code statements, else if condition 2 is satisfied do a second block of code statements, else if condition 3 is satisfied do another block of statements, and so on. We can extend the if statement with an else if.

To understand this, consider the following program:

class IfElseIf{
public static void main(String[] args)
{
int i;
int remainder;
for(i = 1; i <=5 ; i++)
{
remainder = i % 2;
if(remainder == 0)
{
System.out.print(i);
System.out.println(" is even");
}
else if (remainder == 1)
{
System.out.print(i);
System.out.println(" is odd");
}
}
}
}

Output:

 

The anatomy of the extended if …else  if statement

There is no practical limit on the number of else if statements. You may have 0,1, or more than one.

Example:

class Ex0510{
public static void main(String [] args)
{
int i;
int remainder;
for ( i = 1; i <=5; i++)
{
remainder = i % 3;
if (remainder == 0)
{
System.out.println("Remainder is 0");
}
else if(remainder == 1)
{
System.out.println("Remainder is 1");
}
else if (remainder == 2)
{
System.out.println("Remainder is 2");
}
}
}
}

 

OUTPUT:

 

Using if … else … else if … else statements

The final extension of the if statement uses if, else if, and a final else as well. The final else is used to catch any condition not considered in the if and else ifconditions.

An if statement can have one if, zero or more else if, and zero or one else clauses.

 

EXAMPLE:

class Ex0511{
public static void main(String [] args)
{
int i;
int remainder;
for(i=1; i <=5; i++)
{
remainder = i % 5;
if (remainder == 0)
{
System.out.println("Remainder is 0");
}
else if (remainder == 1)
{
System.out.println("Remainder is 1");
}
else if (remainder == 2)
{
System.out.println("Remainder is 2");
}
else if (remainder > 2)
{
System.out.println("Remainder > 2");
}
}
}
}

 

OUTPUT:

 

Are the {} required in if statements?

In java, the curly braces are used whenever we need to group together more than one statement. Consider the following program:

class Ex0512{
public static void main(String [] args)
{
int time = 17;
if (time < 12)
System.out.print("Good");
System.out.print("Morning");
}
}

 

OUTPUT: 

 

The output above is not correct. If the time is before 12, we want both Good and Morning to be displayed. If the time > = 12,nothing should be  displayed. What has happened above?

Since the time is >=12, the condition in the if statement evaluates to a false. The next statement is not executed. The statement after that gets executed. If we want to put more than one statement to be executed/not-executed, we need to put these statements inside the curly braces {} as shown below:

class Ex0514{
public static void main(String[] args)
{
int i;
for(i = 1; i <=3; i ++)
System.out.println(i);
System.out.println("The end");
}
}

 

OUTPUT:

Good programming practice suggests you use the curly braces {} even if you have single statement. This can prevent logic errors should you modify your program later on.

 


Was it useful?  




Please login to reply to this problem.



Related Problems on Fixoncloud