0% found this document useful (0 votes)
22 views

Branching and Looping

The document discusses different types of control flow statements in Java including selection statements like if/else and switch statements, the ternary operator, and looping statements like while, do-while, and for loops. It provides syntax and examples for each type of statement.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Branching and Looping

The document discusses different types of control flow statements in Java including selection statements like if/else and switch statements, the ternary operator, and looping statements like while, do-while, and for loops. It provides syntax and examples for each type of statement.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

DECISION MAKING AND BRANCHING

I.Introduction:
A Java program is a set of statements, which are normally executed
sequentially in the order in which they appear. A programming language uses a control statement
to cause the flow of execution of advance and branch based on the changes to the state of the
program. JAVA program controls the statement can be put into following categories
1.Selection statement or Decision making and branching statements
2.Iteration statements or Decision making and looping statements
3.Jump statement

II.Decision making with if statement:


The if statement is a powerful decision making statement and is used to control
the flow of execution of statement. It is basically a two-way decision statement and is used in
conjunction with an expression.
The if statement may be implemented in different forms depending on the
complexity of condition to be tested.
1. simple if statement
2. if….else statement
3. Nested if….else statement
4. else if ladder

1.Simple if statement
The general form of a simple if statement is
Syntax: if(test expression)
{
Statement-block;
}
Statement-x;

Here the ‘statement-block’ may be a single statement or a group of


statements. If the test expression is true the statement-block will be executed; otherwise the
statement-block will be skipped and the execution will jump to the statement-x

Example:
class Simpleif
{
public static void main(String args[])
{
int marks=45;
if(marks>=35)
System.out.println("pass");
}
}

2.if….else statement
The if….else statement is an extension of the simple if statement .The
general form is
Syntax: if (test expression)
{
True-block statement(s)
}
else
{
False-block statement(s)
}
Statement-x
If the test expression is true then the true-block statement(s) are executed; otherwise the
false-block statement(s) are executed. In both the cases the control is transferred subsequently to
the statement-x.
Example:
class Ifelse
{
public static void main(String args[])
{
int marks=45;
if(marks>=35)
System.out.println("pass");
else
System.out.println("fail");
}
}

3.Nested if…..Else statement


When a series of decisions are involved we may have to use more than one
if….else statement in nested form as follows;

Syntax: if (test condition1)


{
if(test condition2)
{
Statement-1;
}
else
{
Statement-2;
}
}
else
{
Statement-3;
}
Statement-x;

If the condition-1 is false the statement-3 will be executed; otherwise it continues to


perform the second test. If the condition-2 true the statement-1 will be evaluated; otherwise the
statement-2 will be evaluated and then the control is transferred to the statement-x;
Example:
class Nestedif
{
public static void main(String args[])
{
int a=5,b=4,c=7;
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");
}
}
}

4.else if ladder
There is another way of putting ifs together when multipath decisions are
involved. A multipath decision is a chain of ifs in which the statement associated with each else
is an if. It takes the following general form

Syntax: if (condition1)
statement-1;
else if(condition2)
statement-2
else if(condition3)
statement-3
………………………………..
else if(condition n)
statement-n;
else
default-statement;

statement-x;

This condition are evaluated from the top( of the ladder) downwards. As soon as
the true condition is found the statement associated with it is executed and the control is
transferred to the statement-x(skipping the rest of the ladder). When all the n conditions become
false then the final else containing the default-statement will be executed the logic of execution
of else if ladder statements.
Example:
class Elseif
{
public static void main(String args[])
{
int a=5,b=4,c=7;
if((a>b)&&(a>c))
System.out.println("a is big");
else if((b>a)&&(b>c)
System.out.println("b is big");
else
System.out.println"c is big");
}
}

III.The switch Statement:


We have seen that when one of the many alternatives is to be selected we can design a
program using if statements to control the selection. Java has 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.
Syntax: switch (expression)
{
case value-1;
block-1;
break;
case value-2
block-2;
break;
………
………
default:
default-block
break;
}
Statement-x;

The expression is and integer expression or characters. Value-1, value-2……. Are


constants or constant expressions (evaluable to an integral constant) and are known as case
labels. Each of these values should be unique within a switch statement. Block-1,block-2…. are
statement lists and may contain zero or more statements. There is no need to put braces around
these blocks but it is important to note that case labels end with a colon(: ). When the switch is
executed the value of the expression is successively compared against the values value-1, value-
2…. If a case is found whose value matches with the value of the expression then the block of
statements that follows the case are executed.

Example
class Switchex
{
public static void main (String args[])
{
int index,marks=512;
String grade;
index=marks/6;
switch(index)
{
case 1:grade=”Honors “;
break;
case 2: grade=”first division”;
break;
case 3: grade=”second division”;
beak;
case 4:grade=”third division”;
break;
default:
grade=”fail”;
break;
}
System.out.println(grade);
}
}

