SlideShare a Scribd company logo
Control Flow Statements
Branching Statements
Looping Statements
Jump Statemetns
Branching Statements
• If statement
• If else Statement
• If..else…if statement
• Nest if…else statement
• Switch Statement
<<goTo first slide>>
If Statement
• An if statement consists of a Boolean expression followed by one or more
statements. If the Boolean expression evaluates to true then the block of
code inside the if statement will be executed. If not the first set of code
after the end of the if statement (after the closing curly brace) will be
executed.
if(Boolean expression)
{
//Some code should be work
}
public static void main(String[] y)
{
int my_int=25;
if(my_int==25)
{
System.out.println("the condition is true");
}
}
Go to branch statement slide
If else Statement
• An if statement can be followed by an optional else statement,
which executes when the Boolean expression is false.
if(Boolean expression)
{ //some code excut }
else
{ //some code excut }
public static void main(String[] y)
{
int my_int=25;
if(my_int<25)
{ System.out.println("the condition is true"); }
else
{ System.out.println("the condition is not true"); }
}
• Go to branch statement slide
If…else…if statement
• An if statement can be followed by an optional else if...else statement,
which is very useful to test various conditions using single if...else if
statement.
When using if , else if , else statements there are few points to keep in mind.
• An if can have zero or one else's and it must come after any else if's.
• An if can have zero to many else if's and they must come before the else.
• Once an else if succeeds, none of the remaining else if's or else's will be
tested.
if(Boolean Expression 1)
{ //some code }
else if (Boolean Expression 2)
{ //some code }
else if (Boolean Expression 2)
{ //some code }
else
{ //some code }
public static void main(String[] y)
{
int my_int=25;
if(my_int<25)
{
System.out.println("the condition is true");
}
else if(my_int>25)
{System.out.println("the else if condition is true for 25 >
25");
}
else if(my_int==25)
{
System.out.println("the condition is true for 25==25");
}
else
{
System.out.println("any of the condition is not ture");
}
}
Go to branch statement slides
Nest if…else Statement
• It is always legal to nest if-else statements which means you can use one if
or else if statement inside another if or else if statement.
if(boolean expression 1)
{
if(boolean expression 2)
{ //some code }
else
{ //some code }
}
public static void main(String[] y)
{
int course_value=90;
String course_name="java";
String Course_type="core & advanced";
if(course_value==90)
{
if(course_name=="java")
{
if(Course_type=="core & advanced")
{
System.out.println("the nested condition is
true");
}
}
}
else
{
System.out.println("the nested conditions are not true");
}
}
Go to Branch statement slide
Switch Statement
• Switch case statements are a substitute for long if statements that
compare a variable to several "integral" values.. The value of the variable
given into switch is compared to the value following each of the cases, and
when one value matches the value of the variable, the computer
continues executing the program from that point.
switch(expression)
{
case value1: {//some code}break;
case value2: {//some code}break;
default: {//some code}break;
}
public static void main(String[] y)
{
int x=10;
/*
* x value is compared with case values
*/
switch(x)
{
case 10:{System.out.println("the x value is 10");}break;
case 30:{System.out.println("the x value is 30");}break;
case 40:{System.out.println("the x value is 40");}break;
}
}
Go to branch statement slide
LOOP control Statements
• loop is a sequence of instruction s that is continually repeated until a
certain condition is reached.
There are four types of loops:
• For loop
• For each loop
• While loop
• Do..While loop
<<goto first slide>>
For loop
• The for is an entry-entrolled loop and is used when an action is to
repeated for a predetermined number of times
for(initial value; test condition; increment)
{ //some code }
public static void main(String[] y)
{
for(int i=0; i<5; i++)
{System.out.println("hello world "+i);}
}
Go to loop slide
Foreach Statement
• A ForEach style loop is designed to cycle through a collection of
objects,such as an array, in strictly sequential fashion, from start to
finish.Implement a foreach loop by using the keyword foreach, java adds
the foreach compability by enhancing the for statement.
• for(type itr-var:collection)statement-block
int[] a={1,2,3,4};
for(int x:a)
{
System.out.println("array of a["+x+"] "+a[x]);
}
Go to loop slide
While loop(Entry Control loop)
• It repeates a statement or block while its controlling expression(any boolean
Expression) is true
• The body of the loop will be executed as long as the conditional expression is
true.
while(condition)
{
//body of code
}
public static void main(String[] y)
{
while(x<5)
{ /*x++ increment value of x;*/
System.out.println("the value of x is "+x);
x++;
}
}
Go to loop slide
Do-while loop
• A while loop is initially false then the body of the loop will not be executed at
all .The do-while loop always executes its body at least once because its
conditional expression is at the bottom of the loop
do
{ //body of code
}while(boolean expression);
System.out.println("the do While loop");
int x=1;
do
{
/*x++ increment value of x;
*/
System.out.println("the value of x is "+x);
x++;
}while(x<5);
Go to loop slided
Jump Statements
• Break
• Continue
• Return
<<goto first slide>>
Jump Statements
• Break
• In java the break statement has 3 uses
• Terminates a statement sequence in a switch statement
• It can be used to exit loop
• It can be used more civilized form of goto statement
• Using Break to exit the loop
By using break, you can force immediate termination of a loop .when break
statement is encountered inside a loop the loop is terminated and
program control resumes at the next statement following loop
public static void main(String[] y)
{
for(int i=0;i<10;i++)
{
if(i==5)
{
System.out.println("condition break");
break;
}
System.out.println("the value of i is::"+i);
}
}
• Using break in the form of GOTO
• Syntax Break label_name;
public static void main(String[] y)
{
first:{
secound:{
third:{
System.out.println("third block");
if(true)
{
break secound;
}
}
System.out.println("secound block");
}
System.out.println("first block");
}
}
go to jump statements
Continue
• A continue statement causes control to be transferred directly to the
conditional expression that controls the loop
public static void main(String[] y)
{
for(int i=0;i<5;i++)
{
if(true)
{
System.out.println("the if block");
continue;
}
System.out.println("the for loop");
}
}
Using continue with labels
• Specify the labels with continue statement
public static void main(String[] y)
{
outer:for(int i=0;i<=2;i++){
for(int j=0;j<=1;j++)
{
System.out.println("value of J is::"+j);
continue outer;
}
System.out.println("the value of i is::"+i);
}
/*
for continue labels it should be used with loops only not for blocks
*/
}
Go to jump statements
Return
• The return statement is used to explicitly return from a method.That is,it
causes program control to transfer back to the caller of the method.At any
time in a method the return statement can be used to cause execution to
branch back to the caller of the method.Thus,the return statement can be
used to cause execution to branch back to the caller ot the method.Thus, the
return statement immediately terminates the method in which it is executed.
public static void main(String[] y)
{
System.out.println("before return statement");
if(true)
{
return;
}
System.out.println("after return statement");
/*
here return causes execution to return to the java run-time system
*/
}
class Demo_break
{
int demo()
{
return(1);
}
public static void main(String[] y)
{
Demo_break d=new Demo_break();
System.out.println("the return value
is"+d.demo());
}
}
• goto jump statement
• Find the errors in the following
1. If(x+y=z&&y>0)
2. If(x>0);
A=b+c;
Else
A=a*b;
3. If(x<0)||(q<0)
Class ifelse
{ int result=55; char grade;
if(result>90) grade=‘A’;
else if(result>80)grade=“B”;
else if(result>70) grade=“c”;
else grade=“F”;
else grade=“g”;
System.out.println(“the grade is “+grade);
}
See the output
• Correct the code to print the output “correct
value”
public static void main(String[] args) {
int i=2;
if(i=2)
{
System.out.println("correct value");
}
else
{
System.out.println("not correct value");
}
Control flow statements in java

More Related Content

PPTX
Control Statements in Java
PPSX
Data Types & Variables in JAVA
PPTX
Threads & Concurrency
PPTX
Basic Concepts of OOPs (Object Oriented Programming in Java)
PDF
Introduction To Computer
PPTX
Python dictionary
PDF
Python tuple
PPTX
Control Statements in Java
Data Types & Variables in JAVA
Threads & Concurrency
Basic Concepts of OOPs (Object Oriented Programming in Java)
Introduction To Computer
Python dictionary
Python tuple

What's hot (20)

PPTX
Classes, objects in JAVA
PPTX
Java abstract class & abstract methods
PPTX
Control statements in java
PPT
Exception Handling in JAVA
PPTX
Classes objects in java
PPS
Interface
PDF
Java conditional statements
PPT
Java-java virtual machine
PDF
Methods in Java
PPT
Java Servlets
PPT
Java static keyword
PPTX
Type casting in java
PPTX
Methods in java
PPT
Final keyword in java
PPTX
Interface in java
PPSX
Exception Handling
PPT
Abstract class in java
PPTX
Operators in java
PPTX
Classes, objects in JAVA
Java abstract class & abstract methods
Control statements in java
Exception Handling in JAVA
Classes objects in java
Interface
Java conditional statements
Java-java virtual machine
Methods in Java
Java Servlets
Java static keyword
Type casting in java
Methods in java
Final keyword in java
Interface in java
Exception Handling
Abstract class in java
Operators in java
Ad

Viewers also liked (20)

PPTX
Switch statement, break statement, go to statement
PPT
Strings Arrays
PPT
4.1 sequentioal search
PPTX
PPTX
Byte array to hex string transformer
PPSX
Break and continue statement in C
PDF
5 2. string processing
PDF
Design, Simulation and Verification of Generalized Photovoltaic cells Model U...
PDF
Java Programming - 04 object oriented in java
PPTX
Switch case and looping statement
PPT
algorithm
PPTX
TypeScript by Howard
PDF
Multi string PV array
PDF
10. switch case
PDF
Java Programming - 03 java control flow
PPTX
java programming- control statements
PDF
Java 8 new features or the ones you might actually use
PPTX
Nomenclature of Organic Compounds (IUPAC)
PPTX
Epics and User Stories
Switch statement, break statement, go to statement
Strings Arrays
4.1 sequentioal search
Byte array to hex string transformer
Break and continue statement in C
5 2. string processing
Design, Simulation and Verification of Generalized Photovoltaic cells Model U...
Java Programming - 04 object oriented in java
Switch case and looping statement
algorithm
TypeScript by Howard
Multi string PV array
10. switch case
Java Programming - 03 java control flow
java programming- control statements
Java 8 new features or the ones you might actually use
Nomenclature of Organic Compounds (IUPAC)
Epics and User Stories
Ad

Similar to Control flow statements in java (20)

PPTX
Lecture - 5 Control Statement
PPT
Control statements
PPTX
Object oriented programming_Unit1_contro statements.pptx
PDF
how to write loops in java explained vividly
PPT
Repetition Structure
DOCX
Java loops
PDF
Control flow statements in java web applications
PPTX
Unit-02 Selection, Mathematical Functions and loops.pptx
PPT
C Sharp Jn (3)
PPTX
Control structures in java
PPTX
Core java
PPT
Loops
PPTX
DAY_1.2.pptx
PPTX
130706266060138191
PPT
6_A1944859510_21789_2_2018_06. Branching Statements.ppt
PPT
M C6java5
PPTX
07 flow control
PPTX
Module 5 : Statements & Exceptions
PPTX
JPC#8 Introduction to Java Programming
PPTX
Programming in java - Concepts- Operators- Control statements-Expressions
Lecture - 5 Control Statement
Control statements
Object oriented programming_Unit1_contro statements.pptx
how to write loops in java explained vividly
Repetition Structure
Java loops
Control flow statements in java web applications
Unit-02 Selection, Mathematical Functions and loops.pptx
C Sharp Jn (3)
Control structures in java
Core java
Loops
DAY_1.2.pptx
130706266060138191
6_A1944859510_21789_2_2018_06. Branching Statements.ppt
M C6java5
07 flow control
Module 5 : Statements & Exceptions
JPC#8 Introduction to Java Programming
Programming in java - Concepts- Operators- Control statements-Expressions

More from yugandhar vadlamudi (15)

ODP
Toolbarexample
ODP
Singleton pattern
PPT
Object Relational model for SQLIite in android
DOCX
JButton in Java Swing example
PPTX
Collections framework in java
PPTX
Packaes & interfaces
PPTX
Exception handling in java
DOCX
JMenu Creation in Java Swing
DOCX
Adding a action listener to button
PPTX
Dynamic method dispatch
PPTX
Operators in java
PPTX
Inheritance
PPTX
Closer look at classes
PPTX
java Applet Introduction
PPTX
Class introduction in java
Toolbarexample
Singleton pattern
Object Relational model for SQLIite in android
JButton in Java Swing example
Collections framework in java
Packaes & interfaces
Exception handling in java
JMenu Creation in Java Swing
Adding a action listener to button
Dynamic method dispatch
Operators in java
Inheritance
Closer look at classes
java Applet Introduction
Class introduction in java

Recently uploaded (20)

PDF
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
PDF
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
PPTX
Odoo 18 Sales_ Managing Quotation Validity
PPTX
Congenital Hypothyroidism pptx
PPTX
vedic maths in python:unleasing ancient wisdom with modern code
PPTX
Strengthening open access through collaboration: building connections with OP...
PPTX
Introduction and Scope of Bichemistry.pptx
PPTX
IMMUNIZATION PROGRAMME pptx
PPTX
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
PPTX
Presentation on Janskhiya sthirata kosh.
PDF
What Is Coercive Control? Understanding and Recognizing Hidden Abuse
PDF
Electrolyte Disturbances and Fluid Management A clinical and physiological ap...
PDF
The Final Stretch: How to Release a Game and Not Die in the Process.
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PPTX
Open Quiz Monsoon Mind Game Prelims.pptx
PDF
High Ground Student Revision Booklet Preview
PDF
UTS Health Student Promotional Representative_Position Description.pdf
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Odoo 18 Sales_ Managing Quotation Validity
Congenital Hypothyroidism pptx
vedic maths in python:unleasing ancient wisdom with modern code
Strengthening open access through collaboration: building connections with OP...
Introduction and Scope of Bichemistry.pptx
IMMUNIZATION PROGRAMME pptx
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
Presentation on Janskhiya sthirata kosh.
What Is Coercive Control? Understanding and Recognizing Hidden Abuse
Electrolyte Disturbances and Fluid Management A clinical and physiological ap...
The Final Stretch: How to Release a Game and Not Die in the Process.
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
Open Quiz Monsoon Mind Game Prelims.pptx
High Ground Student Revision Booklet Preview
UTS Health Student Promotional Representative_Position Description.pdf
Open Quiz Monsoon Mind Game Final Set.pptx
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
NOI Hackathon - Summer Edition - GreenThumber.pptx

Control flow statements in java

  • 1. Control Flow Statements Branching Statements Looping Statements Jump Statemetns
  • 2. Branching Statements • If statement • If else Statement • If..else…if statement • Nest if…else statement • Switch Statement <<goTo first slide>>
  • 3. If Statement • An if statement consists of a Boolean expression followed by one or more statements. If the Boolean expression evaluates to true then the block of code inside the if statement will be executed. If not the first set of code after the end of the if statement (after the closing curly brace) will be executed. if(Boolean expression) { //Some code should be work } public static void main(String[] y) { int my_int=25; if(my_int==25) { System.out.println("the condition is true"); } } Go to branch statement slide
  • 4. If else Statement • An if statement can be followed by an optional else statement, which executes when the Boolean expression is false. if(Boolean expression) { //some code excut } else { //some code excut } public static void main(String[] y) { int my_int=25; if(my_int<25) { System.out.println("the condition is true"); } else { System.out.println("the condition is not true"); } } • Go to branch statement slide
  • 5. If…else…if statement • An if statement can be followed by an optional else if...else statement, which is very useful to test various conditions using single if...else if statement. When using if , else if , else statements there are few points to keep in mind. • An if can have zero or one else's and it must come after any else if's. • An if can have zero to many else if's and they must come before the else. • Once an else if succeeds, none of the remaining else if's or else's will be tested. if(Boolean Expression 1) { //some code } else if (Boolean Expression 2) { //some code } else if (Boolean Expression 2) { //some code } else { //some code }
  • 6. public static void main(String[] y) { int my_int=25; if(my_int<25) { System.out.println("the condition is true"); } else if(my_int>25) {System.out.println("the else if condition is true for 25 > 25"); } else if(my_int==25) { System.out.println("the condition is true for 25==25"); } else { System.out.println("any of the condition is not ture"); } } Go to branch statement slides
  • 7. Nest if…else Statement • It is always legal to nest if-else statements which means you can use one if or else if statement inside another if or else if statement. if(boolean expression 1) { if(boolean expression 2) { //some code } else { //some code } }
  • 8. public static void main(String[] y) { int course_value=90; String course_name="java"; String Course_type="core & advanced"; if(course_value==90) { if(course_name=="java") { if(Course_type=="core & advanced") { System.out.println("the nested condition is true"); } } } else { System.out.println("the nested conditions are not true"); } } Go to Branch statement slide
  • 9. Switch Statement • Switch case statements are a substitute for long if statements that compare a variable to several "integral" values.. The value of the variable given into switch is compared to the value following each of the cases, and when one value matches the value of the variable, the computer continues executing the program from that point. switch(expression) { case value1: {//some code}break; case value2: {//some code}break; default: {//some code}break; }
  • 10. public static void main(String[] y) { int x=10; /* * x value is compared with case values */ switch(x) { case 10:{System.out.println("the x value is 10");}break; case 30:{System.out.println("the x value is 30");}break; case 40:{System.out.println("the x value is 40");}break; } } Go to branch statement slide
  • 11. LOOP control Statements • loop is a sequence of instruction s that is continually repeated until a certain condition is reached. There are four types of loops: • For loop • For each loop • While loop • Do..While loop <<goto first slide>>
  • 12. For loop • The for is an entry-entrolled loop and is used when an action is to repeated for a predetermined number of times for(initial value; test condition; increment) { //some code } public static void main(String[] y) { for(int i=0; i<5; i++) {System.out.println("hello world "+i);} } Go to loop slide
  • 13. Foreach Statement • A ForEach style loop is designed to cycle through a collection of objects,such as an array, in strictly sequential fashion, from start to finish.Implement a foreach loop by using the keyword foreach, java adds the foreach compability by enhancing the for statement. • for(type itr-var:collection)statement-block int[] a={1,2,3,4}; for(int x:a) { System.out.println("array of a["+x+"] "+a[x]); } Go to loop slide
  • 14. While loop(Entry Control loop) • It repeates a statement or block while its controlling expression(any boolean Expression) is true • The body of the loop will be executed as long as the conditional expression is true. while(condition) { //body of code } public static void main(String[] y) { while(x<5) { /*x++ increment value of x;*/ System.out.println("the value of x is "+x); x++; } } Go to loop slide
  • 15. Do-while loop • A while loop is initially false then the body of the loop will not be executed at all .The do-while loop always executes its body at least once because its conditional expression is at the bottom of the loop do { //body of code }while(boolean expression); System.out.println("the do While loop"); int x=1; do { /*x++ increment value of x; */ System.out.println("the value of x is "+x); x++; }while(x<5); Go to loop slided
  • 16. Jump Statements • Break • Continue • Return <<goto first slide>>
  • 17. Jump Statements • Break • In java the break statement has 3 uses • Terminates a statement sequence in a switch statement • It can be used to exit loop • It can be used more civilized form of goto statement • Using Break to exit the loop By using break, you can force immediate termination of a loop .when break statement is encountered inside a loop the loop is terminated and program control resumes at the next statement following loop
  • 18. public static void main(String[] y) { for(int i=0;i<10;i++) { if(i==5) { System.out.println("condition break"); break; } System.out.println("the value of i is::"+i); } }
  • 19. • Using break in the form of GOTO • Syntax Break label_name; public static void main(String[] y) { first:{ secound:{ third:{ System.out.println("third block"); if(true) { break secound; } } System.out.println("secound block"); } System.out.println("first block"); } } go to jump statements
  • 20. Continue • A continue statement causes control to be transferred directly to the conditional expression that controls the loop public static void main(String[] y) { for(int i=0;i<5;i++) { if(true) { System.out.println("the if block"); continue; } System.out.println("the for loop"); } }
  • 21. Using continue with labels • Specify the labels with continue statement public static void main(String[] y) { outer:for(int i=0;i<=2;i++){ for(int j=0;j<=1;j++) { System.out.println("value of J is::"+j); continue outer; } System.out.println("the value of i is::"+i); } /* for continue labels it should be used with loops only not for blocks */ } Go to jump statements
  • 22. Return • The return statement is used to explicitly return from a method.That is,it causes program control to transfer back to the caller of the method.At any time in a method the return statement can be used to cause execution to branch back to the caller of the method.Thus,the return statement can be used to cause execution to branch back to the caller ot the method.Thus, the return statement immediately terminates the method in which it is executed. public static void main(String[] y) { System.out.println("before return statement"); if(true) { return; } System.out.println("after return statement"); /* here return causes execution to return to the java run-time system */ }
  • 23. class Demo_break { int demo() { return(1); } public static void main(String[] y) { Demo_break d=new Demo_break(); System.out.println("the return value is"+d.demo()); } } • goto jump statement
  • 24. • Find the errors in the following 1. If(x+y=z&&y>0) 2. If(x>0); A=b+c; Else A=a*b; 3. If(x<0)||(q<0)
  • 25. Class ifelse { int result=55; char grade; if(result>90) grade=‘A’; else if(result>80)grade=“B”; else if(result>70) grade=“c”; else grade=“F”; else grade=“g”; System.out.println(“the grade is “+grade); } See the output
  • 26. • Correct the code to print the output “correct value” public static void main(String[] args) { int i=2; if(i=2) { System.out.println("correct value"); } else { System.out.println("not correct value"); }