Professional Documents
Culture Documents
DFC30133
Object Oriented Programming
CHAPTER 3 :
3
At the end of the class, students
should be able to :
COURSE LEARNING
OUTCOME
4
CONTROL
STATEMENTS
INTRODUCTION
❖ Looping statements
6
SELECTION
STATEMENTS
SELECTION STATEMENTS
• Used to evaluate expressions and direct the program execution based on the
result.
❖ if
If statements :
❖ switch i. Single if
ii. If-Else
iii. Nested if
8
I. SINGLE IF STATEMENT
• Syntax
• Executes the set of statements if (<condition>)
in if block, when the given {
condition is satisfied. <Conditional statements1>;
}
• Executes the statements in the else
else block, when the condition {
is not satisfied. <Conditional statements2>;
}
11
EXAMPLE PROGRAM
class Sample
This program accepts a
{
number, checks whether it
public static void main (String args[])
is less than 0 and displays
{
an appropriate message.
int num;
num = Integer.parseInt(args[0]);
if (num<0)
System.out.println("Negative");
else
System.out.println("Positive");
}
12
}
III. NESTED IF STATEMENT
if (<Condition 1>)
{
if (<Condition 2>)
• The if statements
{
written within the body Inner if
<Conditional statements1>;
of another if statement condition
}
to test multiple
else
conditions is called
{
nested if.
<Conditional statements2>;
Outer if
}
• Syntax : condition
}
else
{
<Conditional statements3>;
}
13
EXAMPLE PROGRAM
class Highest {
public static void main (String args[])
{
int x = 5; int y=7; int z=3;
if(x>y) {
if(x>z)
System.out.println(x + " is the highest");
else
System.out.println(z + " is the highest");
}
else {
if (y>z)
System.out.println(y + " is the highest");
What do you think is the else
System.out.println(z +" is the highest");
purpose of this program? }
}
14 }
THE SWITCH STATEMENT
15
EXAMPLE PROGRAM class SwitchDemo {
public static void main(String args[])
{
int month = Integer.parseInt(args[0]);
switch (month)
{
case 1: System.out.println("January"); break;
case 2: System.out.println("February"); break;
case 3: System.out.println("March"); break;
The switch statement case 4: System.out.println("April"); break;
takes an integer value as case 5: System.out.println("May"); break;
input and displays the case 6: System.out.println("June"); break;
month based on the default: System.out.println("wrong choice");
integer entered. }
}
}
16
LOOPING
STATEMENTS
LOOPING STATEMENTS
18
I. FOR LOOP
• This looping statement enables you to repeat a set of instructions based on the
condition.
• Used when the number of loops is known before the first loop.
• Syntax
19
I. FOR LOOP
20
EXAMPLE PROGRAM
class Loop
{
public static void main(String args[])
{
int num, bilUlangan;
System.out.print("Display number 1 to 10: ");
for (bilUlangan = 0; bilUlangan <= 10; bilUlangan ++)
{ num = bilUlangan +1;
System.out.print(num + "\t");
}
}
}
21
II. WHILE LOOP
do {
//loop statements
} while(condition);
24
EXAMPLE PROGRAM
class DoWhileDemo
{
public static void main(String args[])
{
int x=20;
do
{
Program illustrates the System.out.print(x);
working of a do-while x=x+1;
loop. }while(x<15);
}
}
25
BREAK
&
CONTINUE
III. DO-WHILE LOOP
• The break and continue statements in Java allow transfer of control from one
statement to another.
• The break statement causes termination of the loop and transfers the control
outside the loop.
• The continue statement will transfer the control to the beginning of the loop.
27
class BreakDemo
EXAMPLE : BREAK {
public static void main(String args[])
{
int i;
System.out.println("Entering into the loop");
for(i=1; i<50; i++)
{
if(i==10)
break; // exit from for loop
System.out.print(i+" ");
}
System.out.println();
System.out.println("Exiting out of the loop");
}
}
28
EXAMPLE : BREAK
class ContinueDemo
{
public static void main (String args[])
{
for(int i=1; i<=20; i++)
Program illustrates the {
use of continue if(i%2!=0)
continue; //next iteration
statement. The program
System.out.print(i+" ");
displays multiples of 2.
}
}
}
29
PROGRAMMING EXERCISE
Develop a program that will receive three (3) integer numbers from the user.
Your program will then identify the smallest number based on the integer numbers
entered by the user.