Lesson 4 - If statements4.1 If statement
Example ex4-1 introduces a new statement which can be used to make a decision. The 'if' statement has the syntax: if (condition) statement; Where 'condition' is a test on the contents of one or more variables, and 'statement' is some code that should only be executed in circumstances when the test is passed. In the example, the program only outputs the string "Thank you" when the variable 'value' contains a number which is less than 10. In general the condition can be a combination of tests of equality '==' (N.B. two equals signs), less than '<', greater than '>', less than or equals '<=', greater than or equals '>=', or not equal '!='. Tests can be bracketed with parentheses, and combined with the 'and' operation using the symbol '&&', with the 'or' operation using the symbol '||', and with the 'not' operation using the symbol '!'. Thus a test of suitability for jury service might be: ((age >= 21)&&(age < 65)) That is: age greater or equal to 21 and less than 65. |
||
4.2 If-else statement
Example ex4-2 demonstrates a variant of the 'if' statement which has a single test condition and two statements. It has the syntax: if (condition) statement1; else statement2; With this form, the condition is evaluated and tested for truth. If the test condition is true, then statement1 is executed. If the test condition is false, then statement2 is executed. In the example, if 'value' contains a number less than 10 then the program prints 'Thank you', otherwise it prints 'Can't you follow instructions'. You can see that it must always be the case that one of the two statements is performed. |
||
4.3 Compound if-else statement
Example 4-3 shows that whole blocks of instructions can be controlled by an 'if' statement, not just a single statement. In general wherever we can have a single statement, C++ allows us to put a block of statements enclosed in '{' .. '}'. Thus the syntax of the if statement operating on a block would become: if (condition) { statement1; statement2; etc } and the syntax of the if-else statement operating on two blocks would become: if (condition) { statement1; statement2; etc } else { statement3; statement4; etc } In the example, two instructions are executed if 'value' contains a number less than 10, and a different couple of instructions are executed if this is not the case. Just a reminder here: the layout of the source code with spaces and tabs does not affect its meaning in any way. |
||
4.4 Chained if-else statement
Example ex4-4 demonstrates how 'if-else' statements can be used to choose between more than two alternatives. In this example, the program needs to choose between four responses, depending on the numerical value of 'age'. You should be able to follow the logic here as a 'chain' of tests: if the age is less than 13 then the person is a child, otherwise if the age of the person is less than 20 then the person is a teenager, otherwise if their age is less than 65 they are an adult. If none of these tests are true, then the person is a senior citizen. Clearly you have to think carefully about the order in which you do these tests. What is wrong with this variant:? |
||
if (age < 20) cout << "You are a teenager." << endl; else if (age < 13) cout << "You are a child." << endl; or this:? if (age < 13) cout << "You are a child." << endl; if (age < 20) cout << "You are a teenager." << endl; |
||
4.5 Exercisesa. Write a program (inorder.cpp) which reads two numbers and reports whether the two numbers are in ascending size, as in:inorder Enter two numbers 70 30 These numbers are not in ascending order. inorder Enter two numbers 20 40 These numbers are in ascending order. small Enter three numbers : 17 12 15 The smallest was: 12 |