SlideShare uma empresa Scribd logo
1 de 32
Presented By
Niloy Saha
Control Statements in Java
Department of Computer Science & Engineering
Control Statements
Selection
Iteration
Jump
Outlines
2
Selection
Selection Statements
Switch Statement
 Selection Statements are also called Decision Making Statements.
Selection Statements
3
if Statements
Simple if
if else
if- else- if Ladder
Nested if
if Statements
4
Simple if
Syntax :
if (condition)
{
statement1;
}
Purpose: The statements will be evaluated if the value of the condition is true.
5
Simple if
Start
End
Condition
Statements
FalseTrue
Flow Chart:
6
Example
7
if else
Syntax :
if (condition)
{
statement1;
}
else
{
statement2;
}
Purpose: The statement 1 is evaluated if the value of the condition is true otherwise
statement 2 is true.
8
if else
FalseTrue
Flow Chart: Start
End
Condition
Statement 1 Statement 2
9
Example
10
If-else-if Ladder
Syntax :
if(condition)
statements;
else if(condition)
statements;
else if(condition)
statements;
...
...
else
statements;
11
Examples
import java.util.Scanner;
class Day
{
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
System.out.println("Enet day between 0 to 6 Day = ");
int day = s.nextInt();
if (day == 0)
{
System.out.println("n Sunday");
}
else if (day == 1)
{
System.out.println("n Monday");
}
else if (day == 2)
{
System.out.println("n Tuesday");
}
else if (day == 3)
{
System.out.println("n Wednesday");
}
else if (day == 4)
{
System.out.println("n Thursday");
}
else if (day == 5)
{
System.out.println("n Friday");
}
else
{
System.out.println("n Saturday");
}
}
}
12
Nested if
• A nested if is an if statement that is the target of another if or else.
• Nested ifs are very common in programming.
Syntax :
if(condition)
{
if(condition)
statements....
else
statements....
}
else
{
if(condition)
statements....
else
statements....
}
13
Example
14
switch
Syntax :
switch (expression)
{
case value 1 :
statement 1 ; break;
case value 2 :
statement 2 ; break;
...
...
case value N :
statement N ; break;
default :
statements ; break;
}
Purpose: The statements N will be evaluated if the value of the logical expression is true.
15
switch
Flow Chart:
Case A
Case B
…
default
False
False
False
Case A Statements
break;
Case B Statements
break;
Case C Statements
break;
Default Statements
Start
Variable or Expression
True
True
True
End
16
Example
17
Iteration Statements
Iterations/ Loops
while
do while
for
Each loop has four types of
statements :
 Initialization
 Condition checking
 Execution
 Increment / Decrement
18
while
Syntax:
initialization
while(final value)
{
statements;
increment/decrement;
}
Purpose: To evaluate the statements from initial value to final value with given
increment/decrement.
m=1
while(m<=20)
{
System.out.println(m);
m=m+1;
}
19
Example
class while1
{
public static void main(String args[])
{
int i=1;
while(i<=10)
{
System.out.println("n" + i);
i++;
}
}
}
Output :
1
2
3
4
5
6
7
8
9
10
 print values from 1 to 10
