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

What's hot

Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
Naz Abdalla
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
arvind pandey
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
SIVASHANKARIRAJAN
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
BHUVIJAYAVELU
 
Data Types, Variables, and Operators
Data Types, Variables, and OperatorsData Types, Variables, and Operators
Data Types, Variables, and Operators
Marwa Ali Eissa
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
Madishetty Prathibha
 
Control structures in java
Control structures in javaControl structures in java
Control structures in java
VINOTH R
 
Type casting in java
Type casting in javaType casting in java
Type casting in java
Farooq Baloch
 
Method overloading
Method overloadingMethod overloading
Method overloading
Lovely Professional University
 
Java variable types
Java variable typesJava variable types
Java variable types
Soba Arjun
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
Madishetty Prathibha
 
Operators in C++
Operators in C++Operators in C++
Operators in C++
Sachin Sharma
 
Data types in java
Data types in javaData types in java
Data types in java
HarshitaAshwani
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
Tech_MX
 
Java operators
Java operatorsJava operators
Java operators
Shehrevar Davierwala
 
Operators in java presentation
Operators in java presentationOperators in java presentation
Operators in java presentation
kunal kishore
 
Strings in c#
Strings in c#Strings in c#
Strings in c#
Dr.Neeraj Kumar Pandey
 
An Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: ArraysAn Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: Arrays
Martin Chapman
 
String in java
String in javaString in java
Operators and Expressions in Java
Operators and Expressions in JavaOperators and Expressions in Java
Operators and Expressions in Java
Abhilash Nair
 

What's hot (20)

Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Data Types, Variables, and Operators
Data Types, Variables, and OperatorsData Types, Variables, and Operators
Data Types, Variables, and Operators
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
 
Control structures in java
Control structures in javaControl structures in java
Control structures in java
 
Type casting in java
Type casting in javaType casting in java
Type casting in java
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Java variable types
Java variable typesJava variable types
Java variable types
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
 
Operators in C++
Operators in C++Operators in C++
Operators in C++
 
Data types in java
Data types in javaData types in java
Data types in java
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Java operators
Java operatorsJava operators
Java operators
 
Operators in java presentation
Operators in java presentationOperators in java presentation
Operators in java presentation
 
Strings in c#
Strings in c#Strings in c#
Strings in c#
 
An Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: ArraysAn Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: Arrays
 
String in java
String in javaString in java
String in java
 
Operators and Expressions in Java
Operators and Expressions in JavaOperators and Expressions in Java
Operators and Expressions 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_selection
alish 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_selection
alish 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 example
umaghosal12101974
 
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
ManojKhadilkar1
 
3. control statements
3. control statements3. control statements
3. control statements
amar kakde
 
M C6java5
M C6java5M C6java5
M C6java5
mbruggen
 
Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programming
nmahi96
 
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
LovelitJose
 
C language UPTU Unit3 Slides
C language UPTU Unit3 SlidesC language UPTU Unit3 Slides
C language UPTU Unit3 Slides
Rakesh Roshan
 
control statements
control statementscontrol statements
control statements
Azeem 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.1
sotlsoc
 
Data structures
Data structuresData structures
Data structures
Khalid Bana
 
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 Statement
manish kumar
 
Controls & Loops in C
Controls & Loops in C Controls & Loops in C
Controls & Loops in C
Thesis Scientist Private Limited
 
Operators, control statements represented in java
Operators, control statements  represented in javaOperators, control statements  represented in java
Operators, control statements represented in java
TharuniDiddekunta
 
Selection Control Structures
Selection Control StructuresSelection Control Structures
Selection Control Structures
PRN USM
 
Lect 3-4 Zaheer Abbas
Lect 3-4 Zaheer AbbasLect 3-4 Zaheer Abbas
Lect 3-4 Zaheer Abbas
Information Technology Center
 
Decision Making and Branching
Decision Making and BranchingDecision Making and Branching
Decision Making and Branching
Munazza-Mah-Jabeen
 

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

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

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

C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5
sayalidalavi006
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
paigestewart1632
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
TechSoup
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
NgcHiNguyn25
 

Recently uploaded (20)

C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
 

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