SlideShare a Scribd company logo
1 of 38
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");
}
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
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
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
 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
Use loops (for or while) to generate the following
patterns
*
* *
* * *
* * * *
* * * * *
*
**
***
****
***** *
* *
* * *
* * * *
* * * * *
* * * * *
* * * *
* * *
* *
*
 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

What's hot

Control structures i
Control structures i Control structures i
Control structures i Ahmad Idrees
 
Control structures in java
Control structures in javaControl structures in java
Control structures in javaVINOTH R
 
Ch02 primitive-data-definite-loops
Ch02 primitive-data-definite-loopsCh02 primitive-data-definite-loops
Ch02 primitive-data-definite-loopsJames Brotsos
 
Chapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java StatementsChapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java StatementsIt Academy
 
Control Structures
Control StructuresControl Structures
Control StructuresGhaffar Khan
 
Control statements
Control statementsControl statements
Control statementsCutyChhaya
 
Automated Repair of Feature Interaction Failures in Automated Driving Systems
Automated Repair of Feature Interaction Failures in Automated Driving SystemsAutomated Repair of Feature Interaction Failures in Automated Driving Systems
Automated Repair of Feature Interaction Failures in Automated Driving SystemsLionel Briand
 
Std 12 Computer Chapter 7 Java Basics (Part 2)
Std 12 Computer Chapter 7 Java Basics (Part 2)Std 12 Computer Chapter 7 Java Basics (Part 2)
Std 12 Computer Chapter 7 Java Basics (Part 2)Nuzhat Memon
 
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 structureNuzhat Memon
 
Control Structures in Visual Basic
Control Structures in  Visual BasicControl Structures in  Visual Basic
Control Structures in Visual BasicTushar Jain
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in JavaRavi_Kant_Sahu
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJTANUJ ⠀
 
Control structures selection
Control structures   selectionControl structures   selection
Control structures selectionOnline
 

What's hot (20)

M C6java5
M C6java5M C6java5
M C6java5
 
Control structures i
Control structures i Control structures i
Control structures i
 
07 flow control
07   flow control07   flow control
07 flow control
 
Control statements in java programmng
Control statements in java programmngControl statements in java programmng
Control statements in java programmng
 
Control structures in java
Control structures in javaControl structures in java
Control structures in java
 
Ch02 primitive-data-definite-loops
Ch02 primitive-data-definite-loopsCh02 primitive-data-definite-loops
Ch02 primitive-data-definite-loops
 
Chapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java StatementsChapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java Statements
 
Control Structures
Control StructuresControl Structures
Control Structures
 
Control Structures: Part 1
Control Structures: Part 1Control Structures: Part 1
Control Structures: Part 1
 
Control statements
Control statementsControl statements
Control statements
 
Automated Repair of Feature Interaction Failures in Automated Driving Systems
Automated Repair of Feature Interaction Failures in Automated Driving SystemsAutomated Repair of Feature Interaction Failures in Automated Driving Systems
Automated Repair of Feature Interaction Failures in Automated Driving Systems
 
130707833146508191
130707833146508191130707833146508191
130707833146508191
 
Std 12 Computer Chapter 7 Java Basics (Part 2)
Std 12 Computer Chapter 7 Java Basics (Part 2)Std 12 Computer Chapter 7 Java Basics (Part 2)
Std 12 Computer Chapter 7 Java Basics (Part 2)
 
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
 
Control statement-Selective
Control statement-SelectiveControl statement-Selective
Control statement-Selective
 
Control Structures in Visual Basic
Control Structures in  Visual BasicControl Structures in  Visual Basic
Control Structures in Visual Basic
 
C# Loops
C# LoopsC# Loops
C# Loops
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in Java
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
 
Control structures selection
Control structures   selectionControl structures   selection
Control structures selection
 

Similar to control statements

Similar to control statements (20)

Android Application Development - Level 3
Android Application Development - Level 3Android Application Development - Level 3
Android Application Development - Level 3
 
Java chapter 5
Java chapter 5Java chapter 5
Java chapter 5
 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
 
OOPS USING C++(UNIT 2)
OOPS USING C++(UNIT 2)OOPS USING C++(UNIT 2)
OOPS USING C++(UNIT 2)
 
Lecture - 5 Control Statement
Lecture - 5 Control StatementLecture - 5 Control Statement
Lecture - 5 Control Statement
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
Switch case and looping
Switch case and loopingSwitch case and looping
Switch case and looping
 
Loops
LoopsLoops
Loops
 
Chapter 4 flow control structures and arrays
Chapter 4 flow control structures and arraysChapter 4 flow control structures and arrays
Chapter 4 flow control structures and arrays
 
C Sharp Jn (3)
C Sharp Jn (3)C Sharp Jn (3)
C Sharp Jn (3)
 
Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structures
 
Loops c++
Loops c++Loops c++
Loops c++
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++
 
Oop object oriented programing topics
Oop object oriented programing topicsOop object oriented programing topics
Oop object oriented programing topics
 
JPC#8 Introduction to Java Programming
JPC#8 Introduction to Java ProgrammingJPC#8 Introduction to Java Programming
JPC#8 Introduction to Java Programming
 
Java tutorial PPT
Java tutorial PPTJava tutorial PPT
Java tutorial PPT
 
Java tutorial PPT
Java tutorial  PPTJava tutorial  PPT
Java tutorial PPT
 
Chap05
Chap05Chap05
Chap05
 
Ch05
Ch05Ch05
Ch05
 

Recently uploaded

HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........LeaCamillePacle
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxLigayaBacuel1
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 

Recently uploaded (20)

HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 

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"); }
  • 9.
  • 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
  • 14.
  • 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
  • 17.
  • 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
  • 30.
  • 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
  • 34.
  • 35.
  • 36. Use loops (for or while) to generate the following patterns * * * * * * * * * * * * * * * * ** *** **** ***** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  • 37.
  • 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.