Write about Decision making and Branching statements in Java with Examples?


Decision making and Branching statements:   

A java program is a set of statements which are normally executed sequentially same in the order in which they appear.

When a program breaks the sequential flow and jumps to another part of the code, it is called branching. When the branching is based on a particular condition it is known as conditional branching. If branching takes place without any decision it is known as unconditional branching.

Decision making statements are

1. If

2. switch

3. conditional operator statement

Decision making with if statement :

The if statement if a powerful decision making statement and in used to control the flow of execution of statements. It is basically a two way decision statements .

if(test expression)

it allows the computer to evaluate the expression first and then depending on whether the value of the expression is true or false, it transfers the control to a particular statement.

for eg:

if (bank balance is zero)
borrow money;

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 statement

® else if ladder

Simple if statement:

The general form of a simple if statement is:

if(test expression)
{
Statement–block;
}
Statement x;

The statement block may be a single statement (or) a group of statement.

If the text expression is true the statement block will be executed, otherwise the statement block will be stopped and the execution will jump to the statement x.

Eg  :

if ( category==sports)
{
marks=marks+bonus;
}
System.out.println (“marks”);

If else statement:

 The if else statement is an extension of the simple if statement.
The general form is

if(test expression)
{
True–block statement;
}
else
{
False–block statement;
}
Statement–x;

If the test expression is true then the true block statements immediately following the if statement are executed otherwise the false block statements are executed. In either case true block (or) false block will be executed not both.

In both the cases the control is transferred subsequently for the statement x.

Eg:

if (code==1)
boy=boy+1;
else
girl=girl+1;

// count the even & odd number in a list.
Eg:

class Test
{
 public static void main (String arg[ ])
{
int number[ ]={50,65,80,15,23,18};
int even=0, odd=0;
for(int i=0; i
{
if(number[i] % 2==0)
{
even=even+1;
}
else
{
odd=odd+1;
}
}
System.out.println (“even numbers”+even+”odd numbers”+odd);
}
}

Nested if else statement:

When a series of decisions are involved we may have to use more than and if else statement in nested form as follows. 
          
if(test condition 1)
{
if(test condition 2)
{
Statement -1;
}
else
{
Statement -2;
}
}
else
{
Statement-3;
}
Statement-x;  

If the condition 1 is true stamen 3 will be executed otherwise it continuous to perform the second test. If the condition 2 is true the statement 1 will be executed otherwise the statement 2 will be evaluated and then the control is transferred to the statement x.   

class Largest
{
public static void main(String a[ ])
{
int a, b, c;
a=100;
b=150;
c=125;
if(a > b)
{
if (a > c)
System.out.println (“a is big”+a);
else
System.out. println(“c is big”+c);
}
else
{
if(b > c)
System.out.println (“b is big”+b);
else
System.out.println (“c is big”+c);
}
}
}

Else if ladder:

There is another way of putting if’s together when multipath decisions are involved. A multipath decision is a chain of its in which the statement associated with each else is on if. It takes the following general form.

if(condition 1)
Statement -1;
else if (condition 2)
Statement -2;
else if (condition 3)
Statement -3;
-------------
-------------
else if(condition n)
Statement-n;
else
Default statement;
Statement x;

The construct is known as the else if ladder. The conditions are evaluated from the top to down words as soon as the true condition is found. The stamen associated with it is executed and the control is transferred to the statement x. (skipping the rest of the ladder)  . When all the n controls become false then the final else contains the default statement will be executed.
 
class Sample
{
public static void main(String a[ ])
{
int roll_number [ ]={101, 102, 103, 104, 105};
int number [ ] ={85, 72, 35, 68, 50};
for(int i=0; i <  roll_number. Length ;i++)
{
if(marks[i] > 79)
System.out.println(rollnumber[i]+”distension”);
else if (marks[1] >> 59)
System.out.println (rollnumber[i]+”I class”);
else if(marks[i] > 49)
System.out.println (rollnumber[i]+”II class”);
else
System.out.println (rollnumber[i]+”fail”);
}
}
}

Switch :

Java is a built in multi-way decision statement known as switch. The switch statement tests the value of a given variable (or expression) against a list of case values and when a match is found a block of statements associated with that case is executed. The general form of the switch statement is as shown below.       

switch(expression)
{
case value-1:
Block-1;
break;
case value-2:
Block-2;
break;
--------------
-------------
case value-n:
Block-n;
break;
default:
Default statement;
}
Statement x;

The expression is an integer expression or characters. Value-1, value-2 and ….. are   constants(or) constant expressions and are known as case labels. Each of these values should be unique with in a switch statement , block-1, block-2 are statement lists and may contain 0  or more statements . there is no need to put braces around these blocks, but it is important to note that case levels and with a colon(:).

The break statement at the end of each block. End of particular case and causes on exist from the switch statement, transferring the control to the statement x, following the switch. 

The default is an optional case. When present it will be executed if the value of the expression doesn’t match with any of the case values.

Eg :

import java.io.*;
class Arthemtic
{
public static void main (String a[ ] ) throwsIOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
char choice;
int a=10, b=20, c;
System.out.println (“1:addition”);
System.out.println (“2:subtraction”);
System.out.println (“3:multiplication”);
System.out.println (“4:divison”);
System.out.println (“enter your choice”);
choice=br.read( );
switch(choice)
{
case 1: c=a+b;
System.out. println(“addition value is”+c);
break;
case 2:  c=a–b;
System.out.println (“sub value is “+c);
break;
case 3:  c=a*b;
System.out.println (“mul value is”+c);
break;
case 4:   c=a/b;
System.out.println (“division value is”+c);
break;
default:
System.out.println(“enter invalid choice”);
}
}
}

The ?: operator (conditional operator) :

This  operator is useful for making two way decisions.

This operator is a combination of :? and takes three operands. This operator is popularly known as the conditional operator.

The general form of  the conditional operator is as follows   :
          
Conditional expression ? exp 1 : exp 2 ;

The conditional expression is evaluated first if the result is true expression 1 is evaluated and it is the result of total statements. Otherwise expression 2 is evaluated and its value is returned.

For eg:

if(x < 0)
flag=0;
else
flag=1;

Can be written as flag=(x<0 0:1="" o:p="">




Related

Java 4218004723953638473

Post a Comment

emo-but-icon

item