SlideShare a Scribd company logo
Ayesha Sani
Lecturer GIS
GCUF
 Statements inside your source files are generally executed
from top to bottom, in the order that they appear.
 Control flow statements, however, break up the flow of
execution by employing decision making, looping, and
branching, enabling your program to conditionally execute
particular blocks of code.
1. Selection or decision-making statements (if, if-else,
switch)
2. Looping statements (while, do-while, for)
3. Branching statements (break, continue, return)
Introduction – Control
Statements
Selection or decision making
statements
(if, if-else, switch)
The if statement
executes a block of code
only if the specified
expression is true.
If the value is false, then
the if block is skipped
and execution continues
with the rest of the
program.
Selection or decision-making statements
(if-then, if-then-else, switch)
The if/else statement is
an extension of the if
statement. If the
statements in the if
statement fails, the
statements in the else
block are executed.
Selection or decision-making statements
(if-then, if-then-else, switch)
More Examples of
“if” and “if-else”
Basic Structure of “if” and “if/else”
if (booleanExpression) {
statement(s);
}
Example:
if ((i > 0) && (i > 10)) {
System.out.println("i is an " +
"integer between 0 and 10");
}
control statements
class IfElseDemo {
public static void main(String[] args) {
int testscore = 76;
char grade;
if (testscore >= 90) {
grade = 'A';
}
else if (testscore >= 80) {
grade = 'B';
}
else if (testscore >= 70) {
grade = 'C';
}
else if (testscore >= 60) {
grade = 'D';
}
else {
grade = 'F';
}
System.out.println("Grade = " + grade);
}
}
class IfElseDemo {
public static void main(String[] args) {
int testscore = 76;
char grade;
if (testscore >= 90) {
grade = 'A';
}
if (testscore >= 80) {
grade = 'B';
}
if (testscore >= 70) {
grade = 'C';
}
if (testscore >= 60) {
grade = 'D';
}
else {
grade = 'F';
}
System.out.println("Grade = " + grade);
}
}
Adding a semicolon at the end of an if clause is a
common mistake.
if (radius >= 0);
{
area = radius*radius*PI;
System.out.println(
"The area for the circle of radius " +
radius + " is " + area);
}
This mistake is hard to find, because it is not a
compilation error or a runtime error, it is a logic error.
This error often occurs when you use the next-line
block style.
Wrong
if (radius >= 0) {
area = radius*radius*PI;
System.out.println("The area for the “
+ “circle of radius " + radius +
" is " + area);
}
else {
System.out.println("Negative input");
}
The else clause matches the most recent if clause in the
same block. For example, the following statement
int i = 1; int j = 2; int k = 3;
if (i > j)
if (i > k)
System.out.println("A");
else
System.out.println("B");
is equivalent to
int i = 1; int j = 2; int k = 3;
if (i > j)
{
if (i > k)
System.out.println("A");
else
System.out.println("B");
}
Or Change it to
int i = 1; int j = 2; int k = 3;
if (i > j)
{
if (i > k)
System.out.println("A");
}
else
{
System.out.println("B");
}
Nothing is printed as Output
B is printed as Output
control statements
Summary Switch
 The keyword "switch"
is followed by an expression
that should evaluates to byte,
short, char or int primitive data
types ,only.
 In a switch block there can
