New Delhi, India
minigranth@gmail.com

Control Statements In Java

Created with Sketch.

Core Java Tutorial

[fblike]

Control Statements in Java : Introduction

  • Control statements in java are used to alter or change the sequence of program based on some conditions.
  • These control statements in java are the basics and are present in every high level programming or scripting language and also used heavily to alter the sequence of program.
  • Major portion of programs/code written in java have a high density of control statements within them because of their functionality. Different control statements can be used according to different logic or situations or requirements.
  • There are four major control statements in java namely:
    • The If Statement.
    • The If-Else Statement.
    • The If-Else-If Statement/Ladder.
    • The Switch Statement.

 

Control Statements In Java : The Discussion

1) If statement

  • If control statements in java is used to test a condition and it checks for Boolean true or false. In case the condition is true, “If” block is executed.

 

Syntax

 if(condition)

{

            //statements;

}

 

Example

 

This image describes the sample program for if control statements in java.

If Statement : Example

 

Output

 

This image describes the output of sample program for if control statements in java.

If Statement : Output

 

2) If-else Statement

  • If-else control statements in java is also used to test the conditions. But, in case the condition matches, “If block statements are executed otherwise “else” block statements are executed.

 

Syntax

if(condition)

{

            statements; //if condition is true

}

else

{

            statements; //if condition is false

}

 

Example

 

This image describes the sample program for if else control statements in java.

If-Else Statement : Example

 

Output

 

This image describes the output of sample program for if else control statements in java.

If-Else Statement : Output

 

3) If-else-if  ladder statement

  • This is an interesting one. If-else-if ladder control statements in java is used to test multiple conditions each having its own block of statements to be executed.

 

Syntax

if(1st condition)

{

          // statements;

}

else if(2nd condition)

{

           // statements;

}

..

..

..

else

{

            //statements;

}

 

Example

 

This image describes the sample program for if else if control statements in java.

If-Else-If Statement : Example

 

Output

 

This image describes the output of sample program for if else if control statements in java.

If-Else-If Statement : Output

 

4) Switch Statement

  • Switch is a block which is used to match a variable against a set of cases.
  • If a case matches the statements corresponding to the case executes.

 

Syntax

switch(variable)

{

            case label1:

                        //statements;

            case label2:

                        //statements;

            ..

            ..

            ..

            default:

                        //statement to be executed if no case are matched;

}

 

Control Statements In Java : If-else and Switch Difference

  • Switch statement is a fall-through while if-else is not.
  • Fall-through means that in switch if one case matches the all other cases following the matched case gets executed. There is no such thing in if-else .
  • To prevent fall-through we put break keyword after every case.

 

Syntax with break keyword

switch(variable)

{

            case label1:

                        //statement;

            break;

            case label2:

                        //statement;

            break;

            ..

            ..

            default:

                        //statement to be executed if no case matched;

}

 

Example

 

This image describes the sample program for switch control statements in java.

Switch Statement : Example

 

Output

 

This image describes the output of sample program for switch control statements in java.

Switch Statement : Output