You are on page 1of 31

CHAPTER 3 :

CLASSES AND OBJECTS

DFC30133
Object Oriented Programming
CHAPTER 3 :

3.3 CONSTRUCT BRANCHING CLASSES


STATEMENTS AND ARRAYS AND
OBJECTS
2
At the end of the class, students
should be able to :

❑ Explain selection statements in


Java programs.
❑ Explain looping statements in
Java programs.
❑ Build program using branching
statements.
❑ Explain an array in java
program. COURSE LEARNING
❑ Build reference arrays of
objects in Java program.
OUTCOME

3
At the end of the class, students
should be able to :

❑ Perform pass and return array


to method.
❑ Build program using single and
multidimensional array.

COURSE LEARNING
OUTCOME

4
CONTROL
STATEMENTS
INTRODUCTION

• Control statements are used to change the sequential flow of a program.

• These set of statements are executed based on a condition.

• Java supports two types of control statements :

❖ Branching / Selection statements

❖ Looping statements

6
SELECTION
STATEMENTS
SELECTION STATEMENTS

• Used to evaluate expressions and direct the program execution based on the
result.

• The selection statements supported by Java are:

❖ if
If statements :
❖ switch i. Single if
ii. If-Else
iii. Nested if

8
I. SINGLE IF STATEMENT

• Used to execute a set of statements when the given condition is satisfied.


• Syntax :
if (<condition>)
{
<Conditional statements>;
}
• Conditional statements within the block are executed when the condition in the if
statement is satisfied.
9
EXAMPLE PROGRAM

class InputValue The program checks


{
whether the given number
public static void main(String args[])
is greater than 100.
{
int num;
num = Integer.parseInt(args[0]);
if(num<100)
{
System.out.println("Number is less than 100");
}
}
10
}
II. IF-ELSE 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

• Is a multi-way branching statement.


• Contains various case statements.
• The case statements are executed based on the value of the expression.
• A break statement passes the control outside switch structure.

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

• Used to execute a set of instructions repeatedly, as long as the specific condition


is satisfied.
• The looping statements available in Java are:
❖ for
❖ while
❖ do-while

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

for ( initialisation; condition; incrementation / decrementation ){


//loop statements
}

19
I. FOR LOOP

• The for loop has three parts:


❖ Initialisation :
❑ The initial value of the loop control variable is set here.
❖ Condition :
❑ The loop condition is specified and the loop statements are executed as long as
this condition is satisfied.
❖ Incrementation / Decrementation :
❑ The initial value of the loop control variable is either incremented or
decremented each time the loop gets executed.

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

• Is a looping statement that enables you to repeat a set of instructions based on a


condition.
• Used when the number of loops is not known before the first loop.
• Syntax:
<Initialise variable>;
while(condition)
{
//loop statements
<Increment/decrement variable>;
}
22
EXAMPLE PROGRAM
class WhileDemo {
public static void main(String args[])
{
int sum = 0;
int i = 0;
while (i <= 10){
sum = sum+i;
System.out.println("Sum : " + sum);
i++;
Program illustrates the System.out.println("Value i : " + i);
working of a while loop. }
System.out.println("Total : " +sum);
}
}
23
III. DO-WHILE LOOP

• The do-while loop is similar to the while loop.


• The difference only in the location where the condition is checked. In do-while,
condition is checked at the end of loop.
• Syntax

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.

Were you able to write the


THREE (3) programs as shown
in the table?
30
“Any fool can write code that a
computer can understand. Good
END OF SLIDE programmers write code that
- SUBTOPIC 3.3 (PART 1) - humans can understand.”
— Martin Fowler

You might also like