SlideShare a Scribd company logo
JAVA
P.Prathibha
Topics for Today’s Session
What is JAVA
Control Statements in Java
Selection Statements
Looping Statements
Jumping Statements
Control Statements
 The Java control statements inside a program are usually
executed sequentially.
 Sometimes a programmer wants to break the normal flow and
jump to another statement or execute a set of statements
repeatedly.
 Control statements in java enables decision making, looping and
branching.
 A control statement in java is a statement that determines
whether the other statements will be executed or not.
 It controls the flow of a program.
 Control Statements can be divided into three categories,
namely
 Selection statements
 Iteration statements
 Jump statements
Control statements in java
Decision-Making Statements
 Conditional Control Statements allows the program to
select between the alternatives during the program
execution.
 They are also called as decision-making statements or
selection statements.
 Statements that determine which statement to execute
and when are known as decision-making statements.
 These statements decides the flow of the execution based
on some conditions.
 Java’s Selection statements:
 if
 if-else
 nested-if
 if-else-if
 switch-case
Simple if statement
 if statement is the most simple decision making statement
 The if statement determines whether a code should be executed based on
the specified condition.
 if statement’s condition should evaluate only to the boolean values ‘true’
or ‘false’.
 We can add more than one condition in the same if statement by using
&& or || operators.
if(condition)
{
// statements (if Block)
}
//other statements
class IfDemo1 {
public static void main(String[] args)
{
int marks=70;
if(marks > 65)
{
System.out.print("Hello Java! Am in If");
}
}
}
if-then -else Statement
 The if-else statement is used for testing conditions.
 It is used for true as well as for false condition.
 If then Else statement provides two paths.
 The if block is executed when the condition holds true.
When the condition evaluates to false, the statements inside
the else block are executed.
if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}
contd..
// Java program to illustrate if-else statement
class IfElseDemo {
public static void main(String args[])
{
int i = 20;
if (i < 45)
System.out.println("i is smaller than 45");
else
System.out.println("i is greater than 55");
}
}
Nested if statement
An if present inside an if block is known as a nested if block.
It is similar to an if..else statement, except they are defined inside another
if..else statement.
contd..
if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
}
class NestedIfDemo {
public static void main(String args[]) {
int s = 15;
if (s > 30)
{ if (s%2==0)
System.out.println("s is an even number and greater than 30!");
else
System.out.println("s is a odd number and greater than 30!"); }
else {
System.out.println("s is less than 15"); }
System.out.println("Hello World!");
} }
If Else-If ladder:
 If the condition is true, then it will execute the If block.
 Otherwise, it will execute the Else-If block.
 Again, if the condition is not met, then it will move to the else block.
 if-else-if ladder is used to decide among multiple options.
 The if statements are executed from the top down.
 As soon as one of the conditions controlling the if is true, the statement
associated with that if is executed, and the rest of the ladder is bypassed.
 If none of the conditions is true, then the final else statement will be
executed.
if(condition1)
{
//code for if condition1 is true
}
else if(condition2)
{
//code for if condition2 is true
}
else if(condition3)
{
//code for if condition3 is true
}
...
else
{
//code for all the false conditions
}
Switch statement
 The switch statement is a multiway branch statement.
 A switch statement is used to execute a single statement from multiple conditions.
 The switch statement can be used with short, byte, int, long, enum types, etc.
 One or N number of case values can be specified for a switch expression.
 Dulplicate case values are not allowed. A compile-time error is generated by the
compiler if unique values are not used.
 The case value must be literal or constant. Variables are not permissible.
 Usage of break statement is made to terminate the statement sequence. It is optional..
If this statement is not specified, the next case is executed.
 Based on the argument in the switch statement suitable case value will be selected and
executed.
 If no matching case found, then the default will be executed.
Switch(variable/value/expression){
Case :
// statements [];
Case :
// statements [];
…
default:
// statements [];
}
Control statements in java
Looping Statements / Iter
ative Statements
 These are used to execute a block of statements multiple
times.
 It means it executes the same code multiple times until a
specified condition is met,
 These are also called Iteration statements.
 There are three types of looping control statements:
1. For loop
2. While loop
3. Do-while loop
While loop
 While loop is a control flow statement that allows code to be
executed repeatedly based on a given Boolean condition.
 The while loop can be thought of as a repeating if statement.
 It is used for iterating a part of the program several times.
 When the number of iteration is not fixed then while loop is
used.
 While loop executes till the condition becomes false.
while (test_expression)
{
// statements
update_expression;
}
class WhileDemo {
public static void main(String[] args) {
int i=1;
while(i<=10)
{
System.out.println(i);
i++;
}
} }
// Example for infinite while loop
do-while loop
 In Java, the do-while loop is used to execute a part of the program
