ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
CS111 Lab
Decisions, Decisions
Instructor: Michael Gordon
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;
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).¡±
Using else
? if (true or false to check) {
instruction or output;
}
else {
instruction or output;
}
else if
? if (true or false to check) {
instruction or output;
}
else if (another check) {
instruction or output;
}
else {
instruction or output;
}
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.¡±
Negation
? You can also check the opposite value of
a condition using !
? Example:
? if (!(x%2==0)) ¡°x is odd (not even).¡±

More Related Content

If statements

  • 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).¡±