Programming for MSc Part I Part 4: Control Flow
(a) Loops
Definition
• A loop statement instructs the computer to repeat an
operation (the Loop Body ) depending on a condition
• Loops are less general than recursion but often easier to setup
• There are different types of loops, they differn in how and
when the loop condition is being checked
• A loop must be guaranteed to terminate at some point
The ‘while’ Loop
• A while loop in C looks a bit like the if statement:
while (condition) statement
• statement will be repeated as long as condition is true
• The condition is checked before each execution
−→ So statement may not be executed at all!
Example:
while (string[0] != ’\0’)
{
string[0] = toupper (string[0]);
++string;
}
Herbert Martin Dietze <herbert@the-little-red-haired-girl.org> 62
Programming for MSc Part I Part 4: Control Flow — Loops
The ‘do-while’ Loop
• Sometimes we want the loop to be executed at least once
• Therefore it would be better to check the condition after
instead of before the loop body execution
• The do-while loop in C does just that:
do statement while (condition);
• statement will be executed once, and after that as long as
condition is true
Example:
char c;
do
{
printf ("Enter operator or ’q’ to quit!\n");
c = getchar ();
handle_operator (c);
}
while (toupper (c) != ’Q’);
‘while’ vs ‘do-while’
• The condition in while is usually positive while the condition
in do-while is usually negative
• Use do-while if the code must be executed at least once,
else while
Herbert Martin Dietze <herbert@the-little-red-haired-girl.org> 63
Programming for MSc Part I Part 4: Control Flow — Loops
The ‘for’ Loop
• In some cases we want to execute a loop just n times
• We will therefore need a counter to count to n
• The for loop in C combines the while loop with a way to
handle such a counter:
for (expression1; expression2; expression3) statement
• It doe the following things:
1. Execute expression1 (e.g. initialize a counter)
2. While expression2 is true:
(a) Execute statement
(b) Execute expression3
• This can be used for many other things besides counting!
Example:
void cstring_reverse (char *s)
{
int i, size = (int)strlen (s), half = size / 2;
char tmp;
for (i = 0; i < half; i++)
{
tmp = s[size - 1 - i];
s[size - 1 - i] = s[i];
s[i] = tmp;
}
}
Herbert Martin Dietze <herbert@the-little-red-haired-girl.org> 64
Programming for MSc Part I Part 4: Control Flow — Loops
Empty Loops
• Sometimes empty loops make sense
• But often they don’t make the code more readable
Example:
char buf[256];
char *s = read_string (buf);
while (*++s);
append (s, "42");
−→ What does that code do?
Ugly because:
• Assignment and test in one expression: “*++s”
• Condition not quite clear (should be “*s != ’’\0’’”)
• Not quite clear whether the empty loop was on purpose
Slightly enhanced version:
char buf[256];
char *s;
for (s = read_string (buf); *s != ’\0’; s++)
{
}
append (s, "42");
Herbert Martin Dietze <herbert@the-little-red-haired-girl.org> 65
Programming for MSc Part I Part 4: Control Flow
(b) Advanced Branching
The Conditional Operator
• The Conditional Operator is a shortcut for some if statements
• It sets an expression’s value depending on a condition
• The syntax is:
condition? expression1: expression2
Example:
if (x < y)
{
z = x;
}
else
{
z = y;
}
This can be written as:
z = (x < y)? x: y;
−→ Handle with care, this can make code unreadable!
Herbert Martin Dietze <herbert@the-little-red-haired-girl.org> 66
Programming for MSc Part I Part 4: Control Flow
The ‘switch’ Statement
• Sometimes many different branches in a program would make
it very complicated when using if-else constructs
• In some cases the switch statement can help here:
switch (integral-expression) { case-or-default-labels }
Example:
switch (getchar ())
{
case ’a’:
do_a ();
break;
case ’b’:
do_b ();
break;
case ’q’:
do_quit ();
break;
default:
printf ("Invalid input!\n");
}
• After each case there must be one integral constant
• The default label is equivalent to else
• The breaks bail out of the switch statement
Herbert Martin Dietze <herbert@the-little-red-haired-girl.org> 67