SlideShare ist ein Scribd-Unternehmen logo
1 von 13
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Data types
Data typesData types
Data types
 
Data Types & Variables in JAVA
Data Types & Variables in JAVAData Types & Variables in JAVA
Data Types & Variables in JAVA
 
String, string builder, string buffer
String, string builder, string bufferString, string builder, string buffer
String, string builder, string buffer
 
java token
java tokenjava token
java token
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
 
Methods in java
Methods in javaMethods in java
Methods in java
 
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with ExamplesDML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Decision making in JAVA
Decision making in JAVADecision making in JAVA
Decision making in JAVA
 
Data Types, Variables, and Operators
Data Types, Variables, and OperatorsData Types, Variables, and Operators
Data Types, Variables, and Operators
 
C++ decision making
C++ decision makingC++ decision making
C++ decision making
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
data types in C programming
data types in C programmingdata types in C programming
data types in C programming
 
Data types in java
Data types in javaData types in java
Data types in java
 

Andere mochten auch

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 javaAtul Sehdev
 
Branching in PowerPoint
Branching in PowerPointBranching in PowerPoint
Branching in PowerPointKarl Kapp
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methodsfarhan amjad
 
C language control statements
C language  control statementsC language  control statements
C language control statementssuman Aggarwal
 
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 javaJaved Rashid
 
Loops in C Programming
Loops in C ProgrammingLoops in C Programming
Loops in C ProgrammingHimanshu Negi
 
Classes And Objects
Classes And ObjectsClasses And Objects
Classes And Objectsrahulsahay19
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javaPratik Soares
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling pptJavabynataraJ
 
C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.Haard Shah
 
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 JavaAbhilash Nair
 

Andere mochten auch (15)

Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
 
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
 
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
 

Ähnlich wie itft-Decision making and branching in java

Selection statements
Selection statementsSelection statements
Selection statementsHarsh Dabas
 
Decision Making and Branching in C
Decision Making and Branching  in CDecision Making and Branching  in C
Decision Making and Branching in CRAJ KUMAR
 
Chapter 4(1)
Chapter 4(1)Chapter 4(1)
Chapter 4(1)TejaswiB4
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJTANUJ ⠀
 
Conditional statement in c
Conditional statement in cConditional statement in c
Conditional statement in cMuthuganesh S
 
Flow of control by deepak lakhlan
Flow of control by deepak lakhlanFlow of control by deepak lakhlan
Flow of control by deepak lakhlanDeepak Lakhlan
 
Lecture 7 Control Statements.pdf
Lecture 7 Control Statements.pdfLecture 7 Control Statements.pdf
Lecture 7 Control Statements.pdfSalmanKhurshid25
 
Java Decision Control
Java Decision ControlJava Decision Control
Java Decision ControlJayfee Ramos
 
Control statements anil
Control statements anilControl statements anil
Control statements anilAnil Dutt
 
Control statements
Control statementsControl statements
Control statementsCutyChhaya
 
Do While Repetition Structure
Do While Repetition StructureDo While Repetition Structure
Do While Repetition StructureShahzu2
 
Decision Making Statement in C ppt
Decision Making Statement in C pptDecision Making Statement in C ppt
Decision Making Statement in C pptMANJUTRIPATHI7
 
Control structures
Control structuresControl structures
Control structuresGehad Enayat
 

Ähnlich wie 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
 

Mehr von Atul Sehdev

itft-Overview of java language
itft-Overview of java languageitft-Overview of java language
itft-Overview of java languageAtul Sehdev
 
itft-Operators in java
itft-Operators in javaitft-Operators in java
itft-Operators in javaAtul Sehdev
 
itft-Java evolution
itft-Java evolutionitft-Java evolution
itft-Java evolutionAtul Sehdev
 
itft-Inheritance in java
itft-Inheritance in javaitft-Inheritance in java
itft-Inheritance in javaAtul Sehdev
 
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 javaAtul Sehdev
 
ITFT-Classes and object in java
ITFT-Classes and object in javaITFT-Classes and object in java
ITFT-Classes and object in javaAtul Sehdev
 
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 webAtul Sehdev
 
ITFT- Applet in java
ITFT- Applet in javaITFT- Applet in java
ITFT- Applet in javaAtul Sehdev
 

Mehr von 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
 

Kürzlich hochgeladen

The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
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 ConsultingTechSoup
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 

Kürzlich hochgeladen (20)

The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
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
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 

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