Conditions can be chained with "and", "or", and so on, which are expressed as follows
Operator, Meaning
&& = And
|| = Or
! = Not
So we can write things like
if ((option==1) && (user==2)) ...
if ((option==1) || (option==3)) ...
if ((!(option==correctOption)) || (pressedKey==ESC)) ...
Suggested exercises:
- Create a program that asks the user a numbers and answers if it is a mutiple of 2 or a multiple of 3.
- Create a program that asks the user for two integers and answers "One number is positive," "Both numbers are positive" or "None of the numbers is positive," as appropriate.
- Create a program that asks the user for three numbers and displays the greatest one.
- Create a program that asks the user to enter two numbers and says if they are equal or, otherwise, which one is the greatest.
No comments:
Post a Comment