20
do while
Syntax:
initialization
do
{
statements;
increment/decrement;
}
while(final value);
Purpose: To evaluate the statements from initial value to final value with given
increment/decrement.
m=1
do
{
System.out.println(m);
m=m+1;
}
while(m==20);
21
Example
class dowhile1
{
public static void main(String args[])
{
int i = 1;
int sum = 0;
do
{
sum = sum + i;
i++;
}while (i<=10);
System.out.println("nntThe sum of 1 to 10 is .. " + sum);
}
}
Output :
The sum of 1 to 10 is .. 55 22
for
Syntax:
for(initialization;final value;increment/decrement)
{
statements;
}
Purpose: To evaluate the statements from initial value to final value with given
increment/decrement.
for(m=1;m<=20;m=m+1)
{
System.out.println(m);
}
23
Example
class for1
{
public static void main(String args[])
{
int i;
for (i=0;i<5;i++)
{
System.out.println("nExample of for loop ");
}
}
Output :
Example of for loop
Example of for loop
Example of for loop
Example of for loop
Example of for loop
24
Jump Statements
Jump
break
continue
return
25
The break statement
 This statement is used to jump out of a loop.
 Break statement was previously used in switch – case statements.
 On encountering a break statement within a loop, the execution continues with the next
statement outside the loop.
 The remaining statements which are after the break and within the loop are skipped.
 Break statement can also be used with the label of a statement.
 A statement can be labeled as follows.
statementName : SomeJavaStatement
 When we use break statement along with label as,
break statementName;
26
Example
class break1
{
public static void main(String args[])
{
int i = 1;
while (i<=10)
{
System.out.println("n" + i);
i++;
if (i==5)
{
break;
}
}
}
}
Output :
1
2
3
4
27
continue Statement
 This statement is used only within looping statements.
 When the continue statement is encountered, the next iteration starts.
 The remaining statements in the loop are skipped. The execution starts from the
top of loop again.
28
Example
class continue1
{
public static void main(String args[])
{
for (int i=1; i<1=0; i++)
{
if (i%2 == 0)
continue;
System.out.println("n" + i);
}
}
}
Output :
1
3
5
7
9
29
The return Statement
 The last control statement is 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.
 The return statement immediately terminates the method in which it is
executed.
30
Example
class Return1
{
public static void main(String args[])
{
boolean t = true;
System.out.println("Before the return.");
if(t)
return; // return to caller
System.out.println("This won't execute.");
}
}
Output :
Before the return.
31
Thank You

Mais conteúdo relacionado

Mais procurados

Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 

Mais procurados (20)

Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C Language
 
Java constructors
Java constructorsJava constructors
Java constructors
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
 
Java Data Types
Java Data TypesJava Data Types
Java Data Types
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Java swing
Java swingJava swing
Java swing
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Control structures in java
Control structures in javaControl structures in java
Control structures in java
 
Java Decision Control
Java Decision ControlJava Decision Control
Java Decision Control
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Type casting in java
Type casting in javaType casting in java
Type casting in java
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
 
Java: GUI
Java: GUIJava: GUI
Java: GUI
 
Loop(for, while, do while) condition Presentation
Loop(for, while, do while) condition PresentationLoop(for, while, do while) condition Presentation
Loop(for, while, do while) condition Presentation
 
Java Streams
Java StreamsJava Streams
Java Streams
 
Java threads
Java threadsJava threads
Java threads
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
 
Java package
Java packageJava package
Java package
 

Destaque

Control statements in Java
Control statements  in JavaControl statements  in Java
Control statements in Java
Jin Castor
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in Java
Ravi_Kant_Sahu
 
5.interface and packages
5.interface and packages5.interface and packages
5.interface and packages
Deepak Sharma
 

Destaque (18)

java programming- control statements
 java programming- control statements java programming- control statements
java programming- control statements
 
Control statements in Java
Control statements  in JavaControl statements  in Java
Control statements in Java
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in Java
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
 
Ch4
Ch4Ch4
Ch4
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
 
javaMethod.ppt
javaMethod.pptjavaMethod.ppt
javaMethod.ppt
 
CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs
CS6270 Virtual Machines - Java Virtual Machine Architecture and APIsCS6270 Virtual Machines - Java Virtual Machine Architecture and APIs
CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs
 
Java Magazine : The JAVA Virtual Machine alternative languages
Java Magazine : The JAVA Virtual Machine alternative languagesJava Magazine : The JAVA Virtual Machine alternative languages
Java Magazine : The JAVA Virtual Machine alternative languages
 
Data types, Variables, Expressions & Arithmetic Operators in java
Data types, Variables, Expressions & Arithmetic Operators in javaData types, Variables, Expressions & Arithmetic Operators in java
Data types, Variables, Expressions & Arithmetic Operators in java
 
5.interface and packages
5.interface and packages5.interface and packages
5.interface and packages
 
Chapter 2 Java Methods
Chapter 2 Java MethodsChapter 2 Java Methods
Chapter 2 Java Methods
 
Control flow statements in java
Control flow statements in javaControl flow statements in java
Control flow statements in java
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Packages and interfaces
Packages and interfacesPackages and interfaces
Packages and interfaces
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Control statements
Control statementsControl statements
Control statements
 
Class object method constructors in java
Class object method constructors in javaClass object method constructors in java
Class object method constructors in java
 

Semelhante a Control Statements in Java

Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
alish sha
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
alish sha
 
C statements.ppt presentation in c language
C statements.ppt presentation in c languageC statements.ppt presentation in c language
C statements.ppt presentation in c language
chintupro9
 

Semelhante a Control Statements in Java (20)

Programming in java - Concepts- Operators- Control statements-Expressions
Programming in java - Concepts- Operators- Control statements-ExpressionsProgramming in java - Concepts- Operators- Control statements-Expressions
Programming in java - Concepts- Operators- Control statements-Expressions
 
CONTROL STMTS.pptx
CONTROL STMTS.pptxCONTROL STMTS.pptx
CONTROL STMTS.pptx
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
 
Mca i pic u-3 handling input output and control statements
Mca i pic u-3 handling input output and control statementsMca i pic u-3 handling input output and control statements
Mca i pic u-3 handling input output and control statements
 
C statements.ppt presentation in c language
C statements.ppt presentation in c languageC statements.ppt presentation in c language
C statements.ppt presentation in c language
 
Btech i pic u-3 handling input output and control statements
Btech i pic u-3 handling input output and control statementsBtech i pic u-3 handling input output and control statements
Btech i pic u-3 handling input output and control statements
 
Diploma ii cfpc u-3 handling input output and control statements
Diploma ii  cfpc u-3 handling input output and control statementsDiploma ii  cfpc u-3 handling input output and control statements
Diploma ii cfpc u-3 handling input output and control statements
 
Decision Making Statement in C ppt
Decision Making Statement in C pptDecision Making Statement in C ppt
Decision Making Statement in C ppt
 
Controls & Loops in C
Controls & Loops in C Controls & Loops in C
Controls & Loops in C
 
Visula C# Programming Lecture 3
Visula C# Programming Lecture 3Visula C# Programming Lecture 3
Visula C# Programming Lecture 3
 
handling input output and control statements
 handling input output and control statements handling input output and control statements
handling input output and control statements
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
Bsc cs pic u-3 handling input output and control statements
Bsc cs  pic u-3 handling input output and control statementsBsc cs  pic u-3 handling input output and control statements
Bsc cs pic u-3 handling input output and control statements
 
CH-4 (1).pptx
CH-4 (1).pptxCH-4 (1).pptx
CH-4 (1).pptx
 
C statements
C statementsC statements
C statements
 
2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt
 
Constructs (Programming Methodology)
Constructs (Programming Methodology)Constructs (Programming Methodology)
Constructs (Programming Methodology)
 
C++ decision making
C++ decision makingC++ decision making
C++ decision making
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
 

Último

Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
MsecMca
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
Epec Engineered Technologies
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 

Último (20)

Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
22-prompt engineering noted slide shown.pdf
22-prompt engineering noted slide shown.pdf22-prompt engineering noted slide shown.pdf
22-prompt engineering noted slide shown.pdf
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 

Control Statements in Java