[go: up one dir, main page]

0% found this document useful (0 votes)
123 views30 pages

OOJ Lecture 12

This document covers nested if/else statements and multiway branching statements in Java. It provides an example of a nested if/else statement to determine voting eligibility based on citizenship and age. It also discusses how multiway if/else statements can be used to select between more than two possible branches. An exercise is included to output different strings based on multiple conditions.

Uploaded by

Thu Thương
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
123 views30 pages

OOJ Lecture 12

This document covers nested if/else statements and multiway branching statements in Java. It provides an example of a nested if/else statement to determine voting eligibility based on citizenship and age. It also discusses how multiway if/else statements can be used to select between more than two possible branches. An exercise is included to output different strings based on multiple conditions.

Uploaded by

Thu Thương
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 30

Lecture 12

Covers
Nested ifelse statements Multiway branching statements

Reading: Savitch 3.1

12/1

Nested ifelse statements

12/2

Nested ifelse statements


The statements in either branch of an ifelse statement may contain another ifelse statement By placing one ifelse statement inside another, we can select between more than two possible branches This type of statement set-up is called nesting
12/3

Example

Voting example
Rule: You can vote in Australia if you are an Australian citizen and 18 years of age or older. Write a program that asks the user whether they are a citizen. If they are not, display You are not eligible to vote . Otherwise, ask for their age, and display You are too young to vote or You are eligible to vote as the case may be.

12/4

Example

Algorithm
Get users citizenship IF NOT Australian citizen THEN Display You are not eligible to vote ELSE Get users age IF younger than 18 THEN Display You are too young to vote ELSE Display You are eligible to vote ENDIF ENDIF

12/5

public static void main(String[ ] args) { Scanner keyboard = new Scanner(System.in); System.out.print("Are you a citizen of Australia? "); String citizenString = keyboard.nextLine(); char citizen = citizenString.charAt(0); if (citizen != 'y') { System.out.println("You are not eligible to vote"); } else { System.out.print("How old are you? "); int age = keyboard.nextInt( ); if (age < 18) { System.out.println("You are too young to vote"); } else { System.out.println("You are eligible to vote"); } } }

12/6

Example

Program output

Are you a citizen of Australia? yes How old are you? 31 You are eligible to vote Are you a citizen of Australia? no You are not eligible to vote Are you a citizen of Australia? yes How old are you? 16 You are too young to vote 12/7

Multiway ifelse statements

12/8

Multiway ifelse statements

When an ifelse structure is created to decide between more than two mutually exclusive paths, it is termed a multiway branching statement

12/9

Exercise

Problem
Write Java code that outputs pass if the integer variable mark is at least 50, that outputs special exam if mark is less than 50 and the variable special is true, and outputs fail if mark is less than 50 and special is false

12/10

Exercise

Solution

if (mark >= 50) { System.out.println("pass"); } else { if (special == true) System.out.println("special exam"); else System.out.println("fail"); }

12/11

Exercise

Minor improvements
implicit == operator and left-aligned format

if (mark >= 50) { System.out.println("pass"); } else if (special) { System.out.println("special exam"); } else { System.out.println("fail"); }

12/12

Class exercise

What is produced by this code?

int extra = 2; if (extra < 0) { System.out.println("small"); } else if (extra == 0) { System.out.println("medium"); } else { System.out.println("large"); }

12/13

Further examples & exercises

12/14

Class exercise

What is produced by this code?

int x = 2; System.out.println("Start"); if (x <= 3) if (x != 0) System.out.println("Hello from the second if"); else System.out.println("Hello from the else"); System.out.println("End"); 12/15

Class exercise

What is produced by this code?

int x = 4; System.out.println("Start"); if (x <= 3) if (x != 0) System.out.println("Hello from the second if"); else System.out.println("Hello from the else"); System.out.println("End"); 12/16

Class exercise

What is produced by this code?

int x = 4; System.out.println("Start"); if (x <= 3) if (x != 0) System.out.println("Hello from the second if"); else System.out.println("Hello from the else"); System.out.println("End"); 12/17

Matching elses
Each else is matched with the nearest unmatched if It can become confusing to work out which else matches which if, especially when some if statements do not have an else part Always bracket your if statements for clarity and readability

12/18

Bracket for clarity


int x = 4; System.out.println("Start"); if (x <= 3) { if (x != 0) { System.out.println("Hello from the second if"); } else { System.out.println("Hello from the else"); } } System.out.println("End");

12/19

Class exercise

Problem
Write a multiway ifelse statement that classifies the value of an integer variable n into either less than 0, between 0 and 100 inclusive, and greater than 100 Print sensible messages

12/20

Solution

12/21

Example
The integer variable month contains a value representing a month of the year: 1 = January, 2 = February, , 12 = December Write a multiway ifelse statement to display the name of the month based on the content of the variable month If month does not contain an integer value between 1 and 12, an error message should be displayed instead

12/22

Example
import java.util.*; public class MonthName { public static void main(String[ ] args) { Scanner keyboard = new Scanner(System.in); int month; System.out.println("Enter the month [1-12]"); month = keyboard.nextInt( ); if (month == 1) { System.out.println("January"); } else if (month == 2) { System.out.println("February"); } else if (month == 3) { System.out.println("March"); }

12/23

Example
else if (month == 4) { System.out.println("April"); } else if (month == 5) { System.out.println("May"); } else if (month == 6) { System.out.println("June"); } else if (month == 7) { System.out.println("July"); } else if (month == 8) { System.out.println("August"); }

12/24

Example
else if (month == 9) { System.out.println("September"); } else if (month == 10) { System.out.println("October"); } else if (month == 11) { System.out.println("November"); } else if (month == 12) { System.out.println("December"); } else { System.out.println("Not a valid month"); } } }

12/25

Class exercise

Write a Java code segment that outputs to screen the message Red Dwarf, White Star or Blue Moon depending on whether the value of the String object hue is red, white or blue respectively. If hue has any other value, output Black Hole.

12/26

Solution

12/27

Class exercise

Write a Java code segment that checks if an integer variable key is odd or even.
If the variable is even and divisible by 4, output to screen the message even again, otherwise output only even. If the variable is odd and negative, output to screen the message negatively odd, otherwise output positively odd.

12/28

Solution

12/29

Next lecture
Blocks and scope Branching statements

The switch statement The conditional operator

12/30

You might also like