Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
16 views

Ch6 Java DecisionMakingAndBranching SMS

The document discusses different types of control flow and decision-making statements in Java including if, if-else, nested if-else, switch, and the conditional operator. It provides examples and flowcharts to illustrate how each statement works and how they can be used to direct program execution based on different conditions. Key statements covered are simple if, if-else, nested if-else, else-if ladder, switch, and the conditional operator.

Uploaded by

RUDRAKSHI SAWANT
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Ch6 Java DecisionMakingAndBranching SMS

The document discusses different types of control flow and decision-making statements in Java including if, if-else, nested if-else, switch, and the conditional operator. It provides examples and flowcharts to illustrate how each statement works and how they can be used to direct program execution based on different conditions. Key statements covered are simple if, if-else, nested if-else, else-if ladder, switch, and the conditional operator.

Uploaded by

RUDRAKSHI SAWANT
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Chapter 6

Decision Making and


Branching
Introduction

• When a program breaks the sequential flow and jumps to another part of
the code, it is called as branching.
• Java Supports following control or decision making statement (branching).
- if statement
- switch statement
- Conditional operator statement
• if statements:
1) simple if statement 2) if .. else statement
3) Nested if .. else statement 4) else if ladder
Simple if statement
if (test expression) Entry
{
statement-block; Test True
} expression
?
statement-x;

False Statement-block

//Program: IfTest.java
Statement-x

Next statement
The if .. else statement
if (test expression) Entry
{
True False
True-block statement(s) Test
expression
} ?
else
True block False block
{ statement(s) Statement(s)
False-block statement(s)
} Statement-x
Statement-x;
// Program: IfElseTest.java
Nested if .. else statement
if (test condition1)
{ if (test condition2)
{ statement-1;
}
else
{ statement-2;
}
}
else
{ statement-3;
}
statement-x;
Nested if .. else statement
// Program: IfElseNesting.java
Entry

False Test True


Condition1
?

False Test True


Statement-3 Condition2
?

Statement-2 Statement-1

Statement-x
Next statement
The else if ladder
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;
Entry
Flowchart of else .. If ladder
Test
True False
Condition1
?
// Program: ElseIfLadder.java
Test
Statement-1 True False
Condition2
?

Test
Statement-2 True False
Condition3
?

Test
Statement-3 True False
Condition
n
?
Default
Statement-n
Statement

Statement-x
The switch statement
switch (expression)
{ case value-1:
block-1;
break;
case value-2:
block-2;
break;
……….
……….
default:
default-block;
break;
}
statement-x;
Entry Selection process of the switch statement

Switch
expression //Program: CityGuide.java

expression =
. . . value-1
. . . Block-1
expression =
. . .
value-2
. . . Block-2
…………………………….
…………………………….
(No match) default
default block

statement-x
The ? : Operator
Syntax: conditional expression ? expression1 : expression2
• E.g.
If ( x < 0) if ( a < b )
flag = 0; c = a + b;
else else
flag = 1; c = a – b;
Can be written as Can be written as
flag = (x < 0) ? 0 : 1; c = ( a < b ) ? a + b : a - b;
The ? : Operator
Another Example:
{ 4x + 100 for x < 40
salary = { 300 for x = 40
{ 4.5x + 150 for x > 40
Using Conditional Operator:
salary = (x!=40) ? ((x<40) ? (4x+100) : (4.5x+150)) : 300;
Using if .. else statement:
if (x <= 40)
if (x < 40)
salary = 4*x + 100; //Program: CondExpression.java
else
salary = 300;
else
salary = 4.5*x + 150;

You might also like