Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

JAVA PROGRAMMING UNIT - II

DECISION MAKING AND BRANCHING:


 When a program breaks the sequential flow and jumps to another part of
the code is called branching.
 There are two types of branching. They are,
1. Conditional branching
2. Unconditional branching
1. Conditional branching:
When the branching is based on a particular condition called
conditional branching.
2. Unconditional branching:
When the branching is takes place without any decision is called
unconditional branching.
CONTROL (OR) DECISION MAKING STATEMENTS:
Java languages possess such decision making capabilities and supports the
following statement known as control or decision making statements.
 If statements
 Switch statements
 Conditional statements
If statements:
 If statement is powerful decision making statement.
 It is used to control the flow of execution of statements.
 This statement is classified into four types. They are,
1. Simple if Statement
2. If ... else Statement
3. Nesting of if ... else Statement
4. Else ... if Ladder
Simple if Statement:
 Syntax:
if (test-expression)
{
Statement-block;
}
Statement-x;
 Here, Statement-block denotes it may be single statement or group of
statements.
 If the test-expression is true, statement-block will be executed and
statement-x also executed.
 If test-expression is false, statement-block is skipped and statement-x is
executed. Entry
 Flowchart:
Test -expression? True

False Statement-block

Statement-x

Page 1 of 14
JAVA PROGRAMMING UNIT - II

 Sample Program:
class pgm
{
public static void main(String args[ ])
{
int a = 10, b = 20;
if (a > b)
{
System.out.println(‘’a is big’’);
}
System.out.println(‘’thanks’’);
}
}
 Output: thanks

if ... else statement:


 It is an extension of if statement.
 Syntax:
if (test-expression)
{
true-statements;
}
else
{
false-statements;
}
Statement-x;
 If the test-expression is true, true-statements will execute and
statement-x is executed.
 If test-expression is false, true-statements are skipped and
false-statements will execute.
 Flowchart:

entry

test -expression?
false true

false-statements true-statements

Statement-x

Page 2 of 14
JAVA PROGRAMMING UNIT - II

 Sample Program:
class pgm
{
public static void main(String args[ ])
{
int a = 10, b = 20;
if (a > b)
{
System.out.println(‘’a is big’’);
}
else
{
System.out.println(‘’b is big’’);
}
}
}
 Output: b is big

Nesting of if ... else statement:


 While using more than one if else statement, then it is called as nested if.
 Syntax:
if (test-condition1)
{
if (test-condition2)
{
Statement-block1;
}
else
{
Statement-block2;
}
}
else
{
Statement-block3;
}
Statement-x;

Page 3 of 14
JAVA PROGRAMMING UNIT - II

 Flowchart:

entry

test –condition1?
false true

Statement-block3 test –condition2?


false true

Statement-block2 Statement-block1

Statement-x
 Sample Program:
class pgm
{
public static void main(String args[ ])
{
int a = 10, b = 20, c = 30;
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’’);
}
}
}
 Output: c is big

Page 4 of 14
JAVA PROGRAMMING UNIT - II

The else ... if ladder:


 A multipath decision is a chain of ‘’ifs’’ in which the statement associated
with each ‘’else” is an ‘’if’’.
 Syntax:
if (test-condition1)
Statement1;
else if (test-condition2)
Statement2;
else if (test-condition3)
Statement3;
else
Statement4;
Statement-x;

 This construction is known as else ... if ladder.


 The test-condition is executed from top to downwards.
 Flowchart:
entry

test –condition1?
false true

test –condition2? Statement1


false true

test –condition3? Statement2


false true

Statement4 Statement3

Statement-x

Page 5 of 14
JAVA PROGRAMMING UNIT - II

 Sample Program:
class pgm
{
public static void main(String args[ ])
{
float avg = 72.8;
if (avg > 75)
System.out.println(“Distinction”);
else if (avg > 60)
System.out.println(“First Class”);
else if (avg > 50)
System.out.println(“Second Class”);
else
System.out.println(“Third Class”);
}
}

 Output: First Class

Switch Statement:
 Java has built in multi-way decision statement known as switch
statement.
 It tests the value of given variable against a list of case values and when a
