SlideShare a Scribd company logo
Control structures in java
 The control statement are used to control the flow of
execution of the program.
 This execution order depends on the supplied data values
and the conditional logic.
 In java program, control structure is can divide in three
parts:
1. Selection statement
2. Iteration statement
3. Jumps in statement
Control structures in java
Control structures in java
• Selection statement is also called as Decision making
statements.
• Because it provides the decision making capabilities to the
statements.
• In selection statement, there are two types:
1. if statement
2. Switch statement.
Control structures in java
• The Java if statement is used to test the condition.
• It checks Boolean condition: true or false.
• There are various types of if statement in java.
1. if statement
2. if-else statement
3. Nested if-else statement
4. if-else-if ladder
• The Java if statement tests the condition.
• It executes the if block if condition is true.
Syntax:
if(condition)
{
//code to be executed
}
Example:
public class IfExample
{
public static void main(String[] arg)
{
int age=20;
if(age>18)
{
System.out.print("Age is greater than 18");
}
}}
Output: Age is greater than 18
• The Java if-else statement also tests the condition.
• It executes the if block if condition is true otherwise else
block is executed.
Syntax:
if(condition)
{
//code if condition is true
}
Else
{
//code if condition is false
}
public class IfElseExample
{
public static void main(String[] args)
{
int number=13;
if(number%2==0)
{
System.out.println("even number");
}
Else
{
System.out.println("odd number");
} }}
Output: odd number
• The Nested if-else statement executes one if or else if
statement inside another if or else if statement.
Syntax:
if(Boolean_expression 1)
{
// Executes when the Boolean expression 1 is true
if(Boolean_expression 2)
{
// Executes when the Boolean expression 2 is true
}
}
Control structures in java
public class Test
{
public static void main(String args[])
{
int x = 30;
int y = 10;
if( x == 30 )
{
if( y == 10 )
{
System.out.print("X = 30 and Y = 10");
}
}}}
• The if-else-if ladder statement executes one condition from multiple
statements.
Syntax:
if(condition1)
{
//code to be executed if condition1 is true
}
else if(condition2)
{
//code to be executed if condition2 is true
}
else if(condition3)
{
//code to be executed if condition3 is true
}
...
Else
{
//code to be executed if all the conditions are false
}
Control structures in java
public class IfElseIfExample
{
public static void main(String[] args)
{
int marks=65;
if(marks<50)
{
System.out.println("fail");
}
else if(marks>=50 && marks<60)
{
System.out.println("D grade");
}
else if(marks>=60 && marks<70)
{
System.out.println("C grade");
}
else if(marks>=70 && marks<80)
{
System.out.println("B grade");
}
else if(marks>=80 && marks<90)
{
System.out.println("A grade");
}
else if(marks>=90 && marks<100)
{
System.out.println("A+ grade");
}
else
{
System.out.println("Invalid!");
}}}
Output: C grade
Control structures in java
• The Java switch statement executes one statement from
multiple conditions.
• It is like if-else-if ladder statement.
Syntax:
switch(expression)
{
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
......
default:
code to be executed if all cases are not matched;
}
Control structures in java
public class SwitchExample
{
public static void main(String[] args)
{
int number=20;
switch(number)
{
case 10: System.out.println("10");break;
case 20: System.out.println("20");break;
case 30: System.out.println("30");break;
default:System.out.println("Not in 10, 20 or 30");
}}}
Output: 20
Control structures in java
• Looping is also called as iterations.
• The process of repeatedly executing a statements and is
called as looping.
• The statements may be executed multiple times.
• If a loop executing continuous then it is called as Infinite
loop.
• In Iteration statement, there are three types of operation:
1. for loop
2. while loop
3. do-while loop
Control structures in java
• The simple for loop is same as C/C++.
• We can initialize variable, check condition
and increment/decrement value.
Syntax:
for(initialization;condition;incr/decr)
{
//code to be executed
}
public class ForExample
{
public static void main(String[] args)
{
for(int i=1;i<=10;i++)
{
System.out.print(i);
}
}
}
Output:
12345678910
Control structures in java
• The Java while loop is used to iterate a part of the program
several times.
• If the number of iteration is not fixed, it is recommended
to use while loop.
Syntax:
while(condition)
{
//code to be executed
}
public class WhileExample
{
public static void main(String[] args)
{
int i=1;
while(i<=10)
{
System.out.println(i);
i++;
}
} }
Output:
12345678910
Control structures in java
• The Java do-while loop is used to iterate a part of the program
several times.
• If the number of iteration is not fixed and you must have to
execute the loop at least once, it is recommended to use do-
while loop.
• The Java do-while loop is executed at least once because
Condition is checked after loop body.
Syntax:
Do
{
//code to be executed
}
while(condition);
public class DoWhileExample
{
public static void main(String[] args)
{
int i=1;
do
{
System.out.print(i);
i++;
}
while(i<=8);
}
}
Output:
12345678

More Related Content

What's hot (20)

PPTX
Java Tokens
Madishetty Prathibha
 
PPTX
Java string handling
Salman Khan
 
PPTX
Constructor in java
Pavith Gunasekara
 
