SlideShare a Scribd company logo
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

What's hot

Exception Handling in VB.Net
Exception Handling in VB.NetException Handling in VB.Net
Exception Handling in VB.Net
rishisingh190
 
Relational model
Relational modelRelational model
Relational model
Dabbal Singh Mahara
 
Introduction to visual basic programming
Introduction to visual basic programmingIntroduction to visual basic programming
Introduction to visual basic programmingRoger Argarin
 
Java awt (abstract window toolkit)
Java awt (abstract window toolkit)Java awt (abstract window toolkit)
Java awt (abstract window toolkit)
Elizabeth alexander
 
MYSQL - PHP Database Connectivity
MYSQL - PHP Database ConnectivityMYSQL - PHP Database Connectivity
MYSQL - PHP Database Connectivity
V.V.Vanniaperumal College for Women
 
Looping statement in vb.net
Looping statement in vb.netLooping statement in vb.net
Looping statement in vb.netilakkiya
 
Dbms relational model
Dbms relational modelDbms relational model
Dbms relational model
Chirag vasava
 
Relational algebra ppt
Relational algebra pptRelational algebra ppt
Relational algebra ppt
GirdharRatne
 
Php array
Php arrayPhp array
Php array
Nikul Shah
 
Materialize CSS - A Material Design Framework
Materialize CSS - A Material Design FrameworkMaterialize CSS - A Material Design Framework
Materialize CSS - A Material Design Framework
MRD Official
 
Web controls
Web controlsWeb controls
Web controls
Sarthak Varshney
 
PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts
Bharat Kalia
 
Procedural programming
Procedural programmingProcedural programming
Procedural programming
Ankit92Chitnavis
 
Control structures i
Control structures i Control structures i
Control structures i
Ahmad Idrees
 
Visual basic 6.0
Visual basic 6.0Visual basic 6.0
Visual basic 6.0Aarti P
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Monika Mishra
 
Java programming concept
Java programming conceptJava programming concept
Java programming concept
Sanjay Gunjal
 
Lesson 6 php if...else...elseif statements
Lesson 6   php if...else...elseif statementsLesson 6   php if...else...elseif statements
Lesson 6 php if...else...elseif statements
MLG College of Learning, Inc
 
PHP FUNCTIONS
PHP FUNCTIONSPHP FUNCTIONS
PHP FUNCTIONS
Zeeshan Ahmed
 

What's hot (20)

Exception Handling in VB.Net
Exception Handling in VB.NetException Handling in VB.Net
Exception Handling in VB.Net
 
Relational model
Relational modelRelational model
Relational model
 
Introduction to visual basic programming
Introduction to visual basic programmingIntroduction to visual basic programming
Introduction to visual basic programming
 
Java awt (abstract window toolkit)
Java awt (abstract window toolkit)Java awt (abstract window toolkit)
Java awt (abstract window toolkit)
 
MYSQL - PHP Database Connectivity
MYSQL - PHP Database ConnectivityMYSQL - PHP Database Connectivity
MYSQL - PHP Database Connectivity
 
Looping statement in vb.net
Looping statement in vb.netLooping statement in vb.net
Looping statement in vb.net
 
Dbms relational model
Dbms relational modelDbms relational model
Dbms relational model
 
Relational algebra ppt
Relational algebra pptRelational algebra ppt
Relational algebra ppt
 
Php array
Php arrayPhp array
Php array
 
Materialize CSS - A Material Design Framework
Materialize CSS - A Material Design FrameworkMaterialize CSS - A Material Design Framework
Materialize CSS - A Material Design Framework
 
Web controls
Web controlsWeb controls
Web controls
 
PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts
 
Procedural programming
Procedural programmingProcedural programming
Procedural programming
 
Control structures i
Control structures i Control structures i
Control structures i
 
Visual basic 6.0
Visual basic 6.0Visual basic 6.0
Visual basic 6.0
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Java programming concept
Java programming conceptJava programming concept
Java programming concept
 
Lesson 6 php if...else...elseif statements
Lesson 6   php if...else...elseif statementsLesson 6   php if...else...elseif statements
Lesson 6 php if...else...elseif statements
 
PHP FUNCTIONS
PHP FUNCTIONSPHP FUNCTIONS
PHP FUNCTIONS
 