be one or more labeled cases
and the expression that creates
labels for the case must be
unique.
The switch expression is
matched with each case label.
Only the matched case is
executed ,if no case matches
then the default statement (if
present) is executed.
The case statements are executed in sequential
order.)
The keyword break is optional, but it should be used
at the end of each case in order to terminate the
remainder of the switch statement. If the break
statement is not present, the next case statement
will be executed.
The default case, which is optional, can be used to
perform actions when none of the specified cases is
true
control statements
Looping Statements
(for, while, do-while)
The while statement continually
executes a block of statements
while a particular condition is true.
Its syntax can be expressed as:
while (expression) {
statement(s)
}
“while” loops
The Java programming language also provides a do-
while statement, which can be expressed as follows:
do {
statement(s)
} while (expression) ;
“do while” loops
Important
You can implement an infinite loop using the
while statement as follows:
while (true){
// your code goes here
}
Examples
“for” loop
The for statement provides a compact way to iterate
over a range of values
The general form of the “for” statement can be
expressed as follows:
for (initialization; termination; increment) {
statement(s)
}
for (int i=1; i<11; i++) {
System.out.println("Count is: " + i);
}
for loop Example
for (int i=1; i<=10; i++)
{
if (i%2==0){
System.out.println(i+“ is an even number”) ;
}
else{
System.out.println(i+“ is a odd number”) ;
}
}
Branching Statements
(break, continue)
Branching statements
(break, continue)
The break statement is used for breaking the
execution of a loop (while, do-while and for) . It
also terminates the switch statements.
// yourNumber contains an integer input by the user using
// JOptionPane.showInput Dialog
int yourNumber ;
for (i = 0; i < 10; i++) {
if (yourNumber== i) {
break;
}
System.out.println(“Number = ” + i);
}
Branching statements
(break, continue)
The continue statement is used to skip that
specific execution of a loop (while, do-while and
for) .
// yourNumber contains an integer input by the user using
// JOptionPane.showInput Dialog
int yourNumber ;
for (i = 0; i < 10; i++) {
if (yourNumber== i) {
continue;
}
System.out.println(“Number = ” + i);
}
 Statements inside your source files are generally executed
from top to bottom, in the order that they appear.
 Control flow statements, however, break up the flow of
execution by employing decision making, looping, and
branching, enabling your program to conditionally execute
particular blocks of code.
1. Selection or decision-making statements (if, if-else,
switch)
2. looping statements (for, while, do-while)
3. branching statements (break, continue, return)
Summary – Control Statements
Steps of Writing a Java Program
1. Pseudo Code
2. Flow Chart
3. Write Program
4. Run
5. Debug
Algorithm Building
control statements
 Write a program that takes an integer input
from user and print the table of that
number upto 10. (Print the table only if the
input number is between 3 and 20) [Note:
Use for loop]
 Write a program that calculates the product
of the odd integers from 1 to 15, then
displays the results in a message dialog
[Note: Use for loop]
Exercise 1
 Body Mass Index Program
 Print 1 2 3 4 5 4 3 2 1 using a single for
loop
 Change the Cricket program so that all the
input is done in a single loop
 Mobile Bill Calculation Software
Exercise 2
1 pound = 0.453 592 37 kilogram
control statements
control statements
Use loops (for or while) to generate the following
patterns
*
* *
* * *
* * * *
* * * * *
*
**
***
****
***** *
* *
* * *
* * * *
* * * * *
* * * * *
* * * *
* * *
* *
*
control statements
 Write such a Java program that takes integer
numbers input from the user (between 0
and 100) and print the number in English
letters.
Example:
 Input number 8 - Ouput = “Eight”
 Input number 39 - Ouput = “Thirty Nine”
 Input number 61 - Ouput = “Sixty One”
 Hint: Use % (Remainder Operator) with if
Control statement to program this task.

More Related Content

PPT
Repetition Structure
PRN USM
 
PPT
Control structures ii
Ahmad Idrees
 
PPTX
Control flow statements in java
yugandhar vadlamudi
 
PPT
conditional statements
James Brotsos
 
PPT
M C6java6
mbruggen
 
PDF
Java conditional statements
Kuppusamy P
 
PPT
Control structures in C++ Programming Language
Ahmad Idrees
 
PPT
Java control flow statements
Future Programming
 
Repetition Structure
PRN USM
 
Control structures ii
Ahmad Idrees
 
Control flow statements in java
yugandhar vadlamudi
 
conditional statements
James Brotsos
 
M C6java6
mbruggen
 
Java conditional statements
Kuppusamy P
 
Control structures in C++ Programming Language
Ahmad Idrees
 
Java control flow statements
Future Programming
 

What's hot (20)

PPT
M C6java5
mbruggen
 
PPT
Control structures i
Ahmad Idrees
 
PPTX
07 flow control
dhrubo kayal
 
PPT
Control statements in java programmng
Savitribai Phule Pune University
 
PPTX
Control structures in java
VINOTH R
 
PPT
Ch02 primitive-data-definite-loops
James Brotsos
 
PPTX
Chapter 2 : Programming with Java Statements
It Academy
 
