If Else - tstorrnetnz/teaching2022 GitHub Wiki

How to think like a computer scientist (HTTLACS)

Otago Workbook L2

Learning Aim: Use if/else statements to perform selection

Selection using if/else is a straight forward concept from L1 and L2.

comparison operators if/else statements

For you to do:

These problems look at β€œif” statements in Java - also called conditionals.

The resources to help you are below plus video help here - this is a good place to start if you are stuck. The problems below use a few lines of code so that you can enter text into your program when it runs. They use a scanner. For example:

Scanner inp = new Scanner(System.in);

System.out.print("In:");

int num = inp.nextInt();

Use Chapters 4 & 5 of the Otago University Workbook (Level 2) to support you with the following problems. Chapters 3 & 5 (for problems 15 - 17) with the addition of Chapter 9 (problem 18) in HTTLACS are also useful.

Line 1 creates a scanner variable with the value inp

Line 2 prints out β€œIn:”

Line 3 uses the scanner inp to capture the next integer input and assign it to the int variable num.

015 https://repl.it/@trevorstorr/202015#Main.java

016 https://repl.it/@trevorstorr/2021016#Main.java

017 https://repl.it/@trevorstorr/202117#Main.java

018 https://repl.it/@trevorstorr/202118#Main.java

Extension work:

OUWB chapter 5 problems 9 - 13

https://www.codesdope.com/practice/java-decide-if-or-else/

Do the level 1 and level 2 problems