SlideShare a Scribd company logo
Java Control Statements
Java compiler executes the code from top to bottom. The statements in the
code are executed according to the order in which they appear. However, Java
provides statements that can be used to control the flow of Java code. Such
statements are called control flow statements. It is one of the fundamental
features of Java, which provides a smooth flow of program.
• Java provides three types of control flow statements.
• Decision Making statements
• if statements
• switch statement
• Loop statements
• do while loop
• while loop
• for loop
• for-each loop
• Jump statements
• break statement
• continue statement
• Decision-Making statements:
As the name suggests, decision-making statements decide which
statement to execute and when. Decision-making statements evaluate
the Boolean expression and control the program flow depending upon
the result of the condition provided. There are two types of decision-
making statements in Java, i.e., If statement and switch statement.
• Simple if statement
• if-else statement
• if-else-if ladder
• Nested if-statement
1) Simple if statement:
It is the most basic statement among all control flow statements in
Java. It evaluates a Boolean expression and enables the program to
enter a block of code if the expression evaluates to true.
Syntax of if statement is given below.
if(condition) {
statement 1; //executes when condition is true
}
if-else statement
The if-else statement is an extension to the if-statement, which uses another
block of code, i.e., else block. The else block is executed if the condition of the
if-block is evaluated as false.
if(condition) {
statement 1; //executes when condition is true
}
else{
statement 2; //executes when condition is false
}
• if-else-if ladder:
• The if-else-if statement contains the if-statement followed by multiple
else-if statements. In other words, we can say that it is the chain of if-
else statements that create a decision tree where the program may
enter in the block of code where the condition is true. We can also
define an else statement at the end of the chain.
Syntax
if(condition 1) {
statement 1; //executes when condition 1 is true
}
else if(condition 2) {
statement 2; //executes when condition 2 is true
}
else {
statement 2; //executes when all the conditions are false
}
• Nested if-statement
In nested if-statements, the if statement can contain a if or if-else statement inside another if or else-if
statement.
if(condition 1) {
statement 1; //executes when condition 1 is true
if(condition 2) {
statement 2; //executes when condition 2 is true
}
else{
If (condition 3)
{
Statement 3;
}
statement 2; //executes when condition 2 is false
}
}
Switch Statement:
In Java, Switch statements are similar to if-else-if statements. The
switch statement contains multiple blocks of code called cases and a
single case is executed based on the variable which is being switched.
The switch statement is easier to use instead of if-else-if statements. It
also enhances the readability of the program.
• Switch-Case is used when we have to make a choice between the
number of alternatives for a given variable.
• Var can be an integer, character, or string in Java.
• Every switch case must contain a default case. The default case is
executed when all the other cases are false.
• Never forget to include the break statement after every switch case
otherwise the switch case will not terminate.
public class Main
{
public static void main(String[] args) {
//System.out.println("Welcome to Online IDE!! Happy Coding :)");
//Specifying month number
int month=7;
String monthString="";
//Switch statement
switch(month){
//case statements within the switch block
case 1: monthString="1 - January";
break;
case 2: monthString="2 - February";
break;
case 3: monthString="3 - March";
break;
case 4: monthString="4 - April";
break;
case 5: monthString="5 - May";
break;
case 6: monthString="6 - June";
break;
case 7: monthString="7 - July";
break;
case 8: monthString="8 - August";
break;
case 9: monthString="9 - September";
break;
case 10: monthString="10 - October";
break;
case 11: monthString="11 - November";
break;
case 12: monthString="12 - December";
break;
default:System.out.println("Invalid Month!");
}
//Printing month of the given number
System.out.println(monthString);
}
}
public class Main
{
public static void main(String[] args) {
// System.out.println("Welcome to Online IDE!! Happy Coding :)");
char ch='D';
switch(ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
System.out.println("Vowel");
break;
default:
System.out.println("Consonant");
}
}
• The case variables can be int, short, byte, char, or enumeration. String type
is also supported since version 7 of Java
• Cases cannot be duplicate
• Default statement is executed when any of the case doesn't match the value
of expression. It is optional.
• Break statement terminates the switch block when the condition is satisfied.
It is optional, if not used, next case is executed.
• While using switch statements, we must notice that the case expression will
be of the same type as the variable. However, it will also be a constant
value.
Loop Statements
In programming, sometimes we need to execute the block of code repeatedly
while some condition evaluates to true. However, loop statements are used
to execute the set of instructions in a repeated order. The execution of the
set of instructions depends upon a particular condition.
In Java, we have three types of loops that execute similarly. However,
there are differences in their syntax and condition checking time.
• for loop
• while loop
• do-while loop
LOOPS in JAVA
• In programming languages, loops are used to execute a particular
statement/set of instructions again and again.
• The execution of the loop starts when some conditions become true.
• For example, print 1 to 1000, print multiplication table of 7, etc.
• Loops make it easy for us to tell the computer that a given set of
instructions need to be executed repeatedly.
While loops :
The while loop in Java is used when we need to execute a block of code
again and again based on a given boolean condition.
Use a while loop if the exact number of iterations is not known.
If the condition never becomes false, the while loop keeps getting
executed. Such a loop is known as an infinite loop.
Syntax
/*
while (Boolean condition)
{
// Statements -> This keeps executing as long as the condition is true.
}
*/
example
public static void main(String[] args) {
System.out.println(1);
System.out.println(2);
System.out.println(3);
System.out.println("Using Loops:");
int i = 100;
while(i<=200){
System.out.println(i);
i++;
}
System.out.println("Finish Running While Loop!");
}
Example 1:
while(true){
// System.out.println("I am an infinite while loop!");
Do-while loop:
Do- while loop is similar to a while loop except for the fact that it is
guaranteed to execute at least once.
Use a do-while loop when the exact number of iterations is unknown,
but you need to execute a code block at least once.
After executing a part of a program for once, the rest of the code gets
executed on the basis of a given boolean condition.
/* do {
//code
} while (condition); //Note this semicolon */
public class Main
{
public static void main(String[] args) {
//System.out.println("Welcome to Online IDE!! Happy Coding :)");
int b = 10;
do {
System.out.println(b);
b++;
}while(b<5);
}
}
For loop:
For loop in java is used to iterate a block of code multiple times.
Use for loop only when the exact number of iterations needed is
already known to you
/* for (initialize; check_bool_expression; update){
//code;
} */
Break statement :
The break statement is used to exit the loop irrespective of whether the
condition is true or false.
Whenever a ‘break’ is encountered inside the loop, the control is sent
outside the loop
Example
public class Main {
public static void main(String[] args) {
//using for loop
for(int i=10;i>0;i--){
if(i==7){
break; //break the loop
}
System.out.println(i);
}
}
}

More Related Content

Similar to controlStatement.pptx, CONTROL STATEMENTS IN JAVA (20)

PPTX
LOOPING STATEMENTS, JAVA,PROGRAMMING LOGIC
DiwakaranM3
 
PPTX
dizital pods session 5-loops.pptx
VijayKumarLokanadam
 
PPT
control-statements....ppt - definition
Papitha7
 
PPT
_Java__Expressions__and__FlowControl.ppt
JyothiAmpally
 
PPT
_Java__Expressions__and__FlowControl.ppt
JyothiAmpally
 
PPTX
Programming Fundamentals in C++ structures
ayshasafdarwaada
 
PDF
Unit 2=Decision Control & Looping Statements.pdf
Dr. Ambedkar Institute of Technology, Bangalore 56
 
PPT
control-statements detailed presentation
gayathripcs
 
PPTX
6.pptx
HarishNayak47
 
DOCX
Chapter 4(1)
TejaswiB4
 
PPTX
Control structures in java
VINOTH R
 
PPTX
Computer programming 2 Lesson 8
MLG College of Learning, Inc
 
PPTX
Final requirement
arjoy_dimaculangan
 
PPTX
presentation on powerpoint template.pptx
farantouqeer8
 
PPTX
Flow of control C ++ By TANUJ
TANUJ ⠀
 
PPT
control-statements, control-statements, control statement
crrpavankumar
 
PPTX
Java Control Statements
KadarkaraiSelvam
 
PPTX
3. Java Installations ppt for engineering
vyshukodumuri
 
PDF
Android Application Development - Level 3
Isham Rashik
 
LOOPING STATEMENTS, JAVA,PROGRAMMING LOGIC
DiwakaranM3
 
dizital pods session 5-loops.pptx
VijayKumarLokanadam
 
control-statements....ppt - definition
Papitha7
 
_Java__Expressions__and__FlowControl.ppt
JyothiAmpally
 
_Java__Expressions__and__FlowControl.ppt
JyothiAmpally
 
Programming Fundamentals in C++ structures
ayshasafdarwaada
 
Unit 2=Decision Control & Looping Statements.pdf
Dr. Ambedkar Institute of Technology, Bangalore 56
 
control-statements detailed presentation
gayathripcs
 
Chapter 4(1)
TejaswiB4
 
Control structures in java
VINOTH R
 
Computer programming 2 Lesson 8
MLG College of Learning, Inc
 
Final requirement
arjoy_dimaculangan
 
presentation on powerpoint template.pptx
farantouqeer8
 
Flow of control C ++ By TANUJ
TANUJ ⠀
 
control-statements, control-statements, control statement
crrpavankumar
 
Java Control Statements
KadarkaraiSelvam
 
3. Java Installations ppt for engineering
vyshukodumuri
 
Android Application Development - Level 3
Isham Rashik
 

Recently uploaded (20)

PPTX
Knowledge Representation : Semantic Networks
Amity University, Patna
 
PDF
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
PPTX
MODULE 05 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
PDF
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
PPTX
Distribution reservoir and service storage pptx
dhanashree78
 
PPTX
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
PDF
aAn_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
PPTX
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
PPTX
MODULE 03 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
PDF
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
PDF
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
PPT
New_school_Engineering_presentation_011707.ppt
VinayKumar304579
 
PPTX
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
PDF
3rd International Conference on Machine Learning and IoT (MLIoT 2025)
ClaraZara1
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
PPTX
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
PPTX
Final Major project a b c d e f g h i j k l m
bharathpsnab
 
PPTX
Introduction to Internal Combustion Engines - Types, Working and Camparison.pptx
UtkarshPatil98
 
PDF
Data structures notes for unit 2 in computer science.pdf
sshubhamsingh265
 
PPTX
Water Resources Engineering (CVE 728)--Slide 3.pptx
mohammedado3
 
Knowledge Representation : Semantic Networks
Amity University, Patna
 
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
MODULE 05 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
Distribution reservoir and service storage pptx
dhanashree78
 
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
aAn_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
MODULE 03 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
New_school_Engineering_presentation_011707.ppt
VinayKumar304579
 
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
3rd International Conference on Machine Learning and IoT (MLIoT 2025)
ClaraZara1
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
Final Major project a b c d e f g h i j k l m
bharathpsnab
 
Introduction to Internal Combustion Engines - Types, Working and Camparison.pptx
UtkarshPatil98
 
Data structures notes for unit 2 in computer science.pdf
sshubhamsingh265
 
Water Resources Engineering (CVE 728)--Slide 3.pptx
mohammedado3
 
Ad

controlStatement.pptx, CONTROL STATEMENTS IN JAVA

  • 2. Java compiler executes the code from top to bottom. The statements in the code are executed according to the order in which they appear. However, Java provides statements that can be used to control the flow of Java code. Such statements are called control flow statements. It is one of the fundamental features of Java, which provides a smooth flow of program. • Java provides three types of control flow statements. • Decision Making statements • if statements • switch statement • Loop statements • do while loop • while loop • for loop • for-each loop • Jump statements • break statement • continue statement
  • 3. • Decision-Making statements: As the name suggests, decision-making statements decide which statement to execute and when. Decision-making statements evaluate the Boolean expression and control the program flow depending upon the result of the condition provided. There are two types of decision- making statements in Java, i.e., If statement and switch statement.
  • 4. • Simple if statement • if-else statement • if-else-if ladder • Nested if-statement
  • 5. 1) Simple if statement: It is the most basic statement among all control flow statements in Java. It evaluates a Boolean expression and enables the program to enter a block of code if the expression evaluates to true. Syntax of if statement is given below. if(condition) { statement 1; //executes when condition is true }
  • 6. if-else statement The if-else statement is an extension to the if-statement, which uses another block of code, i.e., else block. The else block is executed if the condition of the if-block is evaluated as false. if(condition) { statement 1; //executes when condition is true } else{ statement 2; //executes when condition is false }
  • 7. • if-else-if ladder: • The if-else-if statement contains the if-statement followed by multiple else-if statements. In other words, we can say that it is the chain of if- else statements that create a decision tree where the program may enter in the block of code where the condition is true. We can also define an else statement at the end of the chain.
  • 8. Syntax if(condition 1) { statement 1; //executes when condition 1 is true } else if(condition 2) { statement 2; //executes when condition 2 is true } else { statement 2; //executes when all the conditions are false }
  • 9. • Nested if-statement In nested if-statements, the if statement can contain a if or if-else statement inside another if or else-if statement. if(condition 1) { statement 1; //executes when condition 1 is true if(condition 2) { statement 2; //executes when condition 2 is true } else{ If (condition 3) { Statement 3; } statement 2; //executes when condition 2 is false } }
  • 10. Switch Statement: In Java, Switch statements are similar to if-else-if statements. The switch statement contains multiple blocks of code called cases and a single case is executed based on the variable which is being switched. The switch statement is easier to use instead of if-else-if statements. It also enhances the readability of the program.
  • 11. • Switch-Case is used when we have to make a choice between the number of alternatives for a given variable. • Var can be an integer, character, or string in Java. • Every switch case must contain a default case. The default case is executed when all the other cases are false. • Never forget to include the break statement after every switch case otherwise the switch case will not terminate.
  • 12. public class Main { public static void main(String[] args) { //System.out.println("Welcome to Online IDE!! Happy Coding :)"); //Specifying month number int month=7; String monthString=""; //Switch statement switch(month){ //case statements within the switch block case 1: monthString="1 - January"; break; case 2: monthString="2 - February"; break; case 3: monthString="3 - March"; break;
  • 13. case 4: monthString="4 - April"; break; case 5: monthString="5 - May"; break; case 6: monthString="6 - June"; break; case 7: monthString="7 - July"; break; case 8: monthString="8 - August"; break; case 9: monthString="9 - September"; break; case 10: monthString="10 - October"; break; case 11: monthString="11 - November"; break; case 12: monthString="12 - December"; break; default:System.out.println("Invalid Month!"); } //Printing month of the given number System.out.println(monthString); } }
  • 14. public class Main { public static void main(String[] args) { // System.out.println("Welcome to Online IDE!! Happy Coding :)"); char ch='D'; switch(ch) { case 'a': case 'e': case 'i': case 'o': case 'u': case 'A': case 'E': case 'I': case 'O': case 'U': System.out.println("Vowel"); break; default: System.out.println("Consonant"); } }
  • 15. • The case variables can be int, short, byte, char, or enumeration. String type is also supported since version 7 of Java • Cases cannot be duplicate • Default statement is executed when any of the case doesn't match the value of expression. It is optional. • Break statement terminates the switch block when the condition is satisfied. It is optional, if not used, next case is executed. • While using switch statements, we must notice that the case expression will be of the same type as the variable. However, it will also be a constant value.
  • 16. Loop Statements In programming, sometimes we need to execute the block of code repeatedly while some condition evaluates to true. However, loop statements are used to execute the set of instructions in a repeated order. The execution of the set of instructions depends upon a particular condition. In Java, we have three types of loops that execute similarly. However, there are differences in their syntax and condition checking time. • for loop • while loop • do-while loop
  • 17. LOOPS in JAVA • In programming languages, loops are used to execute a particular statement/set of instructions again and again. • The execution of the loop starts when some conditions become true. • For example, print 1 to 1000, print multiplication table of 7, etc. • Loops make it easy for us to tell the computer that a given set of instructions need to be executed repeatedly.
  • 18. While loops : The while loop in Java is used when we need to execute a block of code again and again based on a given boolean condition. Use a while loop if the exact number of iterations is not known. If the condition never becomes false, the while loop keeps getting executed. Such a loop is known as an infinite loop.
  • 19. Syntax /* while (Boolean condition) { // Statements -> This keeps executing as long as the condition is true. } */
  • 20. example public static void main(String[] args) { System.out.println(1); System.out.println(2); System.out.println(3); System.out.println("Using Loops:"); int i = 100; while(i<=200){ System.out.println(i); i++; } System.out.println("Finish Running While Loop!"); }
  • 21. Example 1: while(true){ // System.out.println("I am an infinite while loop!");
  • 22. Do-while loop: Do- while loop is similar to a while loop except for the fact that it is guaranteed to execute at least once. Use a do-while loop when the exact number of iterations is unknown, but you need to execute a code block at least once. After executing a part of a program for once, the rest of the code gets executed on the basis of a given boolean condition.
  • 23. /* do { //code } while (condition); //Note this semicolon */
  • 24. public class Main { public static void main(String[] args) { //System.out.println("Welcome to Online IDE!! Happy Coding :)"); int b = 10; do { System.out.println(b); b++; }while(b<5); } }
  • 25. For loop: For loop in java is used to iterate a block of code multiple times. Use for loop only when the exact number of iterations needed is already known to you /* for (initialize; check_bool_expression; update){ //code; } */
  • 26. Break statement : The break statement is used to exit the loop irrespective of whether the condition is true or false. Whenever a ‘break’ is encountered inside the loop, the control is sent outside the loop
  • 27. Example public class Main { public static void main(String[] args) { //using for loop for(int i=10;i>0;i--){ if(i==7){ break; //break the loop } System.out.println(i); } } }