match is found, block of statements associated with the case is executed.
 Syntax:
switch (test-expression)
{
case value1: block1;
break;
case value2: block2;
break;
case value3: block3;
break;
.
.
.
default: default-block;
break;
}

Here, default is optional.

Page 6 of 14
JAVA PROGRAMMING UNIT - II

 Flowchart:

entry

test -expression?

value1
block1

value2
block2

value3
block3
.
.
.
default
default-block

 Sample Program:
class pgm
{
public static void main(String args[ ]) throws IOException
{
char choice;
DataInputStream in=new DataInputStream(System.in);
choice = char.parseChar(in.readLine( ));
switch (choice)
{
case ’M’:
System.out.println(“Madurai”);
break;
case ’C’:
System.out.println(“Chennai”);
break;
case ’B’:
System.out.println(“Bombay”);
break;
default:
System.out.println(“Invalid Choice”);
break;
}
}
}

Page 7 of 14
JAVA PROGRAMMING UNIT - II

Conditional Operators:
The character pair ? : is a Ternary Operator and also called as the
conditional operator. This operator is useful for making two-way decisions.
Syntax: exp1 ? exp2 : exp3
Here, exp1 is evaluated first.
If exp1 = true then exp2 is evaluated
If exp1 = false then exp3 is evaluated
For example,
import java.io.*;
class condop
{
public static void main(String args[ ])
{
int a = 10, b = 5, c;
c = (a > b) ? a : b;
System.out.println(“ a = ”+a);
System.out.println(“ b = ”+b);
System.out.println(“ c = ”+c);
}
}
It will display,
a = 10
b=5
c = 10

DECISION MAKING AND LOOPING:


 Looping is a process of repeatedly executing a block of statements.
 The statements in the block may be executed any number of times, from
zero to infinitive numbers. If a loop continues forever, it is called an
infinite loop.
 It consists of two segments.
1. Body of the loop
2. Control statement
 Body of the loop
 The statements between the opening and closing brace are called
body of the loop.
 Control statement
 It tests the certain conditions and then directs the repeated
execution of statements contained in the body of the loop.
 Depending upon the loop, it is classified either as
1. Entry-controlled loop or
2. Exit-controlled loop
1. Entry-controlled loop
 The control conditions are tested before the start at the loop
execution.

Page 8 of 14
JAVA PROGRAMMING UNIT - II

 If conditions are not satisfied then the body of the loop will not be
executed.
2. Exit-controlled loop
 The test is performing at the end of the body of the loop.
 Therefore the body is executed unconditionally for first time.
Looping process:
 Looping process involves the following steps are:
1. Setting and initialization of a counter.
2. Execution of the statements in the loop.
3. Tests for a specified condition for execution of the loop.
4. Incrementing the counter.
 Java provides three constructs for performing loop operations. They are,
1. The while statement
2. The do statement
3. The for statement
1. WHILE STATEMENT:
 While is the entry-controlled loop statement.
 First test-condition is evaluated.
 If the condition is true, the body of the loop is executed.
 After execution of body, the test-condition is once again evaluated
and if it is true, the body is executed once again.
 This process of repeated execution of the body continues until the
test-condition finally becomes false.
 Then the control is transferred out of the loop.
 The while loop makes a test condition before the loop is executed.
Therefore, the body of the loop may not be executed at all if the
condition is not satisfied at the very first attempt.
 Syntax:
while (test-condition)
{
body of the loop
}
 Flowchart:
entry

test-condition
false

true

body of the loop

Page 9 of 14
JAVA PROGRAMMING UNIT - II

 Sample Program:
class pgm
{
public static void main(String args[ ])
{
int n=1, sum=0;
while (n <= 5)
{
sum = sum + n;
n = n + 1;
}
System.out.println(“Sum = “+sum);
}
}

 Output: Sum = 15

2. DO STATEMENT:
 The do statement is the exit-controlled loop statement.
 On reaching the do statement, the program proceeds to execute the
body of the loop before the test is performed.
 At the end of the loop, the test condition in the while statement is
evaluated.
 If the condition is true, the program continues to execute the body of