PPT
Control Structures
Ghaffar Khan
 
PPT
Control Structures: Part 1
Andy Juan Sarango Veliz
 
PPT
Control statements
CutyChhaya
 
PDF
Automated Repair of Feature Interaction Failures in Automated Driving Systems
Lionel Briand
 
PPTX
130707833146508191
Tanzeel Ahmad
 
PPTX
Std 12 Computer Chapter 7 Java Basics (Part 2)
Nuzhat Memon
 
PPTX
Std 12 computer java basics part 3 control structure
Nuzhat Memon
 
PPTX
Control statement-Selective
Nurul Zakiah Zamri Tan
 
PPSX
Control Structures in Visual Basic
Tushar Jain
 
PPTX
C# Loops
Hock Leng PUAH
 
PDF
Control structures in Java
Ravi_Kant_Sahu
 
PPTX
Flow of control C ++ By TANUJ
TANUJ ⠀
 
PPT
Control structures selection
Online
 
M C6java5
mbruggen
 
Control structures i
Ahmad Idrees
 
07 flow control
dhrubo kayal
 
Control statements in java programmng
Savitribai Phule Pune University
 
Control structures in java
VINOTH R
 
Ch02 primitive-data-definite-loops
James Brotsos
 
Chapter 2 : Programming with Java Statements
It Academy
 
Control Structures
Ghaffar Khan
 
Control Structures: Part 1
Andy Juan Sarango Veliz
 
Control statements
CutyChhaya
 
Automated Repair of Feature Interaction Failures in Automated Driving Systems
Lionel Briand
 
130707833146508191
Tanzeel Ahmad
 
Std 12 Computer Chapter 7 Java Basics (Part 2)
Nuzhat Memon
 
Std 12 computer java basics part 3 control structure
Nuzhat Memon
 
Control statement-Selective
Nurul Zakiah Zamri Tan
 
Control Structures in Visual Basic
Tushar Jain
 
C# Loops
Hock Leng PUAH
 
Control structures in Java
Ravi_Kant_Sahu
 
Flow of control C ++ By TANUJ
TANUJ ⠀
 
Control structures selection
Online
 
Ad

Similar to control statements (20)

PPTX
Control statements in java
Madishetty Prathibha
 
PPTX
controlStatement.pptx, CONTROL STATEMENTS IN JAVA
DrNeetuSharma5
 
PPTX
Programming in java - Concepts- Operators- Control statements-Expressions
LovelitJose
 
PPTX
Java chapter 3
Abdii Rashid
 
PDF
Control flow statements in java web applications
RajithKarunarathne1
 
PPT
_Java__Expressions__and__FlowControl.ppt
JyothiAmpally
 
PPTX
Flow of control by deepak lakhlan
Deepak Lakhlan
 
PDF
csj-161127083146power point presentation
deepayaganti1
 
PPTX
Control Statements in Java
Niloy Saha
 
PPTX
Chapter05-Control Structures.pptx
AdrianVANTOPINA
 
PPTX
Computer programming 2 Lesson 9
MLG College of Learning, Inc
 
PPTX
Computer programming 2 - Lesson 7
MLG College of Learning, Inc
 
PPTX
Control structures
Gehad Enayat
 
PPT
Control statements
raksharao
 
PPTX
Pj01 5-exceution control flow
SasidharaRaoMarrapu
 
PPTX
Lecture - 5 Control Statement
manish kumar
 
PPTX
6.pptx
HarishNayak47
 
PDF
java notes.pdf
RajkumarHarishchandr1
 
PPTX
Unit-02 Selection, Mathematical Functions and loops.pptx
jessicafalcao1
 
PPTX
Unit- 1 -Java - Decision Making . pptx
CmDept
 
Control statements in java
Madishetty Prathibha
 
controlStatement.pptx, CONTROL STATEMENTS IN JAVA
DrNeetuSharma5
 
Programming in java - Concepts- Operators- Control statements-Expressions
LovelitJose
 
Java chapter 3
Abdii Rashid
 
Control flow statements in java web applications
RajithKarunarathne1
 
_Java__Expressions__and__FlowControl.ppt
JyothiAmpally
 
