SlideShare una empresa de Scribd logo
1 de 13
Descargar para leer sin conexión
Decision Making and Branching
In JAVA
Decision Making Statements
• There are two types of decision making statements in
Java. They are:
 if statements
 switch statements
The if Statement
• The if statement is a powerful decision making statement and is used to
control the flow of execution of statements. The if statement is Java’s
conditional branch statement. It can be used to route program execution
through two different paths. Here is the general form of the if statement:
if (condition) statement1;
• the if statement may be implemented in different forms depending on the
complexity of condition to be tested.
• Simple if Statement
• If…else Statement
• Nested if..else Statement
• else if ladder
SIMPLE IF STATEMENT
• The general form of a simple if statement is
if(test_expression)
{
Statement_true_block;
} Statement_x;
• The statement block may be a single statement or a
group of statement. If the test expression is true then
statement_true_block will be executed otherwise it
will be skipped.
The If…else Statement
• The If…else Statement is an extension of the simple if statement. An if
statement can be followed by an optional else statement, which executes
when the Boolean expression is false.
• Syntax:
if(test expression)
{
True-block statement;
}else
{
False-block statement;
}Statement-x;
Nesting of IF..Else Statements
When a series of decision are involved then we may have to use more
than one if…else statement in nested form as follows:
The general syntax is
if(test condition1)
{
if(test condition2)
{ Statement1;
}
else
{ Statement2;
}
}
else
{ Statement3;
}Statement-x
THE else if ladder
if(test condition1)
statement1;
else if(test condition2)
Statement2;
else if(test condition3)
statement3;
………….
else if(condition n)
statement n;
else
default statement;
statement x;
The Switch Statement
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.
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
}
rules apply to a switch statement
• The variable used in a switch statement can only be a byte,
short, int, or char.
• 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.
Continued..
• 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 throughto 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.
The ?; operator
• The value of a variable often depends on whether a
particular boolean expression is or is not true and on
nothing else. For instance one common operation is
setting the value of a variable to the maximum of two
quantities.
• In Java you might write
if (a > b) {max = a;
}
else {
max = b;}
The ?; operator
• Setting a single variable to one of two states based on a
single condition is such a common use of if-else that a
shortcut has been devised for it, the conditional
operator, ?;. Using the conditional operator you can
rewrite the above example in a single line like this:
max = (a > b) ? a : b;
• (a > b) ? a : b; is an expression which returns one of two
values, a or b. The condition, (a > b), is tested. If it is
true the first value, a, is returned. If it is false, the
second value, b, is returned
itft-Decision making and branching in java

Más contenido relacionado

La actualidad más candente

Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
BHUVIJAYAVELU
 
Operators and Expressions in Java
Operators and Expressions in JavaOperators and Expressions in Java
Operators and Expressions in Java
Abhilash Nair
 

La actualidad más candente (20)

Data Types, Variables, and Operators
Data Types, Variables, and OperatorsData Types, Variables, and Operators
Data Types, Variables, and Operators
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Java Tokens
Java  TokensJava  Tokens
Java Tokens
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in Java
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Classes and Objects in C#
Classes and Objects in C#Classes and Objects in C#
Classes and Objects in C#
 
Applets in java
Applets in javaApplets in java
Applets in java
 
Access specifiers(modifiers) in java
Access specifiers(modifiers) in javaAccess specifiers(modifiers) in java
Access specifiers(modifiers) in java
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
Operators in java presentation
Operators in java presentationOperators in java presentation
Operators in java presentation
 
Operators and Expressions in Java
Operators and Expressions in JavaOperators and Expressions in Java
Operators and Expressions in Java
 
Finalize() method
Finalize() methodFinalize() method
Finalize() method
 
Decision Making and Looping
Decision Making and LoopingDecision Making and Looping
Decision Making and Looping
 
Data types
Data typesData types
Data types
 

Destacado

Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
farhan amjad
 
Classes And Objects
Classes And ObjectsClasses And Objects
Classes And Objects
rahulsahay19
 
Constants, Variables and Data Types in Java
Constants, Variables and Data Types in JavaConstants, Variables and Data Types in Java
Constants, Variables and Data Types in Java
Abhilash Nair
 

Destacado (15)

ITFT-Constants, variables and data types in java
ITFT-Constants, variables and data types in javaITFT-Constants, variables and data types in java
ITFT-Constants, variables and data types in java
 
Branching in PowerPoint
Branching in PowerPointBranching in PowerPoint
Branching in PowerPoint
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
 
C language control statements
C language  control statementsC language  control statements
C language control statements
 
02 data types in java
02 data types in java02 data types in java
02 data types in java
 
Data types, Variables, Expressions & Arithmetic Operators in java
Data types, Variables, Expressions & Arithmetic Operators in javaData types, Variables, Expressions & Arithmetic Operators in java
Data types, Variables, Expressions & Arithmetic Operators in java
 
Loops in C
Loops in CLoops in C
Loops in C
 
Loops in C Programming
Loops in C ProgrammingLoops in C Programming
Loops in C Programming
 
Classes And Objects
Classes And ObjectsClasses And Objects
Classes And Objects
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling ppt
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Control statements
Control statementsControl statements
Control statements
 
C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.
 
Constants, Variables and Data Types in Java
Constants, Variables and Data Types in JavaConstants, Variables and Data Types in Java
Constants, Variables and Data Types in Java
 

Similar a itft-Decision making and branching in java

Control structures
Control structuresControl structures
Control structures
Gehad Enayat
 

Similar a itft-Decision making and branching in java (20)

Computer programming 2 Lesson 9
Computer programming 2  Lesson 9Computer programming 2  Lesson 9
Computer programming 2 Lesson 9
 
Computer programming 2 - Lesson 7
Computer programming 2 - Lesson 7Computer programming 2 - Lesson 7
Computer programming 2 - Lesson 7
 
Selection statements
Selection statementsSelection statements
Selection statements
 
Decision Making and Branching in C
Decision Making and Branching  in CDecision Making and Branching  in C
Decision Making and Branching in C
 
6.pptx
6.pptx6.pptx
6.pptx
 
Chapter 4(1)
Chapter 4(1)Chapter 4(1)
Chapter 4(1)
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
 
Conditional statement in c
Conditional statement in cConditional statement in c
Conditional statement in c
 
Flow of control by deepak lakhlan
Flow of control by deepak lakhlanFlow of control by deepak lakhlan
Flow of control by deepak lakhlan
 
Lecture 7 Control Statements.pdf
Lecture 7 Control Statements.pdfLecture 7 Control Statements.pdf
Lecture 7 Control Statements.pdf
 
Java Decision Control
Java Decision ControlJava Decision Control
Java Decision Control
 
Control statements anil
Control statements anilControl statements anil
Control statements anil
 
Control statements
Control statementsControl statements
Control statements
 
Do While Repetition Structure
Do While Repetition StructureDo While Repetition Structure
Do While Repetition Structure
 
SWITCH CASE STATEMENT IN C
SWITCH CASE STATEMENT IN CSWITCH CASE STATEMENT IN C
SWITCH CASE STATEMENT IN C
 
Decision Making Statement in C ppt
Decision Making Statement in C pptDecision Making Statement in C ppt
Decision Making Statement in C ppt
 
Introduction to Java
Introduction to JavaIntroduction to Java
Introduction to Java
 
Flow of Control
Flow of ControlFlow of Control
Flow of Control
 
Control structures
Control structuresControl structures
Control structures
 
Control structure
Control structureControl structure
Control structure
 

Más de Atul Sehdev

Más de Atul Sehdev (8)

itft-Overview of java language
itft-Overview of java languageitft-Overview of java language
itft-Overview of java language
 
itft-Operators in java
itft-Operators in javaitft-Operators in java
itft-Operators in java
 
itft-Java evolution
itft-Java evolutionitft-Java evolution
itft-Java evolution
 
itft-Inheritance in java
itft-Inheritance in javaitft-Inheritance in java
itft-Inheritance in java
 
itft-Fundamentals of object–oriented programming in java
itft-Fundamentals of object–oriented programming in javaitft-Fundamentals of object–oriented programming in java
itft-Fundamentals of object–oriented programming in java
 
ITFT-Classes and object in java
ITFT-Classes and object in javaITFT-Classes and object in java
ITFT-Classes and object in java
 
ITFT- C,c++,java and world wide web
ITFT- C,c++,java and world wide webITFT- C,c++,java and world wide web
ITFT- C,c++,java and world wide web
 
ITFT- Applet in java
ITFT- Applet in javaITFT- Applet in java
ITFT- Applet in java
 

Último

Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Último (20)

Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 

itft-Decision making and branching in java

  • 1. Decision Making and Branching In JAVA
  • 2. Decision Making Statements • There are two types of decision making statements in Java. They are:  if statements  switch statements
  • 3. The if Statement • The if statement is a powerful decision making statement and is used to control the flow of execution of statements. The if statement is Java’s conditional branch statement. It can be used to route program execution through two different paths. Here is the general form of the if statement: if (condition) statement1; • the if statement may be implemented in different forms depending on the complexity of condition to be tested. • Simple if Statement • If…else Statement • Nested if..else Statement • else if ladder
  • 4. SIMPLE IF STATEMENT • The general form of a simple if statement is if(test_expression) { Statement_true_block; } Statement_x; • The statement block may be a single statement or a group of statement. If the test expression is true then statement_true_block will be executed otherwise it will be skipped.
  • 5. The If…else Statement • The If…else Statement is an extension of the simple if statement. An if statement can be followed by an optional else statement, which executes when the Boolean expression is false. • Syntax: if(test expression) { True-block statement; }else { False-block statement; }Statement-x;
  • 6. Nesting of IF..Else Statements When a series of decision are involved then we may have to use more than one if…else statement in nested form as follows: The general syntax is if(test condition1) { if(test condition2) { Statement1; } else { Statement2; } } else { Statement3; }Statement-x
  • 7. THE else if ladder if(test condition1) statement1; else if(test condition2) Statement2; else if(test condition3) statement3; …………. else if(condition n) statement n; else default statement; statement x;
  • 8. The Switch Statement 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. 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 }
  • 9. rules apply to a switch statement • The variable used in a switch statement can only be a byte, short, int, or char. • 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.
  • 10. Continued.. • 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 throughto 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.
  • 11. The ?; operator • The value of a variable often depends on whether a particular boolean expression is or is not true and on nothing else. For instance one common operation is setting the value of a variable to the maximum of two quantities. • In Java you might write if (a > b) {max = a; } else { max = b;}
  • 12. The ?; operator • Setting a single variable to one of two states based on a single condition is such a common use of if-else that a shortcut has been devised for it, the conditional operator, ?;. Using the conditional operator you can rewrite the above example in a single line like this: max = (a > b) ? a : b; • (a > b) ? a : b; is an expression which returns one of two values, a or b. The condition, (a > b), is tested. If it is true the first value, a, is returned. If it is false, the second value, b, is returned