Upcoming SlideShare
×

# If statements

1,229 views

Published on

Published in: Sports, Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
1,229
On SlideShare
0
From Embeds
0
Number of Embeds
970
Actions
Shares
0
5
0
Likes
0
Embeds 0
No embeds

No notes for slide

### If statements

1. 1. CS111 Lab Decisions, Decisions Instructor: Michael Gordon
2. 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. 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. 4. Using else  if (true or false to check) { instruction or output; } else { instruction or output; }
5. 5. else if  if (true or false to check) { instruction or output; } else if (another check) { instruction or output; } else { instruction or output; }
6. 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. 7. Negation  You can also check the opposite value of a condition using !  Example:  if (!(x%2==0)) “x is odd (not even).”