SlideShare a Scribd company logo
2
Most read
3
Most read
11
Most read
Control Structures
In JAVA
-M Vishnuvardhan,
Dept. of Computer Science,
SSBN Degree College, ATP
SSBN Degree College, ATP M Vishnuvardhan
Introduction
Program contains a set of instructions . Normally the computer
executes these instructions in the sequence in which they appear, one
by one. This condition is called sequence accomplishment. In order to
modify this flow control structures are used.
A Control structure is structure which is used to modify the flow of the
program.
Control structures are classified in to two types
1.Branching Structures.
2.Looping Structures.
SSBN Degree College, ATP M Vishnuvardhan
Branching Structures
Branching structures are take a decision, basing on the
decision it executes a particular block of code only
once.
In java we have
1.2- way branching Structure Eg: if-else
2.Multi-way Branching Eg: switch
SSBN Degree College, ATP M Vishnuvardhan
2-Way Branching
If-else is a two-way branching structure in java. Here a
decision is made if it is true then a set of statements are
executed other another set of statements are executed
Flow Chart:-
Test
Condition
True Block False Block
Next
Statement
SSBN Degree College, ATP M Vishnuvardhan
Branching Structures
Syntax: if(test condition)
{
//True block
}
else
{
//else block
}
Test condition must always be a relation expression in
java (unlike C where it can be a numeral quantity)
SSBN Degree College, ATP M Vishnuvardhan
Various forms of if-else
1. Simple if
2. Nested if
3. Else if ladder
Else-if ladder is also called as multi-way branching
structure.
SSBN Degree College, ATP M Vishnuvardhan
Multi Way Branching
Switch is a multi-way branching structure in java.
Unlike if-else a switch statement doesn't have condition
instead it allows a variable to be tested for equality
against a list of values. Each value is called a case, and
the variable being switched on is checked for each case.
SSBN Degree College, ATP M Vishnuvardhan
Multi Way Branching
SSBN Degree College, ATP M Vishnuvardhan
Multi Way Branching
Syntax:
switch(expr)
{
case value1: block1 break;
case value2: block2 break;
case value n: blockn break;
default: default block1 break;
}
next statement;
expr must always yield integer/ char quantity only
from jdk1.5.0 Strings are also supported in switch
SSBN Degree College, ATP M Vishnuvardhan
Rules for switch
1. The variable used in a switch statement can only be integers, convertible
integers (byte, short, char), strings and enums.
2. You can have any number of case statements within a switch. Each case is
followed by the value to be compared to and a colon.
3. The value for a case must be the same data type as the variable in the
switch and it must be a constant or a literal.
4. When the variable being switched on is equal to a case, the statements
following that case will execute until a break statement is reached.
5. When a break statement is reached, the switch terminates, and the flow of
control jumps to the next line following the switch statement.
6. Not every case needs to contain a break. If no break appears, the flow of
control will fall through to subsequent cases until a break is reached.
7. A switch statement can have an optional default case, which must appear
at the end of the switch. The default case can be used for performing a
task when none of the cases is true. No break is needed in the default case.
SSBN Degree College, ATP M Vishnuvardhan
Looping Structures
There may be a situation when you need to execute a block of
code several number of times. A loop statement allows us to
execute a statement or group of statements multiple times.
Looping Structures are classified in to two types
1.Entry Control loop
Here test condition is checked before entering the loop body
Eg:- while, for
1.Exit Control loop
Here test condition is checked after executing the loop body
Eg:- do-while
SSBN Degree College, ATP M Vishnuvardhan
Entry Control vs Exit Control
Body of Loop
Condition
Next Statement
True
False
Body of Loop
Condition
Next Statement
False
True
SSBN Degree College, ATP M Vishnuvardhan
Looping Structures
When writing looping structures programmer should take really
care because they may tend to infinite loops.
The following points to be remembered while writing loops
1.Create and initialize a loop control variable
2.Form a condition using the loop control variable
3.Increment or decrement the loop control variable
SSBN Degree College, ATP M Vishnuvardhan
While Loop
Syntax:
while(textCondition)
{
=====
===== //body of loop
=====
}
Eg: int c=1;
while(c<=5)
{
System.out.println(“Java “);
c=c+1;
}
SSBN Degree College, ATP M Vishnuvardhan
for Loop
Syntax:
for(initialization; condition; increment)
{
=====
===== //body of loop
=====
}
Eg: for(int c=1;c<=5;c++)
{
System.out.println(“Java “);
}
SSBN Degree College, ATP M Vishnuvardhan
Nested for loops
If a for loop is placed inside another for loop then it s called as nested
for loops
Eg:
for(int i = 1; i <= 5; ++i)
{
for(int j = 1; j <= 5; ++j)
{
System.out.print(“Java”);
}
System.out.println(“Program");
}
For every iteration of outter for loop the inner for loop runs m times
i.e., for n iterations of outer loop inner loops works for nxm times
SSBN Degree College, ATP M Vishnuvardhan
Labelled loops
Java allows to keep labels for the loops. A label is a valid identifier
follwed by colon. Labelled loops are used for exiting multiple loops at
once with the help of break or continue
Syntax labelName:
Eg:
outter:
for(int i = 1; i <= 5; ++i)
{
inner:
for(int j = 1; j <= 5; ++j)
{
System.out.print(“Java”);
}
System.out.println(“Program");
}
SSBN Degree College, ATP M Vishnuvardhan
Enhanced for Loop (for each loop)
Enhanced for loop is generally used to process arrays collections.
It is introduced in JDK 1.5.0
Features:
It starts with the keyword for like a normal for-loop.
Instead of declaring and initializing a loop counter variable,
you declare a variable that is the same type as the base type of
the array, followed by a colon, which is then followed by the
array name.
In the loop body, you can use the loop variable you created
rather than using an indexed array element.
It’s commonly used to iterate over an array or a Collections
class (eg, ArrayList)
SSBN Degree College, ATP M Vishnuvardhan
Enhanced for Loop (for each loop)
Syntax:
for(datatype var : arrayName)
{
=====
===== //body of loop
=====
}
Eg: int a[]={10,20,30,40,50};
for(int element : a)
{
System.out.println(element);
}
int a[]={10,20,30,40,50};
for(int i=0;i<5;i++))
{
System.out.println(a[i]);
}
(OR)
SSBN Degree College, ATP M Vishnuvardhan
Do While Loop
Syntax:
do
{
=====
===== //body of loop
=====
} while(textCondition);
Eg: int c=1;
do
{
System.out.println(“Java “);
c=c+1;
} while(c<=5);
SSBN Degree College, ATP M Vishnuvardhan
Control Statements
Java provides 3 control statements
1. break 2. continue 3. return
Break statement is generally used to break loop or switch
statement. It breaks the current flow of the program at specified
condition. In case of nested loops it breaks only the loop in which
it is placed.
Syntax : break; public class BreakExample
{
public static void main(String[] args)
{
for(int i=1;i<=10;i++)
{
if(i==5)
break;
System.out.println(i);
}
}
}
SSBN Degree College, ATP M Vishnuvardhan
Control Statements
public class BreakExample2
{
public static void main(String[] args)
{
for(int i=1;i<=3;i++){
for(int j=1;j<=3;j++){
if(i==2&&j==2){
break;
}
System.out.println(i+" "+j);
}
}
}
SSBN Degree College, ATP M Vishnuvardhan
Questions

More Related Content

PDF
Python unit 2 as per Anna university syllabus
PPTX
Exceptions in Java
PPTX
Exception Handling in C++
PPT
Intermediate code generation (Compiler Design)
PPTX
Data Structures - Lecture 7 [Linked List]
PDF
Java thread life cycle
PPTX
Inheritance in c++
PPT
If-else and switch-case
Python unit 2 as per Anna university syllabus
Exceptions in Java
Exception Handling in C++
Intermediate code generation (Compiler Design)
Data Structures - Lecture 7 [Linked List]
Java thread life cycle
Inheritance in c++
If-else and switch-case

What's hot (20)

PPT
SQLITE Android
PDF
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
PPTX
Pointers in c++
PPTX
OOPS In JAVA.pptx
PPTX
Lecture_7-Encapsulation in Java.pptx
PPTX
constructors in java ppt
PPTX
C++ decision making
PPT
Functions in C++
PPT
Class and object in C++
PPTX
Priority Queue in Data Structure
PPTX
Java packages
PPTX
Lesson 6 php if...else...elseif statements
PPT
Files in c++ ppt
PDF
Exception handling in plsql
PDF
Object oriented programming c++
PDF
Php array
PPTX
Data types in C language
PPT
Operators in C++
PDF
Classes and Objects
PDF
4.2 PHP Function
SQLITE Android
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Pointers in c++
OOPS In JAVA.pptx
Lecture_7-Encapsulation in Java.pptx
constructors in java ppt
C++ decision making
Functions in C++
Class and object in C++
Priority Queue in Data Structure
Java packages
Lesson 6 php if...else...elseif statements
Files in c++ ppt
Exception handling in plsql
Object oriented programming c++
Php array
Data types in C language
Operators in C++
Classes and Objects
4.2 PHP Function
Ad

Similar to Control structures (20)

PPTX
PPTX
PDF
Control structures in Java
PPTX
Std 12 computer java basics part 3 control structure
PPTX
Control statements in java
PPT
Control statements
PPT
05. Control Structures.ppt
PPT
Control structures ii
PPTX
Java chapter 3
PPTX
Control Structures in Java with computer codes
PPTX
Control structures
PDF
java notes.pdf
PPTX
control statements
PPTX
Computer programming 2 Lesson 8
PPTX
Lecture - 5 Control Statement
PPTX
Control flow statements in java
PPTX
controlStatement.pptx, CONTROL STATEMENTS IN JAVA
PPTX
Java covers syntax, data types, object-oriented concepts, control flow, excep...
PPTX
Unit-02 Selection, Mathematical Functions and loops.pptx
PPTX
LOOPING STATEMENTS, JAVA,PROGRAMMING LOGIC
Control structures in Java
Std 12 computer java basics part 3 control structure
Control statements in java
Control statements
05. Control Structures.ppt
Control structures ii
Java chapter 3
Control Structures in Java with computer codes
Control structures
java notes.pdf
control statements
Computer programming 2 Lesson 8
Lecture - 5 Control Statement
Control flow statements in java
controlStatement.pptx, CONTROL STATEMENTS IN JAVA
Java covers syntax, data types, object-oriented concepts, control flow, excep...
Unit-02 Selection, Mathematical Functions and loops.pptx
LOOPING STATEMENTS, JAVA,PROGRAMMING LOGIC
Ad

More from M Vishnuvardhan Reddy (20)

PPTX
Python Sets_Dictionary.pptx
PPTX
Lists_tuples.pptx
PPTX
Python Control Structures.pptx
PPTX
Python Strings.pptx
PPTX
Python Basics.pptx
PPTX
Python Operators.pptx
PPTX
Python Datatypes.pptx
PPTX
DataScience.pptx
PPT
PPT
Cascading Style Sheets
PPT
Java Threads
PPT
Java Streams
PPT
Scanner class
PPT
Polymorphism
PPT
PPT
Java applets
PPT
Exception handling
PPT
Constructors
PPT
Classes&amp;objects
PPS
Python Sets_Dictionary.pptx
Lists_tuples.pptx
Python Control Structures.pptx
Python Strings.pptx
Python Basics.pptx
Python Operators.pptx
Python Datatypes.pptx
DataScience.pptx
Cascading Style Sheets
Java Threads
Java Streams
Scanner class
Polymorphism
Java applets
Exception handling
Constructors
Classes&amp;objects

Recently uploaded (20)

PPTX
Final SEM Unit 1 for mit wpu at pune .pptx
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
PPTX
Programs and apps: productivity, graphics, security and other tools
PPT
Module 1.ppt Iot fundamentals and Architecture
PPTX
OMC Textile Division Presentation 2021.pptx
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
1. Introduction to Computer Programming.pptx
PDF
WOOl fibre morphology and structure.pdf for textiles
PPTX
observCloud-Native Containerability and monitoring.pptx
PDF
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
PDF
2021 HotChips TSMC Packaging Technologies for Chiplets and 3D_0819 publish_pu...
PDF
DP Operators-handbook-extract for the Mautical Institute
PDF
A contest of sentiment analysis: k-nearest neighbor versus neural network
PDF
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
PPTX
O2C Customer Invoices to Receipt V15A.pptx
PDF
August Patch Tuesday
PDF
project resource management chapter-09.pdf
PDF
NewMind AI Weekly Chronicles – August ’25 Week III
PDF
Getting Started with Data Integration: FME Form 101
Final SEM Unit 1 for mit wpu at pune .pptx
gpt5_lecture_notes_comprehensive_20250812015547.pdf
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
Programs and apps: productivity, graphics, security and other tools
Module 1.ppt Iot fundamentals and Architecture
OMC Textile Division Presentation 2021.pptx
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
1. Introduction to Computer Programming.pptx
WOOl fibre morphology and structure.pdf for textiles
observCloud-Native Containerability and monitoring.pptx
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
2021 HotChips TSMC Packaging Technologies for Chiplets and 3D_0819 publish_pu...
DP Operators-handbook-extract for the Mautical Institute
A contest of sentiment analysis: k-nearest neighbor versus neural network
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
O2C Customer Invoices to Receipt V15A.pptx
August Patch Tuesday
project resource management chapter-09.pdf
NewMind AI Weekly Chronicles – August ’25 Week III
Getting Started with Data Integration: FME Form 101

Control structures

  • 1. Control Structures In JAVA -M Vishnuvardhan, Dept. of Computer Science, SSBN Degree College, ATP
  • 2. SSBN Degree College, ATP M Vishnuvardhan Introduction Program contains a set of instructions . Normally the computer executes these instructions in the sequence in which they appear, one by one. This condition is called sequence accomplishment. In order to modify this flow control structures are used. A Control structure is structure which is used to modify the flow of the program. Control structures are classified in to two types 1.Branching Structures. 2.Looping Structures.
  • 3. SSBN Degree College, ATP M Vishnuvardhan Branching Structures Branching structures are take a decision, basing on the decision it executes a particular block of code only once. In java we have 1.2- way branching Structure Eg: if-else 2.Multi-way Branching Eg: switch
  • 4. SSBN Degree College, ATP M Vishnuvardhan 2-Way Branching If-else is a two-way branching structure in java. Here a decision is made if it is true then a set of statements are executed other another set of statements are executed Flow Chart:- Test Condition True Block False Block Next Statement
  • 5. SSBN Degree College, ATP M Vishnuvardhan Branching Structures Syntax: if(test condition) { //True block } else { //else block } Test condition must always be a relation expression in java (unlike C where it can be a numeral quantity)
  • 6. SSBN Degree College, ATP M Vishnuvardhan Various forms of if-else 1. Simple if 2. Nested if 3. Else if ladder Else-if ladder is also called as multi-way branching structure.
  • 7. SSBN Degree College, ATP M Vishnuvardhan Multi Way Branching Switch is a multi-way branching structure in java. Unlike if-else a switch statement doesn't have condition instead it allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.
  • 8. SSBN Degree College, ATP M Vishnuvardhan Multi Way Branching
  • 9. SSBN Degree College, ATP M Vishnuvardhan Multi Way Branching Syntax: switch(expr) { case value1: block1 break; case value2: block2 break; case value n: blockn break; default: default block1 break; } next statement; expr must always yield integer/ char quantity only from jdk1.5.0 Strings are also supported in switch
  • 10. SSBN Degree College, ATP M Vishnuvardhan Rules for switch 1. The variable used in a switch statement can only be integers, convertible integers (byte, short, char), strings and enums. 2. You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon. 3. The value for a case must be the same data type as the variable in the switch and it must be a constant or a literal. 4. When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached. 5. When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement. 6. Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached. 7. A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.
  • 11. SSBN Degree College, ATP M Vishnuvardhan Looping Structures There may be a situation when you need to execute a block of code several number of times. A loop statement allows us to execute a statement or group of statements multiple times. Looping Structures are classified in to two types 1.Entry Control loop Here test condition is checked before entering the loop body Eg:- while, for 1.Exit Control loop Here test condition is checked after executing the loop body Eg:- do-while
  • 12. SSBN Degree College, ATP M Vishnuvardhan Entry Control vs Exit Control Body of Loop Condition Next Statement True False Body of Loop Condition Next Statement False True
  • 13. SSBN Degree College, ATP M Vishnuvardhan Looping Structures When writing looping structures programmer should take really care because they may tend to infinite loops. The following points to be remembered while writing loops 1.Create and initialize a loop control variable 2.Form a condition using the loop control variable 3.Increment or decrement the loop control variable
  • 14. SSBN Degree College, ATP M Vishnuvardhan While Loop Syntax: while(textCondition) { ===== ===== //body of loop ===== } Eg: int c=1; while(c<=5) { System.out.println(“Java “); c=c+1; }
  • 15. SSBN Degree College, ATP M Vishnuvardhan for Loop Syntax: for(initialization; condition; increment) { ===== ===== //body of loop ===== } Eg: for(int c=1;c<=5;c++) { System.out.println(“Java “); }
  • 16. SSBN Degree College, ATP M Vishnuvardhan Nested for loops If a for loop is placed inside another for loop then it s called as nested for loops Eg: for(int i = 1; i <= 5; ++i) { for(int j = 1; j <= 5; ++j) { System.out.print(“Java”); } System.out.println(“Program"); } For every iteration of outter for loop the inner for loop runs m times i.e., for n iterations of outer loop inner loops works for nxm times
  • 17. SSBN Degree College, ATP M Vishnuvardhan Labelled loops Java allows to keep labels for the loops. A label is a valid identifier follwed by colon. Labelled loops are used for exiting multiple loops at once with the help of break or continue Syntax labelName: Eg: outter: for(int i = 1; i <= 5; ++i) { inner: for(int j = 1; j <= 5; ++j) { System.out.print(“Java”); } System.out.println(“Program"); }
  • 18. SSBN Degree College, ATP M Vishnuvardhan Enhanced for Loop (for each loop) Enhanced for loop is generally used to process arrays collections. It is introduced in JDK 1.5.0 Features: It starts with the keyword for like a normal for-loop. Instead of declaring and initializing a loop counter variable, you declare a variable that is the same type as the base type of the array, followed by a colon, which is then followed by the array name. In the loop body, you can use the loop variable you created rather than using an indexed array element. It’s commonly used to iterate over an array or a Collections class (eg, ArrayList)
  • 19. SSBN Degree College, ATP M Vishnuvardhan Enhanced for Loop (for each loop) Syntax: for(datatype var : arrayName) { ===== ===== //body of loop ===== } Eg: int a[]={10,20,30,40,50}; for(int element : a) { System.out.println(element); } int a[]={10,20,30,40,50}; for(int i=0;i<5;i++)) { System.out.println(a[i]); } (OR)
  • 20. SSBN Degree College, ATP M Vishnuvardhan Do While Loop Syntax: do { ===== ===== //body of loop ===== } while(textCondition); Eg: int c=1; do { System.out.println(“Java “); c=c+1; } while(c<=5);
  • 21. SSBN Degree College, ATP M Vishnuvardhan Control Statements Java provides 3 control statements 1. break 2. continue 3. return Break statement is generally used to break loop or switch statement. It breaks the current flow of the program at specified condition. In case of nested loops it breaks only the loop in which it is placed. Syntax : break; public class BreakExample { public static void main(String[] args) { for(int i=1;i<=10;i++) { if(i==5) break; System.out.println(i); } } }
  • 22. SSBN Degree College, ATP M Vishnuvardhan Control Statements public class BreakExample2 { public static void main(String[] args) { for(int i=1;i<=3;i++){ for(int j=1;j<=3;j++){ if(i==2&&j==2){ break; } System.out.println(i+" "+j); } } }
  • 23. SSBN Degree College, ATP M Vishnuvardhan Questions