Php mysql ppt
Php mysql pptPhp mysql ppt
Php mysql ppt
 

Similar to Control structures

Control statements
Control statementsControl statements
Control statements
CutyChhaya
 
Std 12 computer java basics part 3 control structure
Std 12 computer java basics part 3 control structureStd 12 computer java basics part 3 control structure
Std 12 computer java basics part 3 control structure
Nuzhat Memon
 
05. Control Structures.ppt
05. Control Structures.ppt05. Control Structures.ppt
05. Control Structures.ppt
AyushDut
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
Madishetty Prathibha
 
Java 2.pptx
Java 2.pptxJava 2.pptx
Java 2.pptx
usmanusman720379
 
Control Structure in JavaScript (1).pptx
Control Structure in JavaScript (1).pptxControl Structure in JavaScript (1).pptx
Control Structure in JavaScript (1).pptx
BansalShrivastava
 
CONTROL STMTS.pptx
CONTROL STMTS.pptxCONTROL STMTS.pptx
CONTROL STMTS.pptx
JavvajiVenkat
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
TANUJ ⠀
 
Bansal presentation (1).pptx
Bansal presentation (1).pptxBansal presentation (1).pptx
Bansal presentation (1).pptx
AbhiYadav655132
 
C Programming - Decision making, Looping
C  Programming - Decision making, LoopingC  Programming - Decision making, Looping
C Programming - Decision making, Looping
MURALIDHAR R
 
Java input Scanner
Java input Scanner Java input Scanner
Java input Scanner
Huda Alameen
 
Decision Making Statement in C ppt
Decision Making Statement in C pptDecision Making Statement in C ppt
Decision Making Statement in C ppt
MANJUTRIPATHI7
 
Android Application Development - Level 3
Android Application Development - Level 3Android Application Development - Level 3
Android Application Development - Level 3
Isham Rashik
 
Control structures
Control structuresControl structures
Control structuresGehad Enayat
 
Computer programming 2 Lesson 8
Computer programming 2  Lesson 8Computer programming 2  Lesson 8
Computer programming 2 Lesson 8
MLG College of Learning, Inc
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in JavaRavi_Kant_Sahu
 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
Niket Chandrawanshi
 
web presentation 138.pptx
web presentation 138.pptxweb presentation 138.pptx
web presentation 138.pptx
AbhiYadav655132
 

Similar to Control structures (20)

Control statements
Control statementsControl statements
Control statements
 
Std 12 computer java basics part 3 control structure
Std 12 computer java basics part 3 control structureStd 12 computer java basics part 3 control structure
Std 12 computer java basics part 3 control structure
 
05. Control Structures.ppt
05. Control Structures.ppt05. Control Structures.ppt
05. Control Structures.ppt
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
 
Java 2.pptx
Java 2.pptxJava 2.pptx
Java 2.pptx
 
6.pptx
6.pptx6.pptx
6.pptx
 
Control Structure in JavaScript (1).pptx
Control Structure in JavaScript (1).pptxControl Structure in JavaScript (1).pptx
Control Structure in JavaScript (1).pptx
 
Comp ppt (1)
Comp ppt (1)Comp ppt (1)
Comp ppt (1)
 
CONTROL STMTS.pptx
CONTROL STMTS.pptxCONTROL STMTS.pptx
CONTROL STMTS.pptx
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
 
Bansal presentation (1).pptx
Bansal presentation (1).pptxBansal presentation (1).pptx
Bansal presentation (1).pptx
 
C Programming - Decision making, Looping
C  Programming - Decision making, LoopingC  Programming - Decision making, Looping
C Programming - Decision making, Looping
 
Java input Scanner
Java input Scanner Java input Scanner
Java input Scanner
 
Decision Making Statement in C ppt
Decision Making Statement in C pptDecision Making Statement in C ppt
Decision Making Statement in C ppt
 
Android Application Development - Level 3
Android Application Development - Level 3Android Application Development - Level 3
Android Application Development - Level 3
 
Control structures
Control structuresControl structures
Control structures
 
Computer programming 2 Lesson 8
Computer programming 2  Lesson 8Computer programming 2  Lesson 8
Computer programming 2 Lesson 8
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in Java
 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
 
