if / else statements

A restaurant gives a discount to children AND seniors. When we write the program we test if someone is a child OR a senior. One person can't be both a child and a senior.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Children and Seniors get discount
package meal;
import java.util.*;
public class Discount {
	public static void main(String[] args) {
		Scanner keyboard=new Scanner(System.in); //do just once
		int age;
		System.out.print("Enter your age:");
		age=keyboard.nextInt();
		if (age <=12 || age>=65)
		  System.out.println("You get a discount");
		else 
		  System.out.println("You do NOT get a discount");
	}//main
}//class
CODE

When you write a program like this, it is important to clarify exactly what is meant by "child" and "senior." In this example 12 is considered a child and 65 is considered a senior.

Good test data would include age<12, age==12, age between 13 and 64, age==65, and age>65.

Challenge: Try writing this to test for who doesn't get a discount in the if statement.

NEXT: