Arithmetic Expressions
float result = (1+2)-3*2/2;
The program will store the resulting value of this expression in a float variable result. Your programs are intelligent enough to perform the evaluation following the PEMDAS.
Shortcuts
i++; // i = i + 1, adds 1 to the value of the variable and stores it back to the variable i--; // i = i - 1 i += 2; // i = i + 2, basically, 2 can be any number
Logical Expressions
The next basic and most common thing you should learn is to evaluate logical expressions.
|| for logical OR
&& for logical AND
! for negation (returns 0 for positive values, 1 otherwise)
Logical operations used to evaluate expressions.
1 2 3 4 5 6 | int gameOver = 1; int win = 0; if (gameOver && !win) { printf("Sorry, you lose!"); } |
1 2 3 4 | int hasBeenKickedOut = 1; int isSuspended = 1; bool isNotGoingToSchool = hasBeenKickedOut || isSuspended; |
I think that
was my first time (if not, then one of the few times) I stored the resulting
value of a logical expression in a variable. Usually it is used inside an if
or else-if statement or as a return value of a function (your will know more
about function later in this course).
1 2 3 | if (hasBeenKickedOut || isSuspended) { isNotGoingToSchool = 1; } |
Previous Topic Course Outline Next Topic
No comments:
Post a Comment