Conditionals
Conditionals
● Conditional expressions allow your programs to
make decisions and take different forks in the road,
depending on the values of variables or user input.
● C provides a few different ways to implement
conditional expressions (also known as branches) in
your programs, some of which likely look familiar
from Scratch.
Conditionals
if (boolean-expression)
{
• If the boolean-expression evaluates to true, all lines
of code between the curly braces will execute in order from
top-to-bottom.
• If the boolean-expression evaluates to false, those
lines of code will not execute.
Conditionals
if (boolean-expression)
{
}
else
{
}
• If the boolean-expression evaluates to true, all lines
of code between the first set of curly braces will execute in
order from top-to-bottom.
• If the boolean-expression evaluates to false, all lines
of code between the second set of curly braces will execute
in order from top-to-bottom.
Conditionals
if (boolean-expr1) • In C, it is possible to create an
{ if–else if-else chain.
// first branch • In Scratch, this required nesting
} blocks.
else if (boolean-expr2)
{ • As you would expect, each
// second branch branch is mutually exclusive.
}
else if (boolean-expr3)
{
// third branch
}
else
{
// fourth branch
}
Conditionals
if (boolean-expr1) • It is also possible to create a
{ chain of non-mutually exclusive
// first branch branches.
}
if (boolean-expr2) • In this example, only the third
{ and fourth branches are
// second branch mutually exclusive. The else
} binds to the nearest if only.
if (boolean-expr3)
{
// third branch
}
else
{
// fourth branch
}
Conditionals
int x = GetInt(); • C’s switch() statement is a
switch(x) conditional statement that permits
{ enumeration of discrete cases,
case 1: instead of relying on Boolean
expressions.
printf(“One!\n”);
break;
case 2: • It’s important to break; between
printf(“Two!\n”); each case, or you will “fall through”
break; each case (unless that is desired
behavior).
case 3:
printf(“Three!\n”);
break;
default:
printf(“Sorry!\n”);
}
Conditionals
int x = GetInt(); • C’s switch() statement is a
switch(x) conditional statement that permits
{ enumeration of discrete cases,
case 5: instead of relying on Boolean
printf(“Five, ”); expressions.
case 4:
printf(“Four, ”);
case 3: • It’s important to break; between
printf(“Three, ”); each case, or you will “fall through”
case 2: each case (unless that is desired
printf(“Two, ”); behavior).
case 1:
printf(“One, ”);
default:
printf(“Blast-
off!\n”);
}
Conditionals
int x;
if (expr)
{
x = 5;
} int x = (expr) ? 5 : 6;
else
{
x = 6;
}
• These two snippets of code act identically.
• The ternary operator (?:) is mostly a cute trick, but is
useful for writing trivially short conditional branches. Be
familiar with it, but know that you won’t need to write it if
you don’t want to.
Conditionals
if (and if-else, and if-else if-…-else)
● Use Boolean expressions to make decisions.
switch
● Use discrete cases to make decisions.
?:
● Use to replace a very simple if-else to make your
code look fancy.