again and again.
 do-while loop is an Exit control loop.
 If the number of iteration is not fixed then the do-while loop is
used.
 This loop executes at least once because the loop is executed before
the condition is checked.
 Therefore, unlike for or while loop, a do-while check for the
condition after executing the statements or the loop body.
do
{
//code for execution
}
while(condition);
// Program to display numbers 1 to 15 using do-while loop
class DoWhileDemo {
public static void main(String[] args) {
int i=1;
do {
System.out.println(i);
i++;
}while(i<=10);
} }
// Program for infinite do-while loop
class DoWhileDemoIn {
public static void main(String[] args)
{
do {
System.out.println("infinitive do while loop");
}while(true);
}
}
For loop:
 The for loop in java is used to iterate and evaluate a code multiple
times.
 When the number of iterations is known by the user, it is
recommended to use the for loop.
 In java there are 3 types of for loops, they are as follows:
1. Simple for loop
2. For-each loop
3. labelled for loop
for (initialization; condition; increment/decrement)
{
statement;
}
// Java program to illustrate for loop
class forLoopDemo {
public static void main(String args[]) {
// Writing a for loop
// to print Hello World 5 times
for (int i = 1; i <= 5; i++)
System.out.println("Hello World");
} }
// Program to display multiplication table
class ForDemo1 {
public static void main(String[] args) {
int n, i;
n=2;
for(i=1;i<=10;i++)
{
System.out.println(n+"*"+i+"="+n*i);
}
} }
Output:
2*1=2
2*2=4
2*3=6
2*4=8
2*5=10
2*6=12
2*7=14
2*8=16
2*9=18
2*10=20
Output:
Hello World
Hello World
Hello World
Hello World
Hello World
For-Each loop:
 The traversal of elements in an array can be done by the for-
each loop.
 The elements present in the array are returned one by one.
 It must be noted that the user does not have to increment the
value in the for-each loop.
for(Type var:array)
{
//code for execution
}
public class ForEachDemo1
{
public static void main(String[] args) {
inta[]={100,101,102,103,104};
for(int i:a)
{ System.out.println(i);
}
} }
Output:
100
101
102
103
104
Labelled For Loop
 In Java, Labelled For Loop is used to give label before any
