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 1

IB Computer Science Learning Goals

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

  • Writing programs that use if – else structures using numerical comparisons

An If Statement

So far you have only written small pieces of code where each statement is executed one after the other in the order they are written. However there are times when you would like to change the way your code behaves based on a decision, this is known as a selection structure

A selection structure is a point in the program where two values are compared (usually variables and/or object attributes). In most programming languages, selection structures are implemented as an IF statement

Based on that comparison, the flow of execution is directed to a particular section of code.

  • Comparisons MUST result in a TRUE or FALSE answer
  • When an “if” statement asks a question and the answer is “true”, it executes a group of statements once and then continues with the rest of the program
  • When the answer to that question is “false”, that group of statements is not executed at all
  • Basically an “if” statement asks the question: “Should I execute this group of statements once?”

This would be the flow chart of program flow when using an if statement

 

 

IB Computer Science Java Selection Flow chart

Creating a Simple Comparison

Comparison Operator
<  less than
<= less than or equal
== equals
!= not equals
>= greater than or equal
> greater than

Example:

Suppose for the following example w, x, y, and z are variables used in a program

x < 5
The answer to this question is “true” if the value stored in x is strictly less than 5, otherwise the answer is “false”


y <= z
The answer to this question is “true” if the value stored in y is greater than or equal to the value stored in z, otherwise the answer is “false”


w == x
The answer to this question is “true” only if the values in x & y are identical, otherwise the answer is “false”


y != w
The answer to this question is “true” only if the values in y & w are different, otherwise the answer is “false”


z >= 7
The answer to this question is “true” if the value stored in z is greater than or equal to 7, otherwise the answer is “false”


x > -5
The answer to this question is “true” if the value stored in x is strictly greater than -5

 

Using Comparisons in a Java Program

IB Computer Science Java If Statement Structure

Be careful:

  • Don’t put a semicolon at the end of the if statement.
  • When using a comparison statement you must guarantee that those variables have a value or you will get a compiler error.
  • You must always have an opening { and closing } brace unless there is only one line to execute for the if.
  • Indent all code inside the if

The following program asks a user to enter their age and then outputs if they are able to get their licence to drive

IB Computer Science If Statement Example Java
IB Computer Science Java If Example Output 1
IB Computer Science Java If Example Output 2

Using an If - else Program Structure

Sometimes in a piece of code you want a certain number of statements to execute if a comparison is true and another set of statements to execute if the comparison is false. In that scenario you can execute an if – else statement

IB Computer Science If Else Flow Chart

The program on the last page would have been a good example of where to use an if-else statement

IB Computer Science Java If Else Example Driving Age

You can also have if statements within other if statements or within other if – else statements. These are often called nested if statements or if-else-if statements
The following example is an interface that a restaurant might use to have their servers enter the meals ordered.

IB Computer Science Java If else If

Here is another example using if statements within other if statements.

A leading manufacturer of microwave ovens recommends that when heating two items, add 50% to the heating time, and when heating three items double the heating time. Heating more than three items is not recommend.

Write a program that asks the user for the number of items and the single item heating time. The program then outputs the recommended heating time.

IB Computer Science Java Nested if

IB Computer Science Practice

1. Store Discount
During a special sale at a store, a 10% discount is taken off of purchases over $10.00. Create a GUI that prompts the user for the amount of purchases and then returns the discounted price.

IB Computer Science Store GUI Example2

2. Volume and Surface Area
Create a geometry program that lets the user choose to calculate the Surface Area OR Volume of any of the following

  • Rectangular Prism
  •  Cylinder
  • Sphere

The user should first select either Surface area OR Volume, then should be offered the choice of what shape.

3. Cell phone Plans

There are 2 cell phone plans that are available

IB Computer Science Cell Phone Plan Question

Write a program that will input the number of minutes used for each type (Daytime, Evening, Weekend) and then output what plan is cheaper and how much cheaper it is. If they cost the same amount state that both are equally good. You can output the cost in cents if you want but in dollars & cents would be better

4. Multiple Choice Quiz
Create a 4 question test consisting of a combination of multiple choice and true and false questions. After each question output if the user was correct or not. At the end of the program output the percentage he got correct.

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