SlideShare a Scribd company logo
Computer
Programming 2
Lesson 9– Java – Decision Making
Prepared by: Analyn G. Regaton
DECISION
MAKING
Decision making structures have one
or more conditions to be evaluated
or tested by the program, along with
a statement or statements that are to
be executed if the condition is
determined to be true, and
optionally, other statements to be
executed if the condition is
determined to be false.
TYPESOF
DECISION
MAKING
STATEMENT
Sr.No. Statement & Description
1 if statement
An if statement consists of a boolean expression followed by one or
more statements.
2 if...else statement
An if statement can be followed by an optional else statement, which
executes when the boolean expression is false.
3 nested if statement
You can use one if or else if statement inside another if or else
if statement(s).
4 switch statement
A switch statement allows a variable to be tested for equality against
a list of values.
SYNTAX
An if statement consists of a Boolean expression followed
by one or more statements.
Syntax
Following is the syntax of an if statement −
if(Boolean_expression) {
// Statements will execute if the Boolean expression is true
}
If the Boolean expression evaluates to true then the block
of code inside the if statement will be executed. If not, the
first set of code after the end of the if statement (after the
closing curly brace) will be executed.
FLOW
DIAGRAM
EXAMPLE
Output
This is if statement.
IF ELSE
STATEMENT
An if statement can be followed by an optional else statement, which
executes when the Boolean expression is false.
Syntax
Following is the syntax of an if...else statement −
if(Boolean_expression) {
// Executes when the Boolean expression is true
}
else {
// Executes when the Boolean expression is false
}
If the boolean expression evaluates to true, then the if block of code will be
executed, otherwise else block of code will be executed.
FLOW
DIAGRAM
EXAMPLE
Output
This is else statement
IF..ELSE..IF
STATEMENT
An if statement can be followed by an optional else if...else statement, which
is very useful to
test various conditions using single if...else if statement.
When using if, else if, else statements there are a few points to keep in mind.
•An if can have zero or one else's and it must come after any else if's.
•An if can have zero to many else if's and they must come before the else.
•Once an else if succeeds, none of the remaining else if's or else's will be
tested.
Syntax
Following is the syntax of an if...else statement −
if(Boolean_expression 1) {
// Executes when the Boolean expression 1 is true
}
else if(Boolean_expression 2) {
// Executes when the Boolean expression 2 is true
}
else if(Boolean_expression 3) {
// Executes when the Boolean expression 3 is true}else {
// Executes when the none of the above condition is true.
}
EXAMPLE
Output
Value of X is 30
NESTED IF –
ELSE
STATEMENT
It is always legal to nest if-else statements which
means you can use one if or else if statement inside
another if or else if statement.
Syntax
if(Boolean_expression 1) {
// Executes when the Boolean expression 1 is true
if(Boolean_expression 2) {
// Executes when the Boolean expression 2 is true
}
}
You can nest else if...else in the similar way as we
have nested if statement.
EXAMPLE
Output
X = 30 and Y = 10
SWITCH
STATEMENT
A switch statement allows a variable to be tested for equality against a
list of values. Each value is called a case,
and the variable being switched on is checked for each case.
Syntax
The syntax of enhanced for loop is −
switch(expression)
{
case value :
// Statements
break; // optional
case value :
// Statements
break; // optional
// You can have any number of case statements.
default : // Optional
// Statements
}
RULESAPPLY
TOSWITCH
STATEMENT
The following rules apply to a switch statement −
 The variable used in a switch statement can only be
integers, convertable integers (byte, short, char),
strings and enums.
 You can have any number of case statements within a
switch. Each case is followed by the value to be
compared to and a colon.
 The value for a case must be the same data type as
the variable in the switch and it must be a constant or
a literal.
 When the variable being switched on is equal to a
case, the statements following that case will execute
until a break statement is reached.
RULESAPPLY
TOSWITCH
STATEMENT
 When a break statement is reached, the
switch terminates, and the flow of control
jumps to the next line following the switch
statement.
 Not every case needs to contain a break. If no
break appears, the flow of control will fall
through to subsequent cases until a break is
reached.
 A switch statement can have an optional
default case, which must appear at the end of
the switch. The default case can be used for
performing a task when none of the cases is
true. No break is needed in the default case.
FLOW
DIAGRAM
EXAMPLE
PROGRAM
Output
Well doneYour
grade is C
REMINDERS
You have to try the example
program to your java compiler to
exercise your mine, you can edit
with it.
Thank you 

More Related Content

What's hot (19)

PPTX
C# conditional branching statement
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Constructs (Programming Methodology)
Jyoti Bhardwaj
 
PPTX
Control statements in Java
Jin Castor
 
PPTX
Control Statements in Java
Niloy Saha
 
PPSX
Control Structures in Visual Basic
Tushar Jain
 
PPTX
Chapter 4 java
Ahmad sohail Kakar
 
PDF
Control structures in Java
Ravi_Kant_Sahu
 
PPTX
Java Decision Control
Jayfee Ramos
 
PPTX
Data structures vb
nicky_walters
 
PPT
Mesics lecture 6 control statement = if -else if__else
eShikshak
 
PPTX
Chapter 2 : Programming with Java Statements
It Academy
 
PPTX
Decision control structures
Rahul Bathri
 
PDF
Python decision making
Learnbay Datascience
 
PPT
Control statements
CutyChhaya
 
PDF
nuts and bolts of c++
guestfb6ada
 
PPTX
Switch statement, break statement, go to statement
Raj Parekh
 
PPTX
Decision statements in vb.net
ilakkiya
 
PPTX
Conditional statements
cherrybear2014
 
C# conditional branching statement
baabtra.com - No. 1 supplier of quality freshers
 
Constructs (Programming Methodology)
Jyoti Bhardwaj
 
Control statements in Java
Jin Castor
 
Control Statements in Java
Niloy Saha
 
Control Structures in Visual Basic
Tushar Jain
 
Chapter 4 java
Ahmad sohail Kakar
 
Control structures in Java
Ravi_Kant_Sahu
 
Java Decision Control
Jayfee Ramos
 
Data structures vb
nicky_walters
 
Mesics lecture 6 control statement = if -else if__else
eShikshak
 
Chapter 2 : Programming with Java Statements
It Academy
 
Decision control structures
Rahul Bathri
 
Python decision making
Learnbay Datascience
 
Control statements
CutyChhaya
 
nuts and bolts of c++
guestfb6ada
 
Switch statement, break statement, go to statement
Raj Parekh
 
Decision statements in vb.net
ilakkiya
 
Conditional statements
cherrybear2014
 

Similar to Computer programming 2 - Lesson 7 (20)

PPTX
control statements
Azeem Sultan
 
PPTX
Java Control Statement Control Statement.pptx
changepass
 
PPTX
Flow of control by deepak lakhlan
Deepak Lakhlan
 
PPS
Control statements
kamal kotecha
 
PPTX
Control structures
Gehad Enayat
 
PPTX
controlStatement.pptx, CONTROL STATEMENTS IN JAVA
DrNeetuSharma5
 
PPTX
Control statements in java
Madishetty Prathibha
 
PPT
Decision Making and Branching in C
RAJ KUMAR
 
PPTX
Chapter05-Control Structures.pptx
AdrianVANTOPINA
 
PDF
Lecture 8
Tanveer Malik
 
PPT
_Java__Expressions__and__FlowControl.ppt
JyothiAmpally
 
PPTX
Conditional statement in c
Muthuganesh S
 
PPTX
6.pptx
HarishNayak47
 
PPTX
3-Decision making and Control structures-28-04-2023.pptx
SrikarPrasadDonavall
 
PPT
slides03.ppt
Anjali127411
 
PDF
C programming decision making
SENA
 
PPTX
Decision_Makoiyuhrttouehgouesygytiuojth0ueyutwaeing.pptx
pratikdagar100
 
PPTX
Decision Making Statement in C ppt
MANJUTRIPATHI7
 
PPTX
Control structures in java
VINOTH R
 
PDF
Control flow statements in java web applications
RajithKarunarathne1
 
control statements
Azeem Sultan
 
Java Control Statement Control Statement.pptx
changepass
 
Flow of control by deepak lakhlan
Deepak Lakhlan
 
Control statements
kamal kotecha
 
Control structures
Gehad Enayat
 
controlStatement.pptx, CONTROL STATEMENTS IN JAVA
DrNeetuSharma5
 
Control statements in java
Madishetty Prathibha
 
Decision Making and Branching in C
RAJ KUMAR
 
Chapter05-Control Structures.pptx
AdrianVANTOPINA
 
Lecture 8
Tanveer Malik
 
_Java__Expressions__and__FlowControl.ppt
JyothiAmpally
 
Conditional statement in c
Muthuganesh S
 
3-Decision making and Control structures-28-04-2023.pptx
SrikarPrasadDonavall
 
slides03.ppt
Anjali127411
 
C programming decision making
SENA
 
Decision_Makoiyuhrttouehgouesygytiuojth0ueyutwaeing.pptx
pratikdagar100
 
Decision Making Statement in C ppt
MANJUTRIPATHI7
 
Control structures in java
VINOTH R
 