IV.The ?: Operator:
The java language has an unusual operator useful for making two-way decisions.
This operator is a combination of ? and : and takes three operands. This operator is popularly
known as the conditional operator. The general form of use of the conditional operator is as
follows

Syntax: conditional expression ? expression1 : expression2

Example:
class TernaryEx
{
public static void main(String args[])
{
int a=5,b=10,big;
big = (a>b)?a:b;
System.out.println("\n Big = "+big);
}
}

The conditional expression is evaluated first. If the result is true expression1 is


evaluated and is returned as the value of the conditional expression. Otherwise expression2 is
evaluated and its value is returned

V.Loopings or Iteration statements or Decision making and looping


statements:
The process of repeatedly executing a block of statements is known as looping.
In loops, a sequence of statements are executed until some condition for the termination of loop
is satisfied.A loop process in general will include the following four steps.
1.Setting and initializing the counter
2.Test for a specified condition for execution of the loop
3.Execution of statements in the loop
4.Increment/decrementing the counter
The java language provides three constructs for performing loop operations.They are
1.While loop
2.do-while loop
3.for loop

1.The while statement:-


The while is an entry-controlled loop statement. The general form is

Syntax: Initialization;
while(test condition)
{
Body of the loop
}
Here condition can be any Boolean expression. The body of the loop will be executed
as long as the conditional expression is true. When condition becomes false, the control passes to
the next line of code immediately following the loop. The braces { } are optional, if it contains
only one statement is being repeated.

Example:
class ExOnWhile
{
public static void main(String args[])
{
int n=1;
while(n<=10)
{
System.out.println(n);
n++;
}
}
}

2.The do-while statement:-


The do-while is an exit controlled loop. The do-while loop allows to execute
statements repeatedly for a specific number of times. It executes always at least once ,because it
is a conditional expression will be at the bottom of the loop. The general form is

Syntax: Initialization;
do
{
Body of the loop;
}while(test condition);
Here condition can be any Boolean expression. The body of the loop will be executed
as long as the conditional expression is true. When condition becomes false, the control passes to
the next line of code immediately following the loop. The braces { } are optional, if it contains
only one statement is being repeated.

Example:
class ExOnDo
{
public static void main(String args[])
{
int n=1;
do
{
System.out.println(n);
n++;
}while(n<=10);
}
}

3.The for statement:-


The for loop is another entry-controlled loop. The general form is

Syntax: for( initialization; condition; increment/decrement)


{
Body of the loop
}

The execution of the for statement is


1. Initialization of the control variables is done first using assignment statements such as i=1
and count=0. the variables i and count are known as loop-control variables.
2. The value of the control variable is tested using the test condition. The test condition is a
relational expression such as i<=10 that determines when the loop will exit. If the
condition is true the body of the loop is executed ; otherwise the loop is terminated and
the execution continues with the statement that immediately follows the loop.
3. When the body of the loop is executed the control is transferred back to the for statement.
Now the control variable is incremented or decremented using an assignment statement
such as i++ or i=i+1 and the new value of the control variable is again tested to see
whether it satisfies the test condition. If the condition is satisfied the body of the loop is
executed again. This process continues till the value of the variable fails to satisfy the test
condition.

Example: class ForEx


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

Features of for loop:


i.More than one variable can be initialized at a time in the for statement separated by a comma.
for(i=1,j=1;i<=10;i++)
ii.The test condition may have any compound relation and the testing need not be limited only to
the loop control variable.
for(i=1,j=1;i<=10 || sum<=100;i++)
{
…………..
…………..
}
iii.The increment section may also have more than one value separated by a comma.
for(i=1,j=1;i<=10 && j<=10; i++,j++)

VI-Jumps in loops Jump statements:


Java supports three jump statements.They are
1.break
2.continue
3.return
These statements transfers control to the another part of the program.
1.break:
In java the break statement has the following uses.
i.It terminates a statement sequence in the switch statement.
ii.It can be used to exit a loop.
When the break statement is encountered inside a loop.The loop is immediately exists
and the program continues with the statement immediately following the loop.
Example:
import java.io.*;
class Breakdemo
{
public static void main(String args[])throws IOException
{
int a=1,sum=0;
while(a<=10)
{
if(a==5)
break;
else
sum=sum+a;
a++;
}
System.out.println("sum="+sum);
}
2.continue:
Some times it is useful to any early iteration of a loop.Break exists the loop without
executing the rest of the statements.But the continue stops the execution and goes back to the
beginning of the loop to begin a new iteration.The continue statement tell the compiler to skip
the following statements and continue with next iteration.
Example:
Example:
import java.io.*;
class Continuedemo
{
public static void main(String args[])throws IOException
{
int a=1,sum=0;
while(a<=10)
{
if(a==5)
continue;
else
sum=sum+a;
a++;
}
System.out.println("sum="+sum);
}

3.Return statement:
The return statement is used to explicitly return from a method i.e it causes program
control to transfer back to the caller of the method. The return statement immediately terminates
the method in which it is executed.

You might also like