Control statements

Conditional control statements in java

Conditional control or decision making is done in java with if statements and switch statement.

Simple if()

Ensures that a statement is executed only when a condition is true.  Conditions typically involve comparison of variables or quantities for equality or inequality

Syntax

if(condition)  

{  

    statement;  

}  

statement;  


Example:

 if (age >= 18)

System. out.println(“You are eligible to vote.”);

if ... else()

This is two way branching statement  and is an extension of if statement and informs what to do if the condition evaluates to false.

Syntax

if(condition)  

{  

    statement;  

}  

else  

{                         

    statement;  

}  

Example :

if(a>b)

System.out.println("A is big");

else

System.out.println("B is big");

Nested if()

When if ..else statement is placed inside another if..else statement then it is called nested if…else statement. This is used for making multiway decisions

Syntax

      if(condition)  

{  

    if(condition)  

    {  

        statement;  

    }  

    else  

    {  

        statement;  

    }  

}  

else  

{  

    statement;  

}  

Example:

if(a>b)

{

if (a>c)

System.out.println("A is big");

else

System.out.println("C is big");

}

else

{

if (b>c)

System.out.println("B is big");

else

System.out.println("C is big")

else ... if ladder

The <condition1> is evaluated first. If it evaluates to true, then /* statements 1 */ are executed. The rest of the if-else-if construct is ignored.

 If <condition 1> is false, then <condition 2> is checked; if <condition 2> is false, <condition 3> is checked.

This process continues until a condition evaluates to true. At this point, the statements following the associated if or else-if are executed.

If none of the conditions evaluate to true, if there is a final else clause, those statements are executed. Otherwise, execution continues with the statements after the if-else-if construct.

If the clauses for any of the conditions is more than one statement, than curly brackets are necessary. Otherwise, the brackets are optional.


Syntax

if(condition)  

{  

   statement;  

}  

else if(condition)  

{  

   statement;  

}  

else  

{  

   statement;  

}  

statement;  

Example:    

if(avg>79)

System.out.println("Distinction");

else if(avg>59)

System.out.println("First Class");

else if(avg>39)

System.out.println("Second Class");

else

System.out.println("Fail");




Using switch 

switch case is used to check a number of possible execution paths. A switch can work with the byte, short, char and int primitive data types. 


Java has a built in multi-way design statement called switch 

The expression inside the switch case may be an integer or a character. 

Since JDK 1.7 we have facility to take an String inside the switch case. 

Each case must be end with colon (:) 

The default statement is executed if there is no match with any case specified in the switch case. 

Each case must be terminated with a break; statement. 

Syntax

switch(expression)  

{  

    case value1: statement();  

    break;  

    case value2: statement();  

    break;  

    case value3: statement();  

    break;  

    default:  statement();  

    break;  

}  


Example:

switch (day)

{

case 1:

System.out.println("Monday");

break;

case 2:

System.out.println("Tuesday");

break;

case 3:

System.out.println("Wednesday");

break;

case 4:

System.out.println("Thrusday");

break;

case 5:

System.out.println("Friday");

break;

case 6:

System.out.println("Saturday");

break;

case 7:

System.out.println("Sunday");

break;

default:

System.out.println("Invalid entry");

}

Comments

Popular posts from this blog

History of C language

Lab programs of C

Algorithm Pseudopods Flow-chat