SlideShare a Scribd company logo
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

PPTX
Arrays in java
Arzath Areeff
 
PDF
Arrays in Java
Naz Abdalla
 
PDF
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Tushar B Kute
 
PPT
Java-java virtual machine
Surbhi Panhalkar
 
PDF
Collections and generics
Muthukumaran Subramanian
 
PPTX
Java program structure
shalinikarunakaran1
 
PPTX
Java Method, Static Block
Infoviaan Technologies
 
PPTX
Data Types, Variables, and Operators
Marwa Ali Eissa
 
Arrays in java
Arzath Areeff
 
Arrays in Java
Naz Abdalla
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Tushar B Kute
 
Java-java virtual machine
Surbhi Panhalkar
 
Collections and generics
Muthukumaran Subramanian
 
Java program structure
shalinikarunakaran1
 
Java Method, Static Block
Infoviaan Technologies
 
Data Types, Variables, and Operators
Marwa Ali Eissa
 

What's hot (20)

PPT
Java Threads and Concurrency
Sunil OS
 
PPTX
Java basics and java variables
Pushpendra Tyagi
 
PPTX
Classes, objects in JAVA
Abhilash Nair
 
PPT
Java collection
Arati Gadgil
 
PPTX
Control flow statements in java
yugandhar vadlamudi
 
PPTX
Array in c#
Prem Kumar Badri
 
PPTX
Operators in java
AbhishekMondal42
 
PPTX
Java if else condition - powerpoint persentation
Maneesha Caldera
 
PPT
Java Basics
Sunil OS
 
PPTX
Packages,static,this keyword in java
Vishnu Suresh
 
PPTX
Delegates and events in C#
Dr.Neeraj Kumar Pandey
 
PDF
An Introduction to Programming in Java: Arrays
Martin Chapman
 
PPT
Java operators
Shehrevar Davierwala
 
PPTX
Control statements in java
Madishetty Prathibha
 
PPTX
String, string builder, string buffer
SSN College of Engineering, Kalavakkam
 
PPTX
Operators and Expressions in Java
Abhilash Nair
 
PPTX
JAVA AWT
shanmuga rajan
 
PPTX
Java input
Jin Castor
 
PDF
Files in java
Muthukumaran Subramanian
 
PPT
Abstract class in java
Lovely Professional University
 
Java Threads and Concurrency
Sunil OS
 
Java basics and java variables
Pushpendra Tyagi
 
Classes, objects in JAVA
Abhilash Nair
 
Java collection
Arati Gadgil
 
Control flow statements in java
yugandhar vadlamudi
 
Array in c#
Prem Kumar Badri
 
Operators in java
AbhishekMondal42
 
Java if else condition - powerpoint persentation
Maneesha Caldera
 
Java Basics
Sunil OS
 
Packages,static,this keyword in java
Vishnu Suresh
 
Delegates and events in C#
Dr.Neeraj Kumar Pandey
 
An Introduction to Programming in Java: Arrays
Martin Chapman
 
Java operators
Shehrevar Davierwala
 
Control statements in java
Madishetty Prathibha
 
String, string builder, string buffer
SSN College of Engineering, Kalavakkam
 
Operators and Expressions in Java
Abhilash Nair
 
JAVA AWT
shanmuga rajan
 
Java input
Jin Castor
 
Abstract class in java
Lovely Professional University
 
Ad

Similar to Control statements in java programmng (20)

PPTX
Java 2.pptx
usmanusman720379
 
PPTX
6.pptx
HarishNayak47
 
PPT
Control statements
raksharao
 
PPTX
Control Statements in Java
Niloy Saha
 
PDF
csj-161127083146power point presentation
deepayaganti1
 
PPTX
Lecture - 5 Control Statement
manish kumar
 
PPTX
Programming in java - Concepts- Operators- Control statements-Expressions
LovelitJose
 
PDF
java notes.pdf
RajkumarHarishchandr1
 