for loop. It is very useful for nesting for loop.
labelname:
for(initialization;condition;incr/decr)
{
//code for execution
}
public class LabeledForDemo {
public static void main(String[] args) {
num:
for(inti=1;i<=5;i++)
{
num1:
for(int j=1;j<=5;j++)
{ if(i==2&&j==2)
{
break num;
}
System.out.println(i+"
"+j);
} }
}
}
Output:
1 1
1 2
1 3
1 4
1 5
2 1
 Branching statements are used to jump from the current
executing loop.
 Branching statements in java are used to jump from a statement
to another statement, thereby the transferring the flow of
execution
break Statement
 break is a keyword.
 It is used within any control statements.
 break – Terminates the loop or switch statement and transfers
execution to the statement immediately following the loop or
switch.
 The break statement in java is used to terminate a loop and
break the current flow of the program.
 Syntax:
break; or
break <label>;
Unconditional Control Statements /
Jump Statements / Branching statements
// Using break statement in for loop
Output:
0
1
2
3
4
5
After loop
continue Statement
 continue is a keyword.
 In Java, the Continue statement is used in loops.
 Continue statement is used to jump to the next iteration of the loop
immediately.
 It is used with for loop, while loop and do-while loop.
Syntax:
continue; or
continue<label>;
class continueTest {
public static void main(String args[]) {
for (int j = 0; j &lt; 10; j++)
{
// If the number is odd then bypass and continue with next value
if (j%2 != 0)
continue;
// only even numbers will be printed
System.out.print(j + " ");
}
} }
Output: 0 2 4 6 8
//continue with label is used to continue the number of loops below
the label
class contlabelDemo{
public static void main(String args[]){
Termi:
for(int i=1;i<=4;i++)
{
for(int k=1;i<=4;k++)
{
System.out.print(“ ”+i+” ”);
if(i==3)
continue Termi;
System.out.println(“Hello”);
}
}
} }
Summary
 In this lesson you learnt about
 Control Statements
 Decision making statements
 Iterative statements
 Branching statements
Control statements in java

More Related Content

PPTX
Control structures in java
PPTX
Operators in java
PPTX
Control Statements in Java
PPTX
oops concept in java | object oriented programming in java
PPTX
Data structures
PDF
davisson-germer experiment.pdf
PDF
OS - Process Concepts
PDF
Java data types, variables and jvm
Control structures in java
Operators in java
Control Statements in Java
oops concept in java | object oriented programming in java
Data structures
davisson-germer experiment.pdf
OS - Process Concepts
Java data types, variables and jvm

What's hot (20)

PPTX
Inheritance in java
PPTX
Constructor in java
PDF
Arrays in Java
PPTX
Classes, objects in JAVA
PPTX
Java awt (abstract window toolkit)
PPTX
I/O Streams
PPTX
Method overloading
PPTX
Java Data Types
PPTX
Classes objects in java
PPTX
Type casting in java
PPTX
This keyword in java
PPTX
Java exception handling
PPTX
Constructor in java
PPT
Final keyword in java
PPSX
Data Types & Variables in JAVA
PPT
Java: GUI
PPT
Abstract class in java
PDF
Java threads
ODP
OOP java
Inheritance in java
Constructor in java
Arrays in Java
Classes, objects in JAVA
Java awt (abstract window toolkit)
I/O Streams
Method overloading
Java Data Types
Classes objects in java
Type casting in java
This keyword in java
Java exception handling
Constructor in java
Final keyword in java
Data Types & Variables in JAVA
Java: GUI
Abstract class in java
Java threads
OOP java
Ad

Similar to Control statements in java (20)

PPTX
LOOPING STATEMENTS, JAVA,PROGRAMMING LOGIC
PPTX
Java Control Statement Control Statement.pptx
PPT
Control statements
PPTX
controlStatement.pptx, CONTROL STATEMENTS IN JAVA
PPT
control-statements, control-statements, control statement
PPT
_Java__Expressions__and__FlowControl.ppt
PPT
_Java__Expressions__and__FlowControl.ppt
PPTX
DECISION MAKING AND BRANCHING - C Programming
PPT
2. Control structures with for while and do while.ppt
PPTX
dizital pods session 5-loops.pptx
PPTX
Java provides statements that can be used to control the flow of Java code
PPT
control-statements....ppt - definition
PPTX
Chapter05-Control Structures.pptx
PPTX
Constructs (Programming Methodology)
PPTX
Unit-02 Selection, Mathematical Functions and loops.pptx
PDF
Control flow statements in java web applications
PPTX
Decision Making Statement in C ppt
PPT
control-statements detailed presentation
DOCX
Chapter 4(1)
PDF
java notes.pdf
LOOPING STATEMENTS, JAVA,PROGRAMMING LOGIC
Java Control Statement Control Statement.pptx
Control statements
controlStatement.pptx, CONTROL STATEMENTS IN JAVA
control-statements, control-statements, control statement
_Java__Expressions__and__FlowControl.ppt
_Java__Expressions__and__FlowControl.ppt
DECISION MAKING AND BRANCHING - C Programming
2. Control structures with for while and do while.ppt
dizital pods session 5-loops.pptx
Java provides statements that can be used to control the flow of Java code
control-statements....ppt - definition
Chapter05-Control Structures.pptx
Constructs (Programming Methodology)
Unit-02 Selection, Mathematical Functions and loops.pptx
Control flow statements in java web applications
Decision Making Statement in C ppt
control-statements detailed presentation
Chapter 4(1)
java notes.pdf
Ad

More from Madishetty Prathibha (11)

PPTX
Object Oriented programming - Introduction
PPTX
Access modifiers in java
PPTX
Structure of java program diff c- cpp and java
PPTX
Operators in java
PPTX
Types of datastructures
PPTX
Introduction to algorithms
PPTX
Introduction to data structures (ss)
PPTX
PDF
Oops concepts || Object Oriented Programming Concepts in Java
PPT
Java features
PPSX
Introduction of java
Object Oriented programming - Introduction
Access modifiers in java
Structure of java program diff c- cpp and java
Operators in java
Types of datastructures
Introduction to algorithms
Introduction to data structures (ss)
Oops concepts || Object Oriented Programming Concepts in Java
Java features
Introduction of java

Recently uploaded (20)

PDF
IGGE1 Understanding the Self1234567891011
PDF
Computing-Curriculum for Schools in Ghana
PDF
1_English_Language_Set_2.pdf probationary
PPTX
Cell Types and Its function , kingdom of life
PDF
A systematic review of self-coping strategies used by university students to ...
PPTX
Unit 4 Skeletal System.ppt.pptxopresentatiom
PDF
Indian roads congress 037 - 2012 Flexible pavement
PDF
Practical Manual AGRO-233 Principles and Practices of Natural Farming
PPTX
Lesson notes of climatology university.
PPTX
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
PPTX
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
What if we spent less time fighting change, and more time building what’s rig...
PPTX
Introduction to Building Materials
PPTX
A powerpoint presentation on the Revised K-10 Science Shaping Paper
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
IGGE1 Understanding the Self1234567891011
Computing-Curriculum for Schools in Ghana
1_English_Language_Set_2.pdf probationary
Cell Types and Its function , kingdom of life
A systematic review of self-coping strategies used by university students to ...
Unit 4 Skeletal System.ppt.pptxopresentatiom
Indian roads congress 037 - 2012 Flexible pavement
Practical Manual AGRO-233 Principles and Practices of Natural Farming
Lesson notes of climatology university.
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Supply Chain Operations Speaking Notes -ICLT Program
What if we spent less time fighting change, and more time building what’s rig...
Introduction to Building Materials
A powerpoint presentation on the Revised K-10 Science Shaping Paper
202450812 BayCHI UCSC-SV 20250812 v17.pptx
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx

Control statements in java