PDF
Java Thread Synchronization
Benj Del Mundo
 
PPT
Exception Handling in JAVA
SURIT DATTA
 
PPTX
MULTI THREADING IN JAVA
VINOTH R
 
PPTX
Type casting in java
Farooq Baloch
 
PDF
Arrays in Java
Naz Abdalla
 
PPTX
Java abstract class & abstract methods
Shubham Dwivedi
 
PPT
Looping statements in Java
Jin Castor
 
PDF
Java conditional statements
Kuppusamy P
 
PPTX
Loop(for, while, do while) condition Presentation
Badrul Alam
 
PPTX
Java
Tony Nguyen
 
PPTX
JAVA AWT
shanmuga rajan
 
PPTX
Applets in java
Wani Zahoor
 
PPTX
File handling
priya_trehan
 
PPTX
Arrays in Java
Abhilash Nair
 
PDF
Methods in Java
Jussi Pohjolainen
 
PPS
Java Exception handling
kamal kotecha
 
PPTX
Nested loops
Neeru Mittal
 
Java string handling
Salman Khan
 
Constructor in java
Pavith Gunasekara
 
Java Thread Synchronization
Benj Del Mundo
 
Exception Handling in JAVA
SURIT DATTA
 
MULTI THREADING IN JAVA
VINOTH R
 
Type casting in java
Farooq Baloch
 
Arrays in Java
Naz Abdalla
 
Java abstract class & abstract methods
Shubham Dwivedi
 
Looping statements in Java
Jin Castor
 
Java conditional statements
Kuppusamy P
 
Loop(for, while, do while) condition Presentation
Badrul Alam
 
JAVA AWT
shanmuga rajan
 
Applets in java
Wani Zahoor
 
File handling
priya_trehan
 
Arrays in Java
Abhilash Nair
 
Methods in Java
Jussi Pohjolainen
 
Java Exception handling
kamal kotecha
 
Nested loops
Neeru Mittal
 

Similar to Control structures in java (20)

PPTX
controlStatement.pptx, CONTROL STATEMENTS IN JAVA
DrNeetuSharma5
 
PPTX
Lecture - 5 Control Statement
manish kumar
 
PPTX
Java learning
moew3
 
PPTX
Unit-02 Selection, Mathematical Functions and loops.pptx
jessicafalcao1
 
PPT
Control statements
CutyChhaya
 
PPTX
Loop
prabhat kumar
 
PPTX
Unit- 1 -Java - Decision Making . pptx
CmDept
 
PPT
Control statements
raksharao
 
PPTX
6.pptx
HarishNayak47
 
PPTX
Java chapter 3
Abdii Rashid
 
PDF
java notes.pdf
RajkumarHarishchandr1
 
PPTX
Programming in java - Concepts- Operators- Control statements-Expressions
LovelitJose
 
PPTX
Java Decision Control
Jayfee Ramos
 
PPTX
control statements
Azeem Sultan
 
PPTX
3. Java Installations ppt for engineering
vyshukodumuri
 
PPTX
Decision_Makoiyuhrttouehgouesygytiuojth0ueyutwaeing.pptx
pratikdagar100
 
PPT
_Java__Expressions__and__FlowControl.ppt
JyothiAmpally
 
PPTX
Flow of control by deepak lakhlan
Deepak Lakhlan
 
PPTX
Java Control Statement Control Statement.pptx
changepass
 
PPTX
Control structures
Gehad Enayat
 
controlStatement.pptx, CONTROL STATEMENTS IN JAVA
DrNeetuSharma5
 
Lecture - 5 Control Statement
manish kumar
 
Java learning
moew3
 
Unit-02 Selection, Mathematical Functions and loops.pptx
jessicafalcao1
 
Control statements
CutyChhaya
 
Unit- 1 -Java - Decision Making . pptx
CmDept
 
Control statements
raksharao
 
Java chapter 3
Abdii Rashid
 
java notes.pdf
RajkumarHarishchandr1
 
Programming in java - Concepts- Operators- Control statements-Expressions
LovelitJose
 
Java Decision Control
Jayfee Ramos
 
control statements
Azeem Sultan
 
3. Java Installations ppt for engineering
vyshukodumuri
 
Decision_Makoiyuhrttouehgouesygytiuojth0ueyutwaeing.pptx
pratikdagar100
 
_Java__Expressions__and__FlowControl.ppt
JyothiAmpally
 
Flow of control by deepak lakhlan
Deepak Lakhlan
 
Java Control Statement Control Statement.pptx
changepass
 
Control structures
Gehad Enayat
 
Ad

Recently uploaded (20)

PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PPTX
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
PPTX
SD_GMRC5_Session 6AB_Dulog Pedagohikal at Pagtataya (1).pptx
NickeyArguelles
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PDF
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PDF
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PDF
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
PPTX
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
PDF
epi editorial commitee meeting presentation
MIPLM
 
PPTX
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
PPTX
DAY 1_QUARTER1 ENGLISH 5 WEEK- PRESENTATION.pptx
BanyMacalintal
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PDF
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
PPTX
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
PPTX
Introduction to Indian Writing in English
Trushali Dodiya
 
