Branching and Looping
Branching and Looping
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
1.Simple if statement
The general form of a simple if statement is
Syntax: if(test expression)
{
Statement-block;
}
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");
}
}
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");
}
}
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
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);
}
}
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++;
}
}
}
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.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.