the loop once again.
 This process continues as long as the condition is true.
 When the condition becomes false, the loop will be terminated and
the control goes to the statement that appears immediately after the
while statement.
 Since the test condition is evaluated at the bottom of the loop. So
the body of the loop is always executed at least once.
 Syntax:
do
{
body of the loop
}
while (test-condition);

Page 10 of 14
JAVA PROGRAMMING UNIT - II

 Flowchart:
entry

body of the loop

test-condition
true

false

 Sample Program:
class pgm
{
public static void main(String args[ ])
{
int n=1, sum=0;
do
{
sum = sum + n;
n = n + 1;
}
while (n <= 5);
System.out.println(“Sum = “+sum);
}
}
 Output: Sum = 15
3. FOR STATEMENT:
 For loop is entry controlled loop that provides a more concise loop
control structure.
 It has three segments. They are,
1. Initialisation
2. Test-condition
3. Increment/decrement
 Initialisation of control variables is done first using assignment
statement which is called as loop-control variable.
 The value of the control variable is tested using the test condition. If
the condition is true, the body of the loop is executed, otherwise the
loop is terminated.
 When the body of the loop is executed, the control is transfer back to
execute the increment or decrement segment.

Page 11 of 14
JAVA PROGRAMMING UNIT - II

 Syntax:
for(initialisation; test-condition; increment/decrement)
{
body of the loop
}
 More than one variable can be initialized at a time which is
separated by commas.
 Like the initialization segment, the increment/decrement also have
two parts or more.
 Sample program:
class pgm
{
public static void main(String args[ ])
{
int sum=0, n;
for (n=1; n<= 5; n++)
{
sum = sum + n;
}
System.out.println(“Sum = “+sum);
}
}
 Output: Sum = 15
Nesting of for loop:
 Nesting of loops that are one for statement within another for
statement.
 It contains inner loop and outer loop.
 Syntax:
for(initialisation; test-condition; increment/decrement)
{
-------------
for(initialisation; test-condition; increment/decrement)
{
------------
------------
}
}

Jumping out of a loop:


 An early exit from a loop can be accomplished by using the break
statement.
 The break statement can be also used within
 Switch
 While
 Do

Page 12 of 14
JAVA PROGRAMMING UNIT - II

 For loop
 When the break statement is encountered inside a loop, the loop is
immediately exited and the program continuous with the statement
immediately following the loop.
 When the loops are noted, the break would only exit from the loop
containing it that is the break will exit only a single loop.
 For example,
while(………) do for(………..)
{ { {
______________ ______________ ______________
______________ ______________ ______________
If (condition) If (condition) If (condition)
break; break; break;
Exit ______________ Exit ______________ Exit ______________
from ______________ from ______________ from ______________
loop } loop } while (………..) loop }
______________ ______________ ______________
Skipping a part of a loop:
 During the loop operations, it may be necessary to skip a part of the body
of the loop under certain conditions.
 Like the break statement, JAVA supports another similar statement called
the continue statement.
 The break statement causes the loop to be terminated
 The continue statement causes the loop to be continued with the next
iteration after skipping any statements in between.
 Syntax: continue;
 For example,
while(………) do for(………..)
{ { {
______________ ______________ ______________
______________ ______________ ______________
If (condition) If (condition) If (condition)
continue; continue; continue;
______________ ______________ ______________
______________ ______________ ______________
} } while (………..) }
______________ ______________ ______________
 In while and do loops, continue causes the control to go directly to the test
condition and then to continue the iteration process.
 In for loop, the increment section of the loop is executed before the test
condition is evaluated.
 Sample Program:
class continuebreak
{
public static void main(String args[ ])

Page 13 of 14
JAVA PROGRAMMING UNIT - II

{
int n, m;
LOOP1:
for (n=1; n<= 100; n++)
{
System.out.println(“ “);
if (n >= 10)
break;
for(m=1; m<=100; m++)
{
System.out.println(“ * “);
if (m == n)
Continue LOOP1;
}
}
System.out.println(“Termination by Break”);
}
}
Output:
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
Termination by Break

Page 14 of 14

You might also like