PPTX
Unit-02 Selection, Mathematical Functions and loops.pptx
jessicafalcao1
 
PDF
Control structures in Java
Ravi_Kant_Sahu
 
PPT
05. Control Structures.ppt
AyushDut
 
PPT
Java control flow statements
Future Programming
 
PPTX
Control structures
Gehad Enayat
 
PPT
Control statements
CutyChhaya
 
PPTX
controlStatement.pptx, CONTROL STATEMENTS IN JAVA
DrNeetuSharma5
 
PPTX
Java chapter 3
Abdii Rashid
 
PPTX
Loop
prabhat kumar
 
PDF
9-java language basics part3
Amr Elghadban (AmrAngry)
 
PPTX
control statements
Azeem Sultan
 
PDF
Java chapter 5
Mukesh Tekwani
 
Java 2.pptx
usmanusman720379
 
Control statements
raksharao
 
Control Statements in Java
Niloy Saha
 
csj-161127083146power point presentation
deepayaganti1
 
Lecture - 5 Control Statement
manish kumar
 
Programming in java - Concepts- Operators- Control statements-Expressions
LovelitJose
 
java notes.pdf
RajkumarHarishchandr1
 
Unit-02 Selection, Mathematical Functions and loops.pptx
jessicafalcao1
 
Control structures in Java
Ravi_Kant_Sahu
 
05. Control Structures.ppt
AyushDut
 
Java control flow statements
Future Programming
 
Control structures
Gehad Enayat
 
Control statements
CutyChhaya
 
controlStatement.pptx, CONTROL STATEMENTS IN JAVA
DrNeetuSharma5
 
Java chapter 3
Abdii Rashid
 
9-java language basics part3
Amr Elghadban (AmrAngry)
 
control statements
Azeem Sultan
 
Java chapter 5
Mukesh Tekwani
 
Ad

More from Savitribai Phule Pune University (10)

PDF
Learn c programming
Savitribai Phule Pune University
 
PPT
R programming by ganesh kavhar
Savitribai Phule Pune University
 
PPT
Networking with java
Savitribai Phule Pune University
 
PPTX
Python for data analysis
Savitribai Phule Pune University
 
PPT
Search engines by ganesh kavhar
Savitribai Phule Pune University
 
PPT
Python by ganesh kavhar
Savitribai Phule Pune University
 
PPTX
Machine learning by ganesh kavhar
Savitribai Phule Pune University
 
PPSX
Android apps upload slideshare
Savitribai Phule Pune University
 
PPTX
Android app upload
Savitribai Phule Pune University
 
R programming by ganesh kavhar
Savitribai Phule Pune University
 
Networking with java
Savitribai Phule Pune University
 
Python for data analysis
Savitribai Phule Pune University
 
Search engines by ganesh kavhar
Savitribai Phule Pune University
 
Python by ganesh kavhar
Savitribai Phule Pune University
 
Machine learning by ganesh kavhar
Savitribai Phule Pune University
 
Android apps upload slideshare
Savitribai Phule Pune University
 

Recently uploaded (20)

PPTX
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PDF
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
PPT
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
PDF
7.Particulate-Nature-of-Matter.ppt/8th class science curiosity/by k sandeep s...
Sandeep Swamy
 
PPTX
Congenital Hypothyroidism pptx
AneetaSharma15
 
PDF
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
PDF
Wings of Fire Book by Dr. A.P.J Abdul Kalam Full PDF
hetalvaishnav93
 
PDF
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
PDF
Landforms and landscapes data surprise preview
jpinnuck
 
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
PDF
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
PPTX
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
PPTX
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
PPTX
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PDF
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
7.Particulate-Nature-of-Matter.ppt/8th class science curiosity/by k sandeep s...
Sandeep Swamy
 
Congenital Hypothyroidism pptx
AneetaSharma15
 
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
Wings of Fire Book by Dr. A.P.J Abdul Kalam Full PDF
hetalvaishnav93
 
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
Landforms and landscapes data surprise preview
jpinnuck
 
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 

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