web presentation 138.pptx
web presentation 138.pptxweb presentation 138.pptx
web presentation 138.pptx
 

More from M Vishnuvardhan Reddy

Python Sets_Dictionary.pptx
Python Sets_Dictionary.pptxPython Sets_Dictionary.pptx
Python Sets_Dictionary.pptx
M Vishnuvardhan Reddy
 
Lists_tuples.pptx
Lists_tuples.pptxLists_tuples.pptx
Lists_tuples.pptx
M Vishnuvardhan Reddy
 
Python Control Structures.pptx
Python Control Structures.pptxPython Control Structures.pptx
Python Control Structures.pptx
M Vishnuvardhan Reddy
 
Python Strings.pptx
Python Strings.pptxPython Strings.pptx
Python Strings.pptx
M Vishnuvardhan Reddy
 
Python Basics.pptx
Python Basics.pptxPython Basics.pptx
Python Basics.pptx
M Vishnuvardhan Reddy
 
Python Operators.pptx
Python Operators.pptxPython Operators.pptx
Python Operators.pptx
M Vishnuvardhan Reddy
 
Python Datatypes.pptx
Python Datatypes.pptxPython Datatypes.pptx
Python Datatypes.pptx
M Vishnuvardhan Reddy
 
DataScience.pptx
DataScience.pptxDataScience.pptx
DataScience.pptx
M Vishnuvardhan Reddy
 
Html forms
Html formsHtml forms
Cascading Style Sheets
Cascading Style SheetsCascading Style Sheets
Cascading Style Sheets
M Vishnuvardhan Reddy
 
Java Threads
Java ThreadsJava Threads
Java Threads
M Vishnuvardhan Reddy
 
Java Streams
Java StreamsJava Streams
Java Streams
M Vishnuvardhan Reddy
 
Scanner class
Scanner classScanner class
Scanner class
M Vishnuvardhan Reddy
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
M Vishnuvardhan Reddy
 
Java intro
Java introJava intro
Java applets
Java appletsJava applets
Java applets
M Vishnuvardhan Reddy
 
Exception handling
Exception handling Exception handling
Exception handling
M Vishnuvardhan Reddy
 
Constructors
ConstructorsConstructors
Constructors
M Vishnuvardhan Reddy
 
Classes&amp;objects
Classes&amp;objectsClasses&amp;objects
Classes&amp;objects
M Vishnuvardhan Reddy
 
Shell sort
Shell sortShell sort

More from M Vishnuvardhan Reddy (20)

Python Sets_Dictionary.pptx
Python Sets_Dictionary.pptxPython Sets_Dictionary.pptx
Python Sets_Dictionary.pptx
 
Lists_tuples.pptx
Lists_tuples.pptxLists_tuples.pptx
Lists_tuples.pptx
 
Python Control Structures.pptx
Python Control Structures.pptxPython Control Structures.pptx
Python Control Structures.pptx
 
Python Strings.pptx
Python Strings.pptxPython Strings.pptx
Python Strings.pptx
 
Python Basics.pptx
Python Basics.pptxPython Basics.pptx
Python Basics.pptx
 
Python Operators.pptx
Python Operators.pptxPython Operators.pptx
Python Operators.pptx
 
Python Datatypes.pptx
Python Datatypes.pptxPython Datatypes.pptx
Python Datatypes.pptx
 
DataScience.pptx
DataScience.pptxDataScience.pptx
DataScience.pptx
 
Html forms
Html formsHtml forms
Html forms
 
Cascading Style Sheets
Cascading Style SheetsCascading Style Sheets
Cascading Style Sheets
 
Java Threads
Java ThreadsJava Threads
Java Threads
 
Java Streams
Java StreamsJava Streams
Java Streams
 
Scanner class
Scanner classScanner class
Scanner class
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Java intro
Java introJava intro
Java intro
 
Java applets
Java appletsJava applets
Java applets
 
Exception handling
Exception handling Exception handling
Exception handling
 
Constructors
ConstructorsConstructors
Constructors
 
Classes&amp;objects
Classes&amp;objectsClasses&amp;objects
Classes&amp;objects
 
Shell sort
Shell sortShell sort
Shell sort
 

Recently uploaded

Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
Peter Spielvogel
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Nexer Digital
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
RinaMondal9
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 

Recently uploaded (20)

Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 

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