Decision Making Statement In Java

When a program breaks the sequential flow and jumps to another part of the code, it is called selection or branching.

When the branching is based on a particular condition, it is known as conditional branching.

If branching takes place without any condition, it is known as unconditional branching.

 

DECISION MAKING WITH IF STATEMENT:

An if statement tests a particular condition; if the condition evaluates to true , a course of action is followed i.e a statement or set of statements is executed . Otherwise (if the condition evaluates to false) ,the course-of-action is ignored.

The if statement may be implemented in different forms depending on the complexity of conditions to be tested.

  • Simple if statement
  • if….else statement
  • Nested if….else statement
  • else if ladder

 IF STATEMENT:

The syntax of the if statement is shown below:

if(expression)

statements;

 

Where a statement may consist of a single statement , a compound statement, or nothing. The expression must be enclosed in paranthesis. If the expression evaluates to true, the statement is executed, otherwise ignored.

Flow Chart Is given As Below:-

 

 

 

For example:-

a=20;

b=10;

if(a>b)

{

           System.out.println(“Largest Value is=”+a);

}

If the given condition is true,then the computer will print the message Largest value is=20 and if not,it will simply skip this statement.

 

IF….ELSE STATEMENT:

This form of if allows either-or condition by providing an else condition block. The syntax of the if-else statement is the following:

 

if(expression)

{

  statement-1;

}

else

{

  statement-2;

}

 

If the expression evaluates to true i.e., a non-zero value, the statement-1 is executed, otherwise statement-2 is executed. The statement-1 and statement-2 can be a single statement, or a compound statement, or a null statement.

 

This statement works in the following manner:-

 

           

The example is given as below:-

……………….

……………….

if(code==1)

boy=boy+1;

else

girl=girl+1;

X X X X X

……………

In this example,if the code is equal to 1,the statement boy=boy+1; is executed and the control is transferred to the statement xxxxx,after skipping the else part.if the code is not equal to 1,the statement boy=boy+1; is skipped and the statement in the else part girl=girl+1; is executed before the control reached the statement xxxxx.

 

NESTED IF….ELSE STATEMENT:

 

A Nested if is an if that has another if in it’s ‘if’s’ body or in it’s else’s body. The nested if can have one of the

following three forms:

1.    if(expression1)

        {

          if(expression2)

           statement-1;

         else

           statement-2;

        }

      else

         body of else;

 

2.     if(expression1)

        body-of-if;

       else 

       {

          if(expression2)

           statement-1;

         else

           statement-2;

        }

 

3.      if(expression1)

        {

          if(expression2)

           statement-1;

         else

           statement-2;

        }

      else

     {

       if(expression3)

           statement-3;

      else

         statement-4;

 } 

Program For Nesting If-else Statements:-

class Ifelsetesting

{

        public static void main(String args[])

        {

              int a=325,b=712,c=478;

              System.out.println(“Largest value is:”);

if(a>b)

{

if(a>c)

{

     System.out.println(a);

}

else

{

System.out.println(c);

}

}

else

{

if(c>b)

{

System.out.println(c);

}

else

{

System.out.println(b);

}

}

}

}

 

 

THE ELSE-IF LADDER:

A common programming construct in the java is the if-else-if ladder , which is often also called the if-else-if staircase because of it’s appearence.

 

It takes the following general form:

if(expression1) 

        statement1;

  else

      if(expression2) 

          statement2;

  else 

      if(expression3) 

          statement3;

   :

   else 

          statement n;

 

The expressions are evaluated from top downward. As soon as expression evaluates to true , the statement associated with it is executed and the rest of the ladder is bypassed. If none of the expression are true, the final else gets executed. If the final else is missing , no action takes place if all other conditions are false.

 

            

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s