SlideShare a Scribd company logo
1 of 25
01/13/19
1
ControlStatements
 Control statements are used in programming languages to cause the
flow of control to advance and branch based on changes to the state
of a program.
 In Java, control statements can be divided under the following three
categories:
 Selection statements
 Iteration statements
 Jump statements
01/13/19
2
ControlStatements
 Selection statements are used in a program to choose different paths
of execution based upon the outcome of an expression or the state of
a variable.
 Using if and if...else
 Nested if Statements
 Using switch Statements
 Conditional Operator
01/13/19
3
ControlStatements
01/13/19
4
ControlStatements
Principal forms:
 Additional forms
01/13/19
5
ControlStatements
01/13/19
6
ControlStatements
if( a > b)
{
System.out.println("A = " + a + "tB = " +
b);
System.out.println("A is greater than B");
}
else
{
System.out.println("A = " + a + "tB = " +
b);
System.out.println("Either both are equal
or B is greater");
}
01/13/19
7
ControlStatements
class Example4_2
{
public static void main(String Args[])
{
int a = 3;
if (a <= 10 && a > 0)
{
System.out.println("Number is valid.");
if ( a < 5)
System.out.println("From 1 to 5");
else
System.out.println("From 5 to 10");
}
else
System.out.println("Number is not valid");
}
}
01/13/19
8
ControlStatements
class Example4_1{
public static void main (String Args[]){
int a = 5;
boolean val = false;
if(val)
System.out.println("val is false, so it won't execute";
else if (a < 0 )
System.out.println("A is a negative value");
else if (a > 0)
System.out.println ("A is a positive value");
else
System.out.println ("A is equal to zero");
}
}
01/13/19
9
ControlStatements
01/13/19
10
ControlStatements
01/13/19
11
ControlStatements
01/13/19
12
ControlStatements
class Example4_3{
public static void main(String Args[]){
int month = 3;
switch (month){
case 1:
System.out.println("The month of January");
break;
case 2:
System.out.println("The month of February");
break;
case 3:
System.out.println("The month of March");
break;
case 4:
System.out.println("The month of April");
break; case 5:
System.out.println("The month of May");
break;
case 6:
System.out.println("The month of June");
break;
case 7:
System.out.println("The month of July");
break;
case 8:
System.out.println("The month of August");
break;
case 9:
System.out.println("The month of September");
break;
case 10:
System.out.println("The month of October");
break;
case 11:
System.out.println("The month of November");
break;
case 12:
System.out.println("The month of December");
break;
default:
System.out.println("Invalid month");
}
}
}
01/13/19
13
ControlStatements
01/13/19
14
ControlStatements
01/13/19
15
ControlStatements
// Returns the smallest n
// such that 2^n >= x
public static int intLog2 (int x)
{
int n = 0, p = 1;
while ( p < x )
{
p *= 2;
n++;
}
return n;
}
Initialization
Testing
Change
01/13/19
16
ControlStatements
 for is a shorthand that combines in one statement initialization,
condition, and change
for ( initialization; condition; change )
{
statement1;
statement2;
...
statementN;
}
01/13/19
17
ControlStatements
// Returns the smallest n
// such that 2^n >= x
public static int intLog2 (int x)
{
int n = 0, p;
for (p = 1; p < x; p *= 2)
{
n++;
}
return n;
}
Initialization
Testing
Change
01/13/19
18
ControlStatements
public class DoWhileExample
{
public static void main (String[ ] args)
{
int i =0;
do
{
System.out.println ("i is : " + i);
i++;
} while (i < 4);
}
}
01/13/19
19
ControlStatements
01/13/19
20
ControlStatements
 Break in a loop instructs the program to immediately quit the current
iteration and go to the first statement following the loop.
SYNTAX break label;
 Break statement has two forms:
 Labeled Break statement
 Unlabeled Break statement
01/13/19
21
ControlStatements
 Labeled Break
for(int var =0; var < 5 ; var++)
{
System.out.println(“Var is : “ + var);
if(var == 3)
break;
}
01/13/19
22
ControlStatements
oUnlabeled Break
Outer:
for(int var1=0; var1 < 5 ; var1++)
{
for(int var2 = 1; var2 < 5;var2++)
{
System.out.println(“var1:” +
var1 + “, var2:” + var2);
if(var1 == 3)
break Outer;
}
}
 Continue statement is used when we want to skip the rest of the
statement in the body of the loop and continue with the next
iteration of the loop.
SYNTAX continue label;
 There are two forms of continue statement in Java.
 Unlabeled Continue Statement
 Labeled Continue Statement
01/13/19
23
ControlStatements
01/13/19
24
ControlStatements
o Labeled Continue
Outer:
for(int var1 =0; var1 < 5 ; var1++)
{
for(int var2=0 ; var2 < 5 ; var2++)
{
if(var2 == 2)
continue Outer;
System.out.println(“var1:” + var1
+ “, var2:”+ var2);
}
}
oUnlabeled Continue
for(int var1 =0; var1 < 5 ; var1++)
{
for(int var2=0 ; var2 < 5 ; var2++)
{
if(var2 == 2)
continue;
System.out.println(“var1:” +
var1 + “, var2:”+ var2);
}
}
 Return in a loop instructs the program to immediately quit the
current method and return to the calling method.
 Example
class Return
{
public static void main(String args[])
{
boolean t = true;
System.out.println("Before the return.");
if(t) return; // return to caller
System.out.println("This won't execute.");
}
}
01/13/19
25
ControlStatements

More Related Content

What's hot (20)

Control flow statements in java
Control flow statements in javaControl flow statements in java
Control flow statements in java
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Wrapper classes
Wrapper classes Wrapper classes
Wrapper classes
 
Type casting in java
Type casting in javaType casting in java
Type casting in java
 
Java string handling
Java string handlingJava string handling
Java string handling
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Java Methods
Java MethodsJava Methods
Java Methods
 
Basic i/o & file handling in java
Basic i/o & file handling in javaBasic i/o & file handling in java
Basic i/o & file handling in java
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
 
Java Presentation
Java PresentationJava Presentation
Java Presentation
 
C# Inheritance
C# InheritanceC# Inheritance
C# Inheritance
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Java Method, Static Block
Java Method, Static BlockJava Method, Static Block
Java Method, Static Block
 
Java loops
Java loopsJava loops
Java loops
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
 

Similar to Control statements in java programmng

Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionalish sha
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionalish sha
 
Unit_3A_If_Else_Switch and a if else statment example
Unit_3A_If_Else_Switch and a if else statment exampleUnit_3A_If_Else_Switch and a if else statment example
Unit_3A_If_Else_Switch and a if else statment exampleumaghosal12101974
 
2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.pptManojKhadilkar1
 
3. control statements
3. control statements3. control statements
3. control statementsamar kakde
 
Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programmingnmahi96
 
Programming in java - Concepts- Operators- Control statements-Expressions
Programming in java - Concepts- Operators- Control statements-ExpressionsProgramming in java - Concepts- Operators- Control statements-Expressions
Programming in java - Concepts- Operators- Control statements-ExpressionsLovelitJose
 
C language UPTU Unit3 Slides
C language UPTU Unit3 SlidesC language UPTU Unit3 Slides
C language UPTU Unit3 SlidesRakesh Roshan
 
control statements
control statementscontrol statements
control statementsAzeem Sultan
 
Java input Scanner
Java input Scanner Java input Scanner
Java input Scanner Huda Alameen
 
Chapter 4.1
Chapter 4.1Chapter 4.1
Chapter 4.1sotlsoc
 
4.CONTROL STATEMENTS_MB.ppt .
4.CONTROL STATEMENTS_MB.ppt                 .4.CONTROL STATEMENTS_MB.ppt                 .
4.CONTROL STATEMENTS_MB.ppt .happycocoman
 
Lecture - 5 Control Statement
Lecture - 5 Control StatementLecture - 5 Control Statement
Lecture - 5 Control Statementmanish kumar
 
Operators, control statements represented in java
Operators, control statements  represented in javaOperators, control statements  represented in java
Operators, control statements represented in javaTharuniDiddekunta
 
Selection Control Structures
Selection Control StructuresSelection Control Structures
Selection Control StructuresPRN USM
 

Similar to Control statements in java programmng (20)

Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
 
Unit_3A_If_Else_Switch and a if else statment example
Unit_3A_If_Else_Switch and a if else statment exampleUnit_3A_If_Else_Switch and a if else statment example
Unit_3A_If_Else_Switch and a if else statment example
 
2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt
 
3. control statements
3. control statements3. control statements
3. control statements
 
M C6java5
M C6java5M C6java5
M C6java5
 
Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programming
 
Programming in java - Concepts- Operators- Control statements-Expressions
Programming in java - Concepts- Operators- Control statements-ExpressionsProgramming in java - Concepts- Operators- Control statements-Expressions
Programming in java - Concepts- Operators- Control statements-Expressions
 
C language UPTU Unit3 Slides
C language UPTU Unit3 SlidesC language UPTU Unit3 Slides
C language UPTU Unit3 Slides
 
control statements
control statementscontrol statements
control statements
 
Java input Scanner
Java input Scanner Java input Scanner
Java input Scanner
 
Chapter 4.1
Chapter 4.1Chapter 4.1
Chapter 4.1
 
Data structures
Data structuresData structures
Data structures
 
4.CONTROL STATEMENTS_MB.ppt .
4.CONTROL STATEMENTS_MB.ppt                 .4.CONTROL STATEMENTS_MB.ppt                 .
4.CONTROL STATEMENTS_MB.ppt .
 
Lecture - 5 Control Statement
Lecture - 5 Control StatementLecture - 5 Control Statement
Lecture - 5 Control Statement
 
Controls & Loops in C
Controls & Loops in C Controls & Loops in C
Controls & Loops in C
 
Operators, control statements represented in java
Operators, control statements  represented in javaOperators, control statements  represented in java
Operators, control statements represented in java
 
Selection Control Structures
Selection Control StructuresSelection Control Structures
Selection Control Structures
 
Lect 3-4 Zaheer Abbas
Lect 3-4 Zaheer AbbasLect 3-4 Zaheer Abbas
Lect 3-4 Zaheer Abbas
 
Decision Making and Branching
Decision Making and BranchingDecision Making and Branching
Decision Making and Branching
 

More from Savitribai Phule Pune University (10)

Learn c programming
Learn c programmingLearn c programming
Learn c programming
 
R programming by ganesh kavhar
R programming by ganesh kavharR programming by ganesh kavhar
R programming by ganesh kavhar
 
Networking with java
Networking with javaNetworking with java
Networking with java
 
Python for data analysis
Python for data analysisPython for data analysis
Python for data analysis
 
Search engines by ganesh kavhar
Search engines by ganesh kavharSearch engines by ganesh kavhar
Search engines by ganesh kavhar
 
Python by ganesh kavhar
Python by ganesh kavharPython by ganesh kavhar
Python by ganesh kavhar
 
Machine learning by ganesh kavhar
Machine learning by ganesh kavharMachine learning by ganesh kavhar
Machine learning by ganesh kavhar
 
Android apps
Android appsAndroid apps
Android apps
 
Android apps upload slideshare
Android apps upload slideshareAndroid apps upload slideshare
Android apps upload slideshare
 
Android app upload
Android app uploadAndroid app upload
Android app upload
 

Recently uploaded

Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxPooja Bhuva
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111GangaMaiya1
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17Celine George
 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationNeilDeclaro1
 
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)Jisc
 
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.docxRamakrishna Reddy Bijjam
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsNbelano25
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfPondicherry University
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptxJoelynRubio1
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
latest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answerslatest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answersdalebeck957
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 

Recently uploaded (20)

Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health Education
 
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)
 
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
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
latest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answerslatest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answers
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 

