object oriented programmingswitch the switch statement is java’s multiway branch statement. the...

27
Object Oriented Programming Lecture # 9-10

Upload: others

Post on 24-Jun-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Object Oriented ProgrammingSwitch The switch statement is Java’s multiway branch statement. The expression must be of type byte, short, int, or char. If none of the constants matches

Object Oriented Programming

Lecture # 9-10

Page 2: Object Oriented ProgrammingSwitch The switch statement is Java’s multiway branch statement. The expression must be of type byte, short, int, or char. If none of the constants matches

Contents

Control Structures

Conditional statements

If

If . . .else

Switch

Nested If

Iteration Statement

For

While

Do . . . while

Page 3: Object Oriented ProgrammingSwitch The switch statement is Java’s multiway branch statement. The expression must be of type byte, short, int, or char. If none of the constants matches

Control Structures

The execution of the program is linear.

Control Structures are used to alter/change the flow of program.

Page 4: Object Oriented ProgrammingSwitch The switch statement is Java’s multiway branch statement. The expression must be of type byte, short, int, or char. If none of the constants matches

Selection Statements

There are three selection statements supported by java

1. If

2. If . . .else

3. switch

Page 5: Object Oriented ProgrammingSwitch The switch statement is Java’s multiway branch statement. The expression must be of type byte, short, int, or char. If none of the constants matches

Selection Statements

Page 6: Object Oriented ProgrammingSwitch The switch statement is Java’s multiway branch statement. The expression must be of type byte, short, int, or char. If none of the constants matches

Selection Statements

Page 7: Object Oriented ProgrammingSwitch The switch statement is Java’s multiway branch statement. The expression must be of type byte, short, int, or char. If none of the constants matches

Selection Statements

Page 8: Object Oriented ProgrammingSwitch The switch statement is Java’s multiway branch statement. The expression must be of type byte, short, int, or char. If none of the constants matches

Selection Statements

Page 9: Object Oriented ProgrammingSwitch The switch statement is Java’s multiway branch statement. The expression must be of type byte, short, int, or char. If none of the constants matches

Selection Statements

Nested if

A nested if is an if statement that is the target of another if or else.

if(condition1){

if(condition2){}if(condition3){}

}

Page 10: Object Oriented ProgrammingSwitch The switch statement is Java’s multiway branch statement. The expression must be of type byte, short, int, or char. If none of the constants matches

Selection Statements

Page 11: Object Oriented ProgrammingSwitch The switch statement is Java’s multiway branch statement. The expression must be of type byte, short, int, or char. If none of the constants matches

Selection Statements

Page 12: Object Oriented ProgrammingSwitch The switch statement is Java’s multiway branch statement. The expression must be of type byte, short, int, or char. If none of the constants matches

Selection Statements

Page 13: Object Oriented ProgrammingSwitch The switch statement is Java’s multiway branch statement. The expression must be of type byte, short, int, or char. If none of the constants matches

Selection Statements

Page 14: Object Oriented ProgrammingSwitch The switch statement is Java’s multiway branch statement. The expression must be of type byte, short, int, or char. If none of the constants matches

Selection Statements

Switch

The switch statement is Java’s multiway branch statement.

The expression must be of type byte, short, int, or char.

If none of the constants matches the value of the expression, then the default statement is executed.

The default statement is optional.

Page 15: Object Oriented ProgrammingSwitch The switch statement is Java’s multiway branch statement. The expression must be of type byte, short, int, or char. If none of the constants matches

Selection Statements

Page 16: Object Oriented ProgrammingSwitch The switch statement is Java’s multiway branch statement. The expression must be of type byte, short, int, or char. If none of the constants matches

Class Activity

Write a program that prints the designation of the employee according to their pay scale:

10000 – 20000 Manager Operations 20000 – 30000 Manager 30000 – 40000 Area Manager 40000 – 50000 Regional Manager

Page 17: Object Oriented ProgrammingSwitch The switch statement is Java’s multiway branch statement. The expression must be of type byte, short, int, or char. If none of the constants matches

Iteration Statements

While

A while statement repeats action again and again as long as a controlling boolean expression is true.

When condition becomes false, control passes to the next line of code immediately following the loop.

Syntax

while(condition) {// body of loop

}

Page 18: Object Oriented ProgrammingSwitch The switch statement is Java’s multiway branch statement. The expression must be of type byte, short, int, or char. If none of the constants matches

Iteration Statements

Page 19: Object Oriented ProgrammingSwitch The switch statement is Java’s multiway branch statement. The expression must be of type byte, short, int, or char. If none of the constants matches

Iteration Statements

Page 20: Object Oriented ProgrammingSwitch The switch statement is Java’s multiway branch statement. The expression must be of type byte, short, int, or char. If none of the constants matches

Iteration Statements

Do...While

The body of a do-while loop is always executed at least once.

Syntax

do{First_StatementSecond_Statement. . .Last_Statement

} while(Boolean_Expression);

Page 21: Object Oriented ProgrammingSwitch The switch statement is Java’s multiway branch statement. The expression must be of type byte, short, int, or char. If none of the constants matches

Iteration Statements

Page 22: Object Oriented ProgrammingSwitch The switch statement is Java’s multiway branch statement. The expression must be of type byte, short, int, or char. If none of the constants matches

Iteration Statements

Page 23: Object Oriented ProgrammingSwitch The switch statement is Java’s multiway branch statement. The expression must be of type byte, short, int, or char. If none of the constants matches

Iteration Statements

For

Syntax

for(initialization; condition; iteration) {// body}

The loop first starts, the initialization portion of the loop is executed and sets the value of the loop control variable.

The condition is evaluated and this must be a Boolean expression.

Iteration is an expression that increments or decrements the loop control variable.

Page 24: Object Oriented ProgrammingSwitch The switch statement is Java’s multiway branch statement. The expression must be of type byte, short, int, or char. If none of the constants matches

Iteration Statements

Page 25: Object Oriented ProgrammingSwitch The switch statement is Java’s multiway branch statement. The expression must be of type byte, short, int, or char. If none of the constants matches

Iteration Statements Nested loops

Page 26: Object Oriented ProgrammingSwitch The switch statement is Java’s multiway branch statement. The expression must be of type byte, short, int, or char. If none of the constants matches

Class Activity

Generate a series of Odd number and even number using for and while loop.

Generate alphabets from A to Z. ASCII code for A=65 and Z=90.

Calculate factorial when a number is supplied on runtime.

Page 27: Object Oriented ProgrammingSwitch The switch statement is Java’s multiway branch statement. The expression must be of type byte, short, int, or char. If none of the constants matches

Thank You!