I start with If-statements with an else and then only later explain that the else is optional.
The line directly above the else has no semi-colon (;)
If there are more than one line of code a begin and end is needed.
An if an its else NEVER share a begin and end. Each needs its own set.
Make a comment next to each end to indicate which part of the If it belongs to - end//if or end;//else - this helps to find logical errors.
Always start with the variable that you are testing and then the condition - if iNum >= 5 then and NOT if 5 <=iNum then - this helps you to read the if-statement and understand what it is doing.
Remember to start with the < or > before the =. => or =< will NOT work but <= and >= will.
Always take note of which number is included in the condition. Read the question twice to be sure not to lose this easy mark.
iNum < 5 - 5 is excluded
iNum <= 5 - 5 is included.
Remember there are many real numbers between the real numbers 4 and 5. If the number needed to be greater or equal to 5 for a real variable if rNum > 4 then will be incorrect. All the decimal values greater than 4 (for example 4.000000000000000000000000000001) will make this condition true.
Remember that testing string variables will be case sensitive. If sPass = 'Password' then and If sPass = 'password' then will not produce the same output for the same input.
Use indentation to show what is inside the if / else
Indent between begin and end
After some practice we then continue with nested-if statements
Always sort the values that are given in either ascending or descending order.
Tackle the question from top to bottom and do not jump around in the question.
The last else usually does not need a condition. All that is left is, is the last option. For example you either have an A, pass or a fail. If you do not have an A and you also did not pass, the only option left will be a fail. If you add a condition incorrectly, for the fail, you then lose a mark for this error that you added unnecessary. In the example below learners often add a condition after the last else and will make a mistake like for example if rAve <=49 which then excludes values between 50 and 49.
Careful not to do more than the question asked. If the question did not specify that you need to ensure that invalid values (for percentages a value less than 0 or more than 100) need to be accommodated for, you do not need to code for this. In this instance you may assume that the user entered a value within the range 0 to 100.
Indent every nested-if under an else but keep the If and the else that belongs to each other aligned.
Indent every If under and else but keep the If and else that belong to each other together
In Dandel10n Delphi Book 1 we use finding the largest and smallest value to practice nested-if statements with. I use this opportunity to explain algorithms (See algorithm notes) as well. I give the learners the algorithm below and then they need to follow and code it. It is important for the learners to learn the code for finding the smallest and largest now, as it is needed in grade 11 and 12 again with some changes.
We need to set the value of HighScore to some value in order to use it in the nested-if condition - If Score > HighScore then
The algorithm will work for all ranges of values. In the example below you could have set HighScore to 0 only if the scores are not less than 0. I always prefer to teach one method that works for all solutions and the algorithm below will work even if it was negative scores to find the highest of.
Finding the highest but not needing to count all learners
Counting and finding the highest. Note the counter condition
Now I introduce multiple conditions with AND and OR.
Every condition needs its own set of brackets.
If you are testing the same variable you need to repeat the variable in each condition.
For a condition AND to be TRUE - ALL the conditions need to be TRUE.
The only way that an If-statement with an OR between the condition can be FALSE is if ALL the conditions are FALSE.
Remember about the order of operations. You can groups conditions together making use of brackets as in the examples below.
Difference between AND and OR
Each condition needs brackets and the variable needs to be repeated for each condition
This if-statement will be true for learners with an A for both IT AND Physical Science OR any learner with an average above 90 regardless of their IT and physical science marks.
This condition will be true for learners who have an average above 90 as well as either an A for IT OR Physical Science