If statement
syntax: if ( <expression> ) <statement>
semantic: if <expression> is nonzero (true), then <statement> is executed; otherwise is skipped
- example: if (grade >= 90)
printf("congratulations!\n");
printf("your grade is %d.\n",grade);
- short circuit evaluation: if (0 && b < c)
if-else statement
- example: if (x < y) min = x;
else min = y;
- Nested if and if-else statement
- "dangling else" problem
if (a == 1) if (a == 1)
if (b == 2) { if (b == 2)
printf("***\n"); printf("***\n"); }
else printf("###\n") else printf("###\n")