In this IB Computer Science lesson you will be learning about:
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.
This would be the flow chart of program flow when using an if statement
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
Be careful:
The following program asks a user to enter their age and then outputs if they are able to get their licence to drive
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
The program on the last page would have been a good example of where to use an if-else statement
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.
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.
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.
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
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
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.
Check out our programing in python courses that focus on high school level coding.
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