Control statements in java programmng

  • 2.  Control statements are used in programming languages to cause the flow of control to advance and branch based on changes to the state of a program.  In Java, control statements can be divided under the following three categories:  Selection statements  Iteration statements  Jump statements 01/13/19 2 ControlStatements
  • 3.  Selection statements are used in a program to choose different paths of execution based upon the outcome of an expression or the state of a variable.  Using if and if...else  Nested if Statements  Using switch Statements  Conditional Operator 01/13/19 3 ControlStatements
  • 7. if( a > b) { System.out.println("A = " + a + "tB = " + b); System.out.println("A is greater than B"); } else { System.out.println("A = " + a + "tB = " + b); System.out.println("Either both are equal or B is greater"); } 01/13/19 7 ControlStatements class Example4_2 { public static void main(String Args[]) { int a = 3; if (a <= 10 && a > 0) { System.out.println("Number is valid."); if ( a < 5) System.out.println("From 1 to 5"); else System.out.println("From 5 to 10"); } else System.out.println("Number is not valid"); } }
  • 9. class Example4_1{ public static void main (String Args[]){ int a = 5; boolean val = false; if(val) System.out.println("val is false, so it won't execute"; else if (a < 0 ) System.out.println("A is a negative value"); else if (a > 0) System.out.println ("A is a positive value"); else System.out.println ("A is equal to zero"); } } 01/13/19 9 ControlStatements
  • 12. 01/13/19 12 ControlStatements class Example4_3{ public static void main(String Args[]){ int month = 3; switch (month){ case 1: System.out.println("The month of January"); break; case 2: System.out.println("The month of February"); break; case 3: System.out.println("The month of March"); break; case 4: System.out.println("The month of April"); break; case 5: System.out.println("The month of May"); break; case 6: System.out.println("The month of June"); break; case 7: System.out.println("The month of July"); break; case 8: System.out.println("The month of August"); break; case 9: System.out.println("The month of September"); break; case 10: System.out.println("The month of October"); break; case 11: System.out.println("The month of November"); break; case 12: System.out.println("The month of December"); break; default: System.out.println("Invalid month"); } } }
  • 15. 01/13/19 15 ControlStatements // Returns the smallest n // such that 2^n >= x public static int intLog2 (int x) { int n = 0, p = 1; while ( p < x ) { p *= 2; n++; } return n; } Initialization Testing Change
  • 16. 01/13/19 16 ControlStatements  for is a shorthand that combines in one statement initialization, condition, and change for ( initialization; condition; change ) { statement1; statement2; ... statementN; }
  • 17. 01/13/19 17 ControlStatements // Returns the smallest n // such that 2^n >= x public static int intLog2 (int x) { int n = 0, p; for (p = 1; p < x; p *= 2) { n++; } return n; } Initialization Testing Change
  • 19. public class DoWhileExample { public static void main (String[ ] args) { int i =0; do { System.out.println ("i is : " + i); i++; } while (i < 4); } } 01/13/19 19 ControlStatements
  • 21.  Break in a loop instructs the program to immediately quit the current iteration and go to the first statement following the loop. SYNTAX break label;  Break statement has two forms:  Labeled Break statement  Unlabeled Break statement 01/13/19 21 ControlStatements
  • 22.  Labeled Break for(int var =0; var < 5 ; var++) { System.out.println(“Var is : “ + var); if(var == 3) break; } 01/13/19 22 ControlStatements oUnlabeled Break Outer: for(int var1=0; var1 < 5 ; var1++) { for(int var2 = 1; var2 < 5;var2++) { System.out.println(“var1:” + var1 + “, var2:” + var2); if(var1 == 3) break Outer; } }
  • 23.  Continue statement is used when we want to skip the rest of the statement in the body of the loop and continue with the next iteration of the loop. SYNTAX continue label;  There are two forms of continue statement in Java.  Unlabeled Continue Statement  Labeled Continue Statement 01/13/19 23 ControlStatements
  • 24. 01/13/19 24 ControlStatements o Labeled Continue Outer: for(int var1 =0; var1 < 5 ; var1++) { for(int var2=0 ; var2 < 5 ; var2++) { if(var2 == 2) continue Outer; System.out.println(“var1:” + var1 + “, var2:”+ var2); } } oUnlabeled Continue for(int var1 =0; var1 < 5 ; var1++) { for(int var2=0 ; var2 < 5 ; var2++) { if(var2 == 2) continue; System.out.println(“var1:” + var1 + “, var2:”+ var2); } }
  • 25.  Return in a loop instructs the program to immediately quit the current method and return to the calling method.  Example class Return { public static void main(String args[]) { boolean t = true; System.out.println("Before the return."); if(t) return; // return to caller System.out.println("This won't execute."); } } 01/13/19 25 ControlStatements