This document discusses if statements in CS111 Lab, including the structure of if statements, common checks used in if statements like greater than, less than, equal to, not equal to, and examples of using else, else if, compound comparisons with AND and OR operators, and negation.
2. If statements
? Structure of if statement:
? if (true or false to check) {
instruction or output;
}
OR (if only one instruction:
? if (true or false to check) instruction or
output;
3. Common Checks
? x>y ¡°x is greater than y?¡±
? x<y ¡°x is less than y?¡±
? x==y ¡°x is equal to y?¡±
? x!=y ¡°x is not equal to y?¡±
? x>=y ¡°x is greater than or equal to y?¡±
? x<=y ¡°x is less than or equal to y?¡±
? x%2==0 ¡°x is divisible by two (even).¡±
4. Using else
? if (true or false to check) {
instruction or output;
}
else {
instruction or output;
}
5. else if
? if (true or false to check) {
instruction or output;
}
else if (another check) {
instruction or output;
}
else {
instruction or output;
}
6. Compound Comparisons
? Use parentheses if clarity is needed.
? Use && for AND and || for OR.
? Examples:
if (x>0 && x<100) ¡°x is greater than 0 and
less than 100.¡±
If (x<0 || x>100) ¡°x is less than 0 or greater
than 100.¡±
7. Negation
? You can also check the opposite value of
a condition using !
? Example:
? if (!(x%2==0)) ¡°x is odd (not even).¡±