Flow of control by deepak lakhlan
Deepak Lakhlan
 
csj-161127083146power point presentation
deepayaganti1
 
Control Statements in Java
Niloy Saha
 
Chapter05-Control Structures.pptx
AdrianVANTOPINA
 
Computer programming 2 Lesson 9
MLG College of Learning, Inc
 
Computer programming 2 - Lesson 7
MLG College of Learning, Inc
 
Control structures
Gehad Enayat
 
Control statements
raksharao
 
Pj01 5-exceution control flow
SasidharaRaoMarrapu
 
Lecture - 5 Control Statement
manish kumar
 
java notes.pdf
RajkumarHarishchandr1
 
Unit-02 Selection, Mathematical Functions and loops.pptx
jessicafalcao1
 
Unit- 1 -Java - Decision Making . pptx
CmDept
 
Ad

Recently uploaded (20)

PDF
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
PPTX
A Smarter Way to Think About Choosing a College
Cyndy McDonald
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
PPTX
HEALTH CARE DELIVERY SYSTEM - UNIT 2 - GNM 3RD YEAR.pptx
Priyanshu Anand
 
PPTX
How to Apply for a Job From Odoo 18 Website
Celine George
 
DOCX
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
PPTX
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
PPTX
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
PDF
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
PPTX
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
PPTX
CDH. pptx
AneetaSharma15
 
PPTX
Command Palatte in Odoo 18.1 Spreadsheet - Odoo Slides
Celine George
 
PDF
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
DOCX
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
PPTX
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
PPTX
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
A Smarter Way to Think About Choosing a College
Cyndy McDonald
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
HEALTH CARE DELIVERY SYSTEM - UNIT 2 - GNM 3RD YEAR.pptx
Priyanshu Anand
 
How to Apply for a Job From Odoo 18 Website
Celine George
 
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
CDH. pptx
AneetaSharma15
 
Command Palatte in Odoo 18.1 Spreadsheet - Odoo Slides
Celine George
 
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 

control statements

  • 2.  Statements inside your source files are generally executed from top to bottom, in the order that they appear.  Control flow statements, however, break up the flow of execution by employing decision making, looping, and branching, enabling your program to conditionally execute particular blocks of code. 1. Selection or decision-making statements (if, if-else, switch) 2. Looping statements (while, do-while, for) 3. Branching statements (break, continue, return) Introduction – Control Statements
  • 3. Selection or decision making statements (if, if-else, switch)
  • 4. The if statement executes a block of code only if the specified expression is true. If the value is false, then the if block is skipped and execution continues with the rest of the program. Selection or decision-making statements (if-then, if-then-else, switch)
  • 5. The if/else statement is an extension of the if statement. If the statements in the if statement fails, the statements in the else block are executed. Selection or decision-making statements (if-then, if-then-else, switch)
  • 6. More Examples of “if” and “if-else”
  • 7. Basic Structure of “if” and “if/else”
  • 8. if (booleanExpression) { statement(s); } Example: if ((i > 0) && (i > 10)) { System.out.println("i is an " + "integer between 0 and 10"); }
  • 10. class IfElseDemo { public static void main(String[] args) { int testscore = 76; char grade; if (testscore >= 90) { grade = 'A'; } else if (testscore >= 80) { grade = 'B'; } else if (testscore >= 70) { grade = 'C'; } else if (testscore >= 60) { grade = 'D'; } else { grade = 'F'; } System.out.println("Grade = " + grade); } } class IfElseDemo { public static void main(String[] args) { int testscore = 76; char grade; if (testscore >= 90) { grade = 'A'; } if (testscore >= 80) { grade = 'B'; } if (testscore >= 70) { grade = 'C'; } if (testscore >= 60) { grade = 'D'; } else { grade = 'F'; } System.out.println("Grade = " + grade); } }
  • 11. Adding a semicolon at the end of an if clause is a common mistake. if (radius >= 0); { area = radius*radius*PI; System.out.println( "The area for the circle of radius " + radius + " is " + area); } This mistake is hard to find, because it is not a compilation error or a runtime error, it is a logic error. This error often occurs when you use the next-line block style. Wrong
  • 12. if (radius >= 0) { area = radius*radius*PI; System.out.println("The area for the “ + “circle of radius " + radius + " is " + area); } else { System.out.println("Negative input"); }
  • 13. The else clause matches the most recent if clause in the same block. For example, the following statement int i = 1; int j = 2; int k = 3; if (i > j) if (i > k) System.out.println("A"); else System.out.println("B"); is equivalent to int i = 1; int j = 2; int k = 3; if (i > j) { if (i > k) System.out.println("A"); else System.out.println("B"); } Or Change it to int i = 1; int j = 2; int k = 3; if (i > j) { if (i > k) System.out.println("A"); } else { System.out.println("B"); } Nothing is printed as Output B is printed as Output
  • 15. Summary Switch  The keyword "switch" is followed by an expression that should evaluates to byte, short, char or int primitive data types ,only.  In a switch block there can be one or more labeled cases and the expression that creates labels for the case must be unique. The switch expression is matched with each case label. Only the matched case is executed ,if no case matches then the default statement (if present) is executed.
  • 16. The case statements are executed in sequential order.) The keyword break is optional, but it should be used at the end of each case in order to terminate the remainder of the switch statement. If the break statement is not present, the next case statement will be executed. The default case, which is optional, can be used to perform actions when none of the specified cases is true
  • 19. The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as: while (expression) { statement(s) } “while” loops
  • 20. The Java programming language also provides a do- while statement, which can be expressed as follows: do { statement(s) } while (expression) ; “do while” loops
  • 21. Important You can implement an infinite loop using the while statement as follows: while (true){ // your code goes here }
  • 23. “for” loop The for statement provides a compact way to iterate over a range of values The general form of the “for” statement can be expressed as follows: for (initialization; termination; increment) { statement(s) } for (int i=1; i<11; i++) { System.out.println("Count is: " + i); }
  • 24. for loop Example for (int i=1; i<=10; i++) { if (i%2==0){ System.out.println(i+“ is an even number”) ; } else{ System.out.println(i+“ is a odd number”) ; } }
  • 26. Branching statements (break, continue) The break statement is used for breaking the execution of a loop (while, do-while and for) . It also terminates the switch statements. // yourNumber contains an integer input by the user using // JOptionPane.showInput Dialog int yourNumber ; for (i = 0; i < 10; i++) { if (yourNumber== i) { break; } System.out.println(“Number = ” + i); }
  • 27. Branching statements (break, continue) The continue statement is used to skip that specific execution of a loop (while, do-while and for) . // yourNumber contains an integer input by the user using // JOptionPane.showInput Dialog int yourNumber ; for (i = 0; i < 10; i++) { if (yourNumber== i) { continue; } System.out.println(“Number = ” + i); }
  • 28.  Statements inside your source files are generally executed from top to bottom, in the order that they appear.  Control flow statements, however, break up the flow of execution by employing decision making, looping, and branching, enabling your program to conditionally execute particular blocks of code. 1. Selection or decision-making statements (if, if-else, switch) 2. looping statements (for, while, do-while) 3. branching statements (break, continue, return) Summary – Control Statements
  • 29. Steps of Writing a Java Program 1. Pseudo Code 2. Flow Chart 3. Write Program 4. Run 5. Debug Algorithm Building
  • 31.  Write a program that takes an integer input from user and print the table of that number upto 10. (Print the table only if the input number is between 3 and 20) [Note: Use for loop]  Write a program that calculates the product of the odd integers from 1 to 15, then displays the results in a message dialog [Note: Use for loop] Exercise 1
  • 32.  Body Mass Index Program  Print 1 2 3 4 5 4 3 2 1 using a single for loop  Change the Cricket program so that all the input is done in a single loop  Mobile Bill Calculation Software Exercise 2
  • 33. 1 pound = 0.453 592 37 kilogram
  • 36. Use loops (for or while) to generate the following patterns * * * * * * * * * * * * * * * * ** *** **** ***** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  • 38.  Write such a Java program that takes integer numbers input from the user (between 0 and 100) and print the number in English letters. Example:  Input number 8 - Ouput = “Eight”  Input number 39 - Ouput = “Thirty Nine”  Input number 61 - Ouput = “Sixty One”  Hint: Use % (Remainder Operator) with if Control statement to program this task.