  • 2. Topics for Today’s Session What is JAVA Control Statements in Java Selection Statements Looping Statements Jumping Statements
  • 3. Control Statements  The Java control statements inside a program are usually executed sequentially.  Sometimes a programmer wants to break the normal flow and jump to another statement or execute a set of statements repeatedly.  Control statements in java enables decision making, looping and branching.  A control statement in java is a statement that determines whether the other statements will be executed or not.  It controls the flow of a program.  Control Statements can be divided into three categories, namely  Selection statements  Iteration statements  Jump statements
  • 5. Decision-Making Statements  Conditional Control Statements allows the program to select between the alternatives during the program execution.  They are also called as decision-making statements or selection statements.  Statements that determine which statement to execute and when are known as decision-making statements.  These statements decides the flow of the execution based on some conditions.  Java’s Selection statements:  if  if-else  nested-if  if-else-if  switch-case
  • 6. Simple if statement  if statement is the most simple decision making statement  The if statement determines whether a code should be executed based on the specified condition.  if statement’s condition should evaluate only to the boolean values ‘true’ or ‘false’.  We can add more than one condition in the same if statement by using && or || operators. if(condition) { // statements (if Block) } //other statements
  • 7. class IfDemo1 { public static void main(String[] args) { int marks=70; if(marks > 65) { System.out.print("Hello Java! Am in If"); } } } if-then -else Statement  The if-else statement is used for testing conditions.  It is used for true as well as for false condition.  If then Else statement provides two paths.  The if block is executed when the condition holds true. When the condition evaluates to false, the statements inside the else block are executed.
  • 8. if (condition) { // Executes this block if // condition is true } else { // Executes this block if // condition is false } contd.. // Java program to illustrate if-else statement class IfElseDemo { public static void main(String args[]) { int i = 20; if (i < 45) System.out.println("i is smaller than 45"); else System.out.println("i is greater than 55"); } }
  • 9. Nested if statement An if present inside an if block is known as a nested if block. It is similar to an if..else statement, except they are defined inside another if..else statement. contd.. if (condition1) { // Executes when condition1 is true if (condition2) { // Executes when condition2 is true } } class NestedIfDemo { public static void main(String args[]) { int s = 15; if (s > 30) { if (s%2==0) System.out.println("s is an even number and greater than 30!"); else System.out.println("s is a odd number and greater than 30!"); } else { System.out.println("s is less than 15"); } System.out.println("Hello World!"); } }
  • 10. If Else-If ladder:  If the condition is true, then it will execute the If block.  Otherwise, it will execute the Else-If block.  Again, if the condition is not met, then it will move to the else block.  if-else-if ladder is used to decide among multiple options.  The if statements are executed from the top down.  As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed.  If none of the conditions is true, then the final else statement will be executed.
  • 11. if(condition1) { //code for if condition1 is true } else if(condition2) { //code for if condition2 is true } else if(condition3) { //code for if condition3 is true } ... else { //code for all the false conditions }
  • 12. Switch statement  The switch statement is a multiway branch statement.  A switch statement is used to execute a single statement from multiple conditions.  The switch statement can be used with short, byte, int, long, enum types, etc.  One or N number of case values can be specified for a switch expression.  Dulplicate case values are not allowed. A compile-time error is generated by the compiler if unique values are not used.  The case value must be literal or constant. Variables are not permissible.  Usage of break statement is made to terminate the statement sequence. It is optional.. If this statement is not specified, the next case is executed.  Based on the argument in the switch statement suitable case value will be selected and executed.  If no matching case found, then the default will be executed. Switch(variable/value/expression){ Case : // statements []; Case : // statements []; … default: // statements []; }
  • 14. Looping Statements / Iter ative Statements  These are used to execute a block of statements multiple times.  It means it executes the same code multiple times until a specified condition is met,  These are also called Iteration statements.  There are three types of looping control statements: 1. For loop 2. While loop 3. Do-while loop
  • 15. While loop  While loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition.  The while loop can be thought of as a repeating if statement.  It is used for iterating a part of the program several times.  When the number of iteration is not fixed then while loop is used.  While loop executes till the condition becomes false. while (test_expression) { // statements update_expression; }
  • 16. class WhileDemo { public static void main(String[] args) { int i=1; while(i<=10) { System.out.println(i); i++; } } } // Example for infinite while loop
  • 17. do-while loop  In Java, the do-while loop is used to execute a part of the program again and again.  do-while loop is an Exit control loop.  If the number of iteration is not fixed then the do-while loop is used.  This loop executes at least once because the loop is executed before the condition is checked.  Therefore, unlike for or while loop, a do-while check for the condition after executing the statements or the loop body. do { //code for execution } while(condition);
  • 18. // Program to display numbers 1 to 15 using do-while loop class DoWhileDemo { public static void main(String[] args) { int i=1; do { System.out.println(i); i++; }while(i<=10); } } // Program for infinite do-while loop class DoWhileDemoIn { public static void main(String[] args) { do { System.out.println("infinitive do while loop"); }while(true); } }
  • 19. For loop:  The for loop in java is used to iterate and evaluate a code multiple times.  When the number of iterations is known by the user, it is recommended to use the for loop.  In java there are 3 types of for loops, they are as follows: 1. Simple for loop 2. For-each loop 3. labelled for loop for (initialization; condition; increment/decrement) { statement; }
  • 20. // Java program to illustrate for loop class forLoopDemo { public static void main(String args[]) { // Writing a for loop // to print Hello World 5 times for (int i = 1; i <= 5; i++) System.out.println("Hello World"); } } // Program to display multiplication table class ForDemo1 { public static void main(String[] args) { int n, i; n=2; for(i=1;i<=10;i++) { System.out.println(n+"*"+i+"="+n*i); } } } Output: 2*1=2 2*2=4 2*3=6 2*4=8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18 2*10=20 Output: Hello World Hello World Hello World Hello World Hello World
  • 21. For-Each loop:  The traversal of elements in an array can be done by the for- each loop.  The elements present in the array are returned one by one.  It must be noted that the user does not have to increment the value in the for-each loop. for(Type var:array) { //code for execution } public class ForEachDemo1 { public static void main(String[] args) { inta[]={100,101,102,103,104}; for(int i:a) { System.out.println(i); } } } Output: 100 101 102 103 104
  • 22. Labelled For Loop  In Java, Labelled For Loop is used to give label before any for loop. It is very useful for nesting for loop. labelname: for(initialization;condition;incr/decr) { //code for execution } public class LabeledForDemo { public static void main(String[] args) { num: for(inti=1;i<=5;i++) { num1: for(int j=1;j<=5;j++) { if(i==2&&j==2) { break num; } System.out.println(i+" "+j); } } } } Output: 1 1 1 2 1 3 1 4 1 5 2 1
  • 23.  Branching statements are used to jump from the current executing loop.  Branching statements in java are used to jump from a statement to another statement, thereby the transferring the flow of execution break Statement  break is a keyword.  It is used within any control statements.  break – Terminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch.  The break statement in java is used to terminate a loop and break the current flow of the program.  Syntax: break; or break <label>; Unconditional Control Statements / Jump Statements / Branching statements
  • 24. // Using break statement in for loop Output: 0 1 2 3 4 5 After loop
  • 25. continue Statement  continue is a keyword.  In Java, the Continue statement is used in loops.  Continue statement is used to jump to the next iteration of the loop immediately.  It is used with for loop, while loop and do-while loop. Syntax: continue; or continue<label>; class continueTest { public static void main(String args[]) { for (int j = 0; j &lt; 10; j++) { // If the number is odd then bypass and continue with next value if (j%2 != 0) continue; // only even numbers will be printed System.out.print(j + " "); } } } Output: 0 2 4 6 8
  • 26. //continue with label is used to continue the number of loops below the label class contlabelDemo{ public static void main(String args[]){ Termi: for(int i=1;i<=4;i++) { for(int k=1;i<=4;k++) { System.out.print(“ ”+i+” ”); if(i==3) continue Termi; System.out.println(“Hello”); } } } }
  • 27. Summary  In this lesson you learnt about  Control Statements  Decision making statements  Iterative statements  Branching statements