Control flow statements in java web applications
RajithKarunarathne1
 
Ad

More from MLG College of Learning, Inc (20)

PPTX
PC111.Lesson1
MLG College of Learning, Inc
 
PPTX
PC111-lesson1.pptx
MLG College of Learning, Inc
 
PPTX
PC LEESOON 6.pptx
MLG College of Learning, Inc
 
PPTX
PC 106 PPT-09.pptx
MLG College of Learning, Inc
 
PPTX
PC 106 PPT-07
MLG College of Learning, Inc
 
PPTX
PC 106 PPT-01
MLG College of Learning, Inc
 
PPTX
PC 106 Slide 04
MLG College of Learning, Inc
 
PPTX
PC 106 Slide no.02
MLG College of Learning, Inc
 
PPTX
pc-106-slide-3
MLG College of Learning, Inc
 
PPTX
PC 106 Slide 2
MLG College of Learning, Inc
 
PPTX
PC 106 Slide 1.pptx
MLG College of Learning, Inc
 
PDF
Db2 characteristics of db ms
MLG College of Learning, Inc
 
PDF
Db1 introduction
MLG College of Learning, Inc
 
Ad

Recently uploaded (20)

PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PDF
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PDF
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PPTX
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PDF
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
Dimensions of Societal Planning in Commonism
StefanMz
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 

Computer programming 2 - Lesson 7

  • 1. Computer Programming 2 Lesson 9– Java – Decision Making Prepared by: Analyn G. Regaton
  • 2. DECISION MAKING Decision making structures have one or more conditions to be evaluated or tested by the program, along with a statement or statements that are to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.
  • 3. TYPESOF DECISION MAKING STATEMENT Sr.No. Statement & Description 1 if statement An if statement consists of a boolean expression followed by one or more statements. 2 if...else statement An if statement can be followed by an optional else statement, which executes when the boolean expression is false. 3 nested if statement You can use one if or else if statement inside another if or else if statement(s). 4 switch statement A switch statement allows a variable to be tested for equality against a list of values.
  • 4. SYNTAX An if statement consists of a Boolean expression followed by one or more statements. Syntax Following is the syntax of an if statement − if(Boolean_expression) { // Statements will execute if the Boolean expression is true } If the Boolean expression evaluates to true then the block of code inside the if statement will be executed. If not, the first set of code after the end of the if statement (after the closing curly brace) will be executed.
  • 7. IF ELSE STATEMENT An if statement can be followed by an optional else statement, which executes when the Boolean expression is false. Syntax Following is the syntax of an if...else statement − if(Boolean_expression) { // Executes when the Boolean expression is true } else { // Executes when the Boolean expression is false } If the boolean expression evaluates to true, then the if block of code will be executed, otherwise else block of code will be executed.
  • 10. IF..ELSE..IF STATEMENT An if statement can be followed by an optional else if...else statement, which is very useful to test various conditions using single if...else if statement. When using if, else if, else statements there are a few points to keep in mind. •An if can have zero or one else's and it must come after any else if's. •An if can have zero to many else if's and they must come before the else. •Once an else if succeeds, none of the remaining else if's or else's will be tested. Syntax Following is the syntax of an if...else statement − if(Boolean_expression 1) { // Executes when the Boolean expression 1 is true } else if(Boolean_expression 2) { // Executes when the Boolean expression 2 is true } else if(Boolean_expression 3) { // Executes when the Boolean expression 3 is true}else { // Executes when the none of the above condition is true. }
  • 12. NESTED IF – ELSE STATEMENT It is always legal to nest if-else statements which means you can use one if or else if statement inside another if or else if statement. Syntax if(Boolean_expression 1) { // Executes when the Boolean expression 1 is true if(Boolean_expression 2) { // Executes when the Boolean expression 2 is true } } You can nest else if...else in the similar way as we have nested if statement.
  • 13. EXAMPLE Output X = 30 and Y = 10
  • 14. SWITCH STATEMENT A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case. Syntax The syntax of enhanced for loop is − switch(expression) { case value : // Statements break; // optional case value : // Statements break; // optional // You can have any number of case statements. default : // Optional // Statements }
  • 15. RULESAPPLY TOSWITCH STATEMENT The following rules apply to a switch statement −  The variable used in a switch statement can only be integers, convertable integers (byte, short, char), strings and enums.  You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.  The value for a case must be the same data type as the variable in the switch and it must be a constant or a literal.  When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.
  • 16. RULESAPPLY TOSWITCH STATEMENT  When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.  Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.  A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.
  • 19. REMINDERS You have to try the example program to your java compiler to exercise your mine, you can edit with it. Thank you 