SlideShare a Scribd company logo
Control Statements
Selection
Iteration
Jump
Outlines
1
Selection
If Statements
Switch Statement
 Selection Statements are also called Decision Making Statements.
Selection Statements
2
if Statements
Simple if
if else
if- else- if Ladder
Nested if
if Statements
3
Simple if
4
Syntax :
if (condition)
{
statement1;
}
Purpose: The statements will be evaluated if the value of the condition is true.
Simple if
Start
End
Condition
Statements
False
5
True
Flow Chart:
Example
6
if else
7
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.
if else
False
True
Flow Chart: Start
End
Condition
Statement 1 Statement 2
8
Example
9
If-else-if Ladder
10
Syntax :
if(condition)
statements;
else if(condition)
statements;
else if(condition)
statements;
...
...
else
statements;
Examples
11
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");
}
}
}
Nested if
12
• 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....
}
Example
13
switch
14
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.
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
15
SWITCH Features
• Switch only tests for equality.
• No 2 case constants in the same switch can have identical values.
• More efficient than nested if’s.
• Faster than if.
Example
17
Iteration Statements
Iterations/ Loops
while
do while
for
18
Each loop has four types of
statements :
 Initialization
 Condition checking
 Execution
 Increment / Decrement
while
19
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;
}
Example
20
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
do while
21
Syntax:
initialization
do
{
statements;
increment/decrement;
}
while(final value);
Purpose: To evaluate
increment/decrement.
the statements from initial value to final value with given
m=1
do
{
System.out.println(m);
m=m+1;
}
while(m==20);
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
23
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);
}
Example
24
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
Nested Loops
Like all other programming languages, Java allows loops to be nested. That is, one loop
may be inside another. For example, here is a program that nests for loops,
// Loops may be nested.
class Nested {
public static void main(String args[]) {
int i, j;
for(i=0; i<10; i++) {
for(j=i; j<10; j++)
System.out.print(".");
System.out.println();
}
}
}
Jump Statements
Jump
break
continue
return
26
The break statement
27
 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
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.
 goto label; break label;
next
statementName : SomeJavaStatement
 When we use break statement along with label as,
break statementName;
Example
28
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
continue Statement
29
 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.
Example
30
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
The return Statement
31
 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.
Example
32
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.

More Related Content

PPTX
Stack and its operations
PPTX
Unit II - LINEAR DATA STRUCTURES
PPTX
Stacks in c++
PPT
358 33 powerpoint-slides_9-stacks-queues_chapter-9
PPTX
Stacks and Queue - Data Structures
PDF
stacks and queues
PPTX
Unit 3 stack
PPT
Stacks and queue
Stack and its operations
Unit II - LINEAR DATA STRUCTURES
Stacks in c++
358 33 powerpoint-slides_9-stacks-queues_chapter-9
Stacks and Queue - Data Structures
stacks and queues
Unit 3 stack
Stacks and queue

What's hot (19)

PPT
PPTX
My lecture stack_queue_operation
PPSX
Stacks Implementation and Examples
PPT
PPTX
Application of Stack - Yadraj Meena
PPT
03 stacks and_queues_using_arrays
PPTX
stacks and queues
PPTX
Stack and queue
PPTX
Polish
PPTX
Stack data structure
PPTX
Stack and its Applications : Data Structures ADT
PPT
Stacks
PPT
Stack Operation In Data Structure
PPTX
STACKS IN DATASTRUCTURE
PPSX
Stacks fundamentals
PPSX
PPTX
Data structure Stack
PPTX
Stack - Data Structure
PPT
Stack application
My lecture stack_queue_operation
Stacks Implementation and Examples
Application of Stack - Yadraj Meena
03 stacks and_queues_using_arrays
stacks and queues
Stack and queue
Polish
Stack data structure
Stack and its Applications : Data Structures ADT
Stacks
Stack Operation In Data Structure
STACKS IN DATASTRUCTURE
Stacks fundamentals
Data structure Stack
Stack - Data Structure
Stack application
Ad

Similar to Programming in java - Concepts- Operators- Control statements-Expressions (20)

PDF
csj-161127083146power point presentation
PPTX
Control Statements in Java
PPTX
Control statements in java
PPTX
control statements
PPTX
controlStatement.pptx, CONTROL STATEMENTS IN JAVA
PPT
Control statements
PPTX
Lecture - 5 Control Statement
PPTX
PPTX
Unit-02 Selection, Mathematical Functions and loops.pptx
PPTX
Object oriented programming_Unit1_contro statements.pptx
PPT
_Java__Expressions__and__FlowControl.ppt
PPTX
Control structures in java
PPTX
Control flow statements in java
PPT
4.CONTROL STATEMENTS_MB.ppt .
PPTX
Java chapter 3
PPT
Control statements
PPT
Control statements in java programmng
PPTX
PDF
java notes.pdf
PPTX
Flow of control by deepak lakhlan
csj-161127083146power point presentation
Control Statements in Java
Control statements in java
control statements
controlStatement.pptx, CONTROL STATEMENTS IN JAVA
Control statements
Lecture - 5 Control Statement
Unit-02 Selection, Mathematical Functions and loops.pptx
Object oriented programming_Unit1_contro statements.pptx
_Java__Expressions__and__FlowControl.ppt
Control structures in java
Control flow statements in java
4.CONTROL STATEMENTS_MB.ppt .
Java chapter 3
Control statements
Control statements in java programmng
java notes.pdf
Flow of control by deepak lakhlan
Ad

More from LovelitJose (6)

PPTX
Professional ethics-Unit5
PPTX
Professional ethics-Unit4
PPTX
Professional ethics-Unit3
PPTX
Professional Ethics Unit2
PPTX
Professional ethics PPT unit 1
PPTX
Programming in java basics
Professional ethics-Unit5
Professional ethics-Unit4
Professional ethics-Unit3
Professional Ethics Unit2
Professional ethics PPT unit 1
Programming in java basics

Recently uploaded (20)

PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPT
Project quality management in manufacturing
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
Sustainable Sites - Green Building Construction
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
composite construction of structures.pdf
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
PPT on Performance Review to get promotions
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
Model Code of Practice - Construction Work - 21102022 .pdf
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
CYBER-CRIMES AND SECURITY A guide to understanding
Project quality management in manufacturing
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Sustainable Sites - Green Building Construction
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
R24 SURVEYING LAB MANUAL for civil enggi
composite construction of structures.pdf
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPT on Performance Review to get promotions
Internet of Things (IOT) - A guide to understanding
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Embodied AI: Ushering in the Next Era of Intelligent Systems

Programming in java - Concepts- Operators- Control statements-Expressions