PPTX
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
PPTX
ENG8_Q1_WEEK2_LESSON1. Presentation pptx
marawehsvinetshe
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
SD_GMRC5_Session 6AB_Dulog Pedagohikal at Pagtataya (1).pptx
NickeyArguelles
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
epi editorial commitee meeting presentation
MIPLM
 
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
DAY 1_QUARTER1 ENGLISH 5 WEEK- PRESENTATION.pptx
BanyMacalintal
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
Introduction to Indian Writing in English
Trushali Dodiya
 
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
ENG8_Q1_WEEK2_LESSON1. Presentation pptx
marawehsvinetshe
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
Ad

Control structures in java

  • 2.  The control statement are used to control the flow of execution of the program.  This execution order depends on the supplied data values and the conditional logic.  In java program, control structure is can divide in three parts: 1. Selection statement 2. Iteration statement 3. Jumps in statement
  • 5. • Selection statement is also called as Decision making statements. • Because it provides the decision making capabilities to the statements. • In selection statement, there are two types: 1. if statement 2. Switch statement.
  • 7. • The Java if statement is used to test the condition. • It checks Boolean condition: true or false. • There are various types of if statement in java. 1. if statement 2. if-else statement 3. Nested if-else statement 4. if-else-if ladder
  • 8. • The Java if statement tests the condition. • It executes the if block if condition is true. Syntax: if(condition) { //code to be executed }
  • 9. Example: public class IfExample { public static void main(String[] arg) { int age=20; if(age>18) { System.out.print("Age is greater than 18"); } }} Output: Age is greater than 18
  • 10. • The Java if-else statement also tests the condition. • It executes the if block if condition is true otherwise else block is executed. Syntax: if(condition) { //code if condition is true } Else { //code if condition is false }
  • 11. public class IfElseExample { public static void main(String[] args) { int number=13; if(number%2==0) { System.out.println("even number"); } Else { System.out.println("odd number"); } }} Output: odd number
  • 12. • The Nested if-else statement executes one if or else if statement inside another if or else if statement. Syntax: if(Boolean_expression 1) { // Executes when the Boolean expression 1 is true if(Boolean_expression 2) { // Executes when the Boolean expression 2 is true } }
  • 14. public class Test { public static void main(String args[]) { int x = 30; int y = 10; if( x == 30 ) { if( y == 10 ) { System.out.print("X = 30 and Y = 10"); } }}}
  • 15. • The if-else-if ladder statement executes one condition from multiple statements. Syntax: if(condition1) { //code to be executed if condition1 is true } else if(condition2) { //code to be executed if condition2 is true } else if(condition3) { //code to be executed if condition3 is true } ... Else { //code to be executed if all the conditions are false }
  • 17. public class IfElseIfExample { public static void main(String[] args) { int marks=65; if(marks<50) { System.out.println("fail"); } else if(marks>=50 && marks<60) { System.out.println("D grade"); } else if(marks>=60 && marks<70) { System.out.println("C grade"); } else if(marks>=70 && marks<80) { System.out.println("B grade"); } else if(marks>=80 && marks<90) { System.out.println("A grade"); } else if(marks>=90 && marks<100) { System.out.println("A+ grade"); } else { System.out.println("Invalid!"); }}} Output: C grade
  • 19. • The Java switch statement executes one statement from multiple conditions. • It is like if-else-if ladder statement. Syntax: switch(expression) { case value1: //code to be executed; break; //optional case value2: //code to be executed; break; //optional ...... default: code to be executed if all cases are not matched; }
  • 21. public class SwitchExample { public static void main(String[] args) { int number=20; switch(number) { case 10: System.out.println("10");break; case 20: System.out.println("20");break; case 30: System.out.println("30");break; default:System.out.println("Not in 10, 20 or 30"); }}} Output: 20
  • 23. • Looping is also called as iterations. • The process of repeatedly executing a statements and is called as looping. • The statements may be executed multiple times. • If a loop executing continuous then it is called as Infinite loop. • In Iteration statement, there are three types of operation: 1. for loop 2. while loop 3. do-while loop
  • 25. • The simple for loop is same as C/C++. • We can initialize variable, check condition and increment/decrement value. Syntax: for(initialization;condition;incr/decr) { //code to be executed }
  • 26. public class ForExample { public static void main(String[] args) { for(int i=1;i<=10;i++) { System.out.print(i); } } } Output: 12345678910
  • 28. • The Java while loop is used to iterate a part of the program several times. • If the number of iteration is not fixed, it is recommended to use while loop. Syntax: while(condition) { //code to be executed }
  • 29. public class WhileExample { public static void main(String[] args) { int i=1; while(i<=10) { System.out.println(i); i++; } } } Output: 12345678910
  • 31. • The Java do-while loop is used to iterate a part of the program several times. • If the number of iteration is not fixed and you must have to execute the loop at least once, it is recommended to use do- while loop. • The Java do-while loop is executed at least once because Condition is checked after loop body. Syntax: Do { //code to be executed } while(condition);
  • 32. public class DoWhileExample { public static void main(String[] args) { int i=1; do { System.out.print(i); i++; } while(i<=8); } } Output: 12345678