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
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)
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
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
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.