IB Computer Science Unit 1
IB Computer Science Unit 2
IB Computer Science Unit 3
IB Computer Science Unit 4
IB Computer Science Unit 5
IB Computer Science Unit 6

IB Computer Science Java Selection Structures Part 2

IB Computer Science Learning Goals

In this IB Computer Science lesson you will be learning about:

  • Write programs that use if – else structures that use more than 1 comparison
  • Write programs that can compare Strings together

Compound Comparisons

Comparisons are joined together using the following operators

  • &&  represents AND
  • || represents OR
  • ! represents NOT

Example:

An AND comparison needs both comparisons to be TRUE to produce another TRUE

  • TRUE && TRUE would be TRUE
  • TRUE && FALSE would be FALSE
  • FALSE && TRUE would be FALSE
  • FALSE && FALSE would be FALSE

An OR comparison needs at least one of the comparisons to be TRUE in order to produce a TRUE

  • TRUE || TRUE would be TRUE
  • TRUE || FALSE would be TRUE
  • FALSE || TRUE would be TRUE
  • FALSE || FALSE would be FALSE

A NOT comparison checks if a comparison is not TRUE

  • ! FALSE would be TRUE
  • ! TRUE would be FALSE

Here is a program that asks the user to enter a number then checks if a number is less than that

 

IB Computer Science Java Compound Comparison 1

Sample Input and Outputs

IB Computer Science Java compound comparison

Both comparisons were true so the overall result of the if statement was true

IB Computer Science Java Compound Comparison

The first comparison was false so the overall result of the if statement was false

IB Computer Science Java Compound Comparison

The second comparison was false so the overall result of the if statement was false

Here is a program that gives the user a choice of colors to pick from and outputs a fun message

IB Computer Science Java Menu Solution

Sample Input and Outputs

First Comparison was true so the if statement executed

Both Comparisons were false so the else statement executed

IB Computer Science Java Compound Comparison Output
IB Computer Science Java Menu Output

IB Computer Science Java Compound Comparison Output

Comparing Strings

Strings are not generally compared using the double equals size in Java. This is because they are actually objects and some unexpected results might happen. You will learn more about that later. Instead you must use the “equals” or “EqualsIgnoreCase” Method

Equals Method

Checks if two strings are identical. Upper and lower case letters are treated differently.

IB Computer Science Java String Equals

EqualsIgnoreCase

Checks if two strings are identical. Upper and lower case letters are NOT treated differently.

IB Computer Science Java Equals Ignore Case

IB Computer Science Practice

1. Grade Level
Write a GUI that inputs the students grade (as a percentage) and outputs what Ontario Level they are at.
Level 4 – 80% – 100%
Level 3 – 70% – 79%
Level 2 – 60% – 69%
Level 1 – 50% – 59%
Failure – 0% – 49%

2. Roller Coaster
A roller coaster has certain restrictions. You can ride only if you are taller than 122 cm tall or have a mature individual older than 16.

Ask the user for 2 things:

  • There height (an integer),
  • If they have an mature individual over 16 riding with them (“y” / “Y” or “n”/ “N”)

Output if they are allowed to ride or not.

3. Favorite Color
Rewrite Mr. Rogers Favorite Color program using a String Comparison.

4. Guessing Game GUI – WRITE THIS USING A GUI

 

IB Computer Science Java Guess a Number

Looking to Learn More about Computer Science and Coding?

Check out our programing in python courses that focus on high school level coding.  

  • Grade 11 Computer Science ICS3U
  • Grade 12 Computer Science ICS4U

These courses are complete with interactive coding lessons, teacher led videos, and more practice questions with complete solutions

Return To International Baccalaureate Computer Science Main Page