Topic 3 Program Control Structures, Lecture notes of Object Oriented Programming

•Write expressions using the conditional operator •Differentiate the Increment and Decrement operator •Implement selection control using if, if…else and switch statements •Implement repetition control in a program by using while, do… while and for loop statements

Typology: Lecture notes

2018/2019

Uploaded on 03/17/2019

sharyza-henry
sharyza-henry 🇲🇾

3 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
2/21/2019
1
TOPIC 03
Program Control Structures
SITI HASNAH TANALOL
PM 3, Block A, FKJ
Learning Outcomes
Upon completion of this lecture, student
should be able to:
Write expressions using the conditional operator
Differentiate the Increment and Decrement
operator
Implement selection control using if, if…else
and switch statements
Implement repetition control in a program by
using while, do… while and for loop
statements,
Contents
Control
structures
Selection
structures
If
if
if..else
Nested if
switch
Loops
Structures
while
do..while
for
Selection Structures
Selection
structures
If
if
if..else
Nested if
switch
if statements
Example
if(x == y)
System.out.println("X is
equal to Y");
if(x != y)
{ System.out.println("X is not
equal to Y");
x = y;
System.out.println("However,
now it is.");
}
if (condition)
execute_statements;
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Topic 3 Program Control Structures and more Lecture notes Object Oriented Programming in PDF only on Docsity!

TOPIC 03

Program Control Structures

SITI HASNAH TANALOL

[email protected]

PM 3, Block A, FKJ

Learning Outcomes

• Upon completion of this lecture, student

should be able to:

• Write expressions using the conditional operator

• Differentiate the Increment and Decrement

operator

• Implement selection control using if, if…else

and switch statements

• Implement repetition control in a program by

using while, do… while and for loop

statements,

Contents

Control structures

Selection

structures

If

if

if..else

Nested if

switch

Loops

Structures

while

do..while

for

Selection Structures

Selection

structures

If

if

if..else

Nested if

switch

if statements

• Example

if(x == y)

System.out.println("X is equal to Y");

if(x != y)

System.out.println("X is not equal to Y"); x = y; System.out.println("However, now it is.");

if (condition)

execute_statements;

if.. Else statements

  • Example

if(x == y)

System.out.println("X is equal to Y"); else System.out.println("X is NOT equal to Y");

if (condition)

execute_statements;

else

execute_statements;

if(x != y)

System.out.println("X is not equal to Y"); x = y; System.out.println("However, now it is.");

} else {

System.out.println("X is NOT

equal to Y");}

if.. else..if statements

  • Example

if(x == y)

System.out.println("X is equal to Y"); else if (x !=y) System.out.println("X is NOT equal to Y"); else System.err.println (“ Error Catch!! ");

if (condition)

execute_statements;

else if (condition)

execute_statements;

else

execute_statements;

Nested If statements

  • Example

if (condition) { if (condition) {

}else if (condition){

}else {

}

}

if (coldOutside==true) { if (snowing==true) { wearParka(); } else { wearJacket(); } } else { wearShorts(); }

Selection Structures

  • Problem

What is the output after executing the following

program?

int mask = 0; int count = 0;

if ((5<7 || (++count <10)) && mask++ <1 ) mask = mask + 1; if (6<8 && false) mask = mask + 10; if( !(mask >1) && ++count>1 ) mask = mask + 100;

System.out.println( " Mask is " + mask + "and count is " + count );

Selection Structures

  • Problem

What is the output after executing the following

program?

int mask = 0; int count = 0;

if (5<7 || ++count <1 && mask++ <1 ) mask = mask + 1; if (6<8 && false) mask = mask + 10; if( !(mask >1) && ++count>1 ) mask = mask + 100;

System.out.println( " Mask is " + mask + " and count is " + count );

Selection Structures

  • Problem

What is the output after executing the following

program?

int n=95;

if (n <= 100) if (n > 90) System.out.println("A"); System.out.println("B"); if (n >80) System.out.println("C"); if (n < 50) System.out.println("D"); else System.out.println("E");

Given that n is :

a) 95

b) 90

c) 45

d) 80

e) 50

The switch Statement The case Statement

3 - 20

  • The break statement ends the case statement.
  • The break statement is optional.
  • If a case does not contain a break , then program

execution continues into the next case.

  • The default section is optional and will be

executed if no CaseExpression matches the

SwitchExpression.

3.9 The switch Statement

NoBreaks.java Practice 3

Practice 3: Solution

3.9 The switch Statement

Practice 4

Practice 4 (20 minutes):

A five digit integer is said to be friendly if

  • the leftmost digit is divisible by 1
  • the leftmost two digits are divisible by 2, the leftmost three digit are

divisible by 3

  • the leftmost four digit are divisible by 4, and the leftmost five digits

( the five-digit number itself) is divisible by 5.

For example, the number 42325 is friendly because 4 is divisible by 1,

42 is divisible by 2, 423 is divisible by 3, 4232 is divisible by 4, and 42325

is divisible by 5.

Write a program that prompts for a five-digit integer and determines

whether or not the number is “friendly”. Use JOptionPane for user input

and output.

Compound Assignment Operators

Differences Between Prefix and Postfix

  • When an increment or decrement are the only operations

in a statement , there is no difference between prefix and

postfix notation.

  • When used in an expression:
    • prefix notation indicates that the variable will be incremented or

decremented prior to the rest of the equation being evaluated.

  • postfix notation indicates that the variable will be incremented or

decremented after the rest of the equation has been evaluated.

++x

X++

Nested if statements and Multiple Alternative Decisions

What is the output for the following Program.

public static void main (String[] args){

int mask = 5;

if (++mask > 5) System.out.println("mask:"

  • mask);

public static void main (String[] args){ int mask = 2;

if (mask++ > 2) System.out.println("mask:" + mask);

public static void main (String[] args){ int mask = 2;

if (mask++ > 2) System.out.println("mask:" + mask);

public static void main (String[] args){ int mask = 5;

if (mask++ > 5) System.out.println("mask:" + mask);

Loop Structures

Control

structures

Loops

Structures

while

do..while

for

The while Loop

4 - 28

while(condition)

{

statements ;

}

Write a program that use the while loop to print

“Hello” five times.

The while Loop for Input Validation

4 - 29

  • Input validation is the process of ensuring that user

input is valid.

System.out.print("Enter a number in the " +

"range of 1 through 100: ");

number = keyboard.nextInt();

// Validate the input.

while (number < 1 || number > 100)

System.out.println("That number is

invalid.");

System.out.print("Enter a number in the " +

"range of 1 through 100: ");

number = keyboard.nextInt();

do…while loops

do

{

statement(s);

}while ( condition );

Practice 9 Solution

Write a program to display the following output.

1

21

321

4321

54321

Practice 9 Solution

Write a program to display the following output.

54321

4321

321

21

1

Nested loops

for(int i = 0; i < 10; i++)

for(int j = 0; j < 10; j++)

loop statement;

for(int i = 0; i < 10; i++){

for(int j = 0; j < 10; j++){

loop statements;

}

}

Nested For Loops

The continue Statement

  • The continue statement will cause the currently

executing iteration of a loop to terminate and the

next iteration will begin.

  • The continue statement will cause the evaluation

of the condition in while and for loops.

  • Like the break statement, the continue

statement should be avoided because it makes the

code hard to read and debug

Problem

  • What is the output after executing the following program?

public static void main(String []

args)

{

int i=0;

while ( i < 10 )

{

i++;

if ( i == 5)

break;

System.out.println(i);

}

}

Problem

  • What is the output after executing the following program?

public static void main(String [] args)

{

int i=0;

while ( i < 10 )

{

i++;

if ( i == 5)

continue;

System.out.println(i);

}

}

Deciding Which Loops to Use

• The while loop :

  • Pretest loop
  • Use it where you do not want the statements to execute if

the condition is false in the beginning.

• The do - while loop :

  • Post-test loop
  • Use it where you want the statements to execute at least one time.

• The for loop:

  • Pretest loop
  • Use it where there is some type of counting variable that

can be evaluated.

The Random Class

4 - 45

• Some applications, such as games and simulations,

require the use of randomly generated numbers.

• The Java API has a class, Random, for this purpose.

To use the Random class, use the following

import statement and create an instance of the

class.

4.11 The Random Class

import java.util.Random;

Random randomNumbers = new Random();

Some Methods of the

Random Class

4 - 46

Method Description

nextDouble() Returns the next random number as a double. The number will be within the range of 0.0 and 1.0.

nextFloat() Returns the next random number as a float. The number will be within the range of 0.0 and 1.0.

nextInt() Returns the next random number as an int. The number will be within the range of an int, which is – 2,147,483, to +2,147,483,648.

nextInt(int n) (^) This method accepts an integer argument, n. It returns a random number as an int. The number will be within the range of 0 to n.

4.11 The Random Class

Random Class

Thank You !!

”Effort always is a best Indicator of Interest”