if
/else
statement — conditional execution
switch
statement — conditional execution
while
loop — repeated execution
do
…while
loop — repeated execution
for
loop — repeated execution
if
/else
construction:
if (condition) statement; // Do this if the condition is true
if (condition) statement; // Do this if the condition is true else statement; // Do this if the condition is false
else statement;
part is optional.
x=3
or printf
,
if
/else
,
{ }
.
if
statement uses a condition
to determine whether certain statements are executed.
1
, 15
, -12
, 92.34
).
Happens first |
( ) |
* / % |
+ - |
> <= >= |
== != |
! |
&& |
| | |
= |
Happens last |
Operation | Meaning |
---|---|
a == b | a is equal to b |
a != b | a is not equal to b |
a < b | a is less than b |
a > b | a is greater than b |
a <= b | a is less than or equal to b |
a >= b | a is greater than or equal to b |
!a | true if a is false, false if a is true |
a && b | true if a and b are both true |
a | | b | true if a or b is true |
x | !x |
---|---|
false | true |
true | false |
x | y | x && y | x | | y |
---|---|---|---|
false | false | false | false |
false | true | false | true |
true | false | false | true |
true | true | true | true |
What is the output of the following program?
int x=5, y=0; printf("x=%d\n", x); printf("y=%d\n", y); printf("!x=%d\n", !x); printf("!y=%d\n", !y); printf("x||y=%d\n", (x||y)); printf("x&&y=%d\n", (x&&y));
x=5 y=0 !x=0 !y=1 x||y=1 x&&y=0
printf("1 < 2 = %d\n", 1 < 2); printf("2 >= 4 = %d\n", 2 >= 4); printf("1<2 && 2<-3 = %d\n", 1<2 && 2<-3); printf("1<2 || 2<-3 = %d\n", 1<2 || 2<-3);
1 < 2 = 1 2 >= 4 = 0 1<2 && 2<-3 = 0 1<2 || 2<-3 = 1
int a=2, b=4; if (a < b) printf("Good\n"); else printf("Bad\n");
Good
if
-else
— an else
statement is
matched to the closest if
in the same block.
if (condition) if (condition) { if (condition2) if (condition2) statement; statement; else } statement; else statement;
#include <stdio.h> int main() { int x,y,z; char smallest; printf("Enter x: "); scanf("%d", &x); printf("Enter y: "); scanf("%d", &y); printf("Enter z: "); scanf("%d", &z); if (x < y) if (x < z) smallest = 'x'; else if (y < z) smallest = 'y'; else smallest = 'z'; printf("%c is smallest\n", smallest); return 0; }
x
, y
and z
.
You can compare characters to each other:
char grade='C'; if (grade > 'C') // failing grade? printf("You'll be back!\n");
'A' < 'B' < 'C' < … < 'X' < 'Y' < 'Z'
'a' < 'b' < 'c' < … < 'x' < 'y' < 'z'
'0' < '1' < '2' < … < '7' < '8' < '9'
'A' == 'a'
. They’re different.
'a' > 'D'
, or if '@' < '$'
.
4 == '4'
. 4
is an int
. '4'
is a char
.
Need to handle reading in the enter key!
#include <stdio.h> int main() { char letter, enter; printf("Enter a letter: "); scanf("%c%c", &letter, &enter); while (letter != '.') { if (letter >= 'a' && letter <= 'z') printf("%c is LOWER case\n", letter); else if (letter >= 'A' && letter <= 'Z') printf("%c is UPPER case\n", letter); else printf("%c is not in my alphabet.\n", letter); printf("Enter another letter (or . to quit): "); scanf("%c%c", &letter, &enter); } return 0; }
Here are some things that you’d think might work, but they don’t:
Wrong | Right |
---|---|
if (x==1| |2) | if (x==1 | | x==2) |
if (a<b<c) | if (a<b && b<c) |
The switch
construct lets you pick one from a number of alternatives:
switch (some value) { case this value: statements; break; case that other value: statements; break; default: statements; }
The value must be an int
or char
, or an expression,
e.g., j+2
, or toupper(c)
.
A switch
statement makes a multi-way decision:
int year; scanf("%d", &year); switch (year) { case 1957: printf("Finest year in all history\n"); break; case 1969: printf("One small step for [a] man\n"); break; case 2003: printf("Punks born in this year\n"); break; case 2004: printf("Slackers born in this year\n"); break; case 2005: printf("Clowns to the left of me\n"); break; case 2006: printf("Jokers to the right\n"); break; case 2025: printf("Welcome to the FUTURE!!!\n"); break; default: printf("Boring year\n"); break; }
char grade = 'A'; switch (grade) { case 'A': printf("Good Job!\n"); break; case 'B': printf("Also Good.\n"); break; default : printf("Study more.\n"); }
Good Job!
char grade = 'C'; switch (grade) { case 'A': printf("Good Job!\n"); break; case 'B': printf("Also Good.\n"); break; default : printf("Study more.\n"); }
Study more.
char grade = 'A'; switch (grade) { case 'A': printf("Good Job!\n"); case 'B': printf("Also Good.\n"); default : printf("Study more.\n"); }
Good Job! Also Good. Study more.
The break
statement is important. Without it, execution
would continue through the statements of the switch
until the end.
#include <stdio.h> int main() { char letter, enter; printf("Enter a character: "); scanf("%c%c", &letter, &enter); switch (letter) { case 'a': case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U': printf("A vowel: %c\n", letter); break; default: printf("Not a vowel: %c\n", letter); } return 0; }
Sometimes we want to leave out the break
statement.
if (a > b) if (b > c) min = c; else min = a;
if (a > b) { if (b > c) min = c; } else min = a;
int a = 5; int b = 9; if (a = b) val = b;
int a = 5; int b = 9; if (a = b) // Perhaps a == b would be better val = b;
Look at that if
statement carefully: that’s =
, not ==
.
That will:
b
to a
.
a
is now 9
.
9
true or false?
val = b
is executed.
;
at end of if
int x=3; if (x >= 4); printf("Hello\n");
Hello
int x=3; if (x >= 4); printf("Hello\n"); // Is just like this: int x=3; if (x >= 4) /* Nothing */; printf("Hello\n");
printf
is great, but
it doesn’t impress the compiler at all. The compiler ignores
indentation and looks at semicolons.
Remember that scanf
requires the ampersand,
but printf
forbids it.
To illustrate the difference:
int x=11, y=22, z=33; printf("%d %d %d\n", x, y, z); printf("%p %p %p\n", &x, &y, &z);
11 22 33 0x7ffce4b75f7c 0x7ffce4b75f78 0x7ffce4b75f74