For example, patrons can only be members of a particular health club if they are female and over the age of 21. This problem could be solved by using a nested 'if':
If Gender = 'F'
If Age > 21
Display "Welcome to the club"
Else
Display "Go away"
End If
Else
Display "Go away"
End If
However, a neater solution would use a compound selection statement:
If Gender = 'F' and Age > 21
Display "Welcome to the club"
Else
Display "Go away"
End If
Compound selection can use any of the conditional operators used in simple selection, but can also use 'and' and 'or'.
In 'C' code, 'and' is written as '&&'; 'or' is written as '||'. For example:
if ((Gender == 'F' && Age > 21) || BankBalance >= 100000) { printf("Welcome to the club"); } else { printf("Go away"); }
NOTE 1: Additional brackets may be required to ensure that the meaning is clear.
NOTE 2: You must write full and correct conditional statements:
if(Age > 21 && < 60) is wrong
if(Age > 21 && Age < 60) is correct
NOTE 3: You must think carefully about the logic:
if(Age > 21 || Age < 60) is correct ?????????
No comments:
Post a Comment