4 Control Structures
4 Control Structures
4 Control Structures
Structures
Chapter 13
Iterative
– executing code multiple times,
ending based on evaluated expression
• for
• while
• do-while
CMPE13 2
if
if(condition)
action;
F
condition
Condition is a C expression, T
which evaluates to TRUE (non-
zero) or FALSE (zero). action
Action is a C statement,
which may be simple or
compound (a block).
CMPE13 3
Examples of if statements
if (x <= 10)
y = x * x + 5;
if (x <= 10)
{ y = x * x + 5; compound statement;
z = (2 * y) / 3; both executed if x <= 10
}
CMPE13 4
About braces indentation…
CMPE13 5
LC-3 code for if statement
if (x == 2)
y = 5;
CMPE13 6
LC-3 code for if-else
LDR R0, R5, #0
BRz ELSE
if(x) ; x is not zero
{ LDR R1, R5, #-1 ; incr y
y++; ADD R1, R1, #1
STR R1, R5, #-1
z--;
LDR R1, R5, #02 ; decr z
} ADD R1, R1, #1
else STR R1, R5, #-2
JMP DONE ; skip else code
{ ; x is zero
y--; ELSE LDR R1, R5, #-1 ; decr y
z++; ADD R1, R1, #-1
STR R1, R5, #-1
} LDR R1, R5, #-2 ; incr z
ADD R1, R1, #1
STR R1, R5, #-2
DONE ... ; next statement
CMPE13 7
More if examples
• if(0 <= age && age <= 11)
kids += 1;
• if(month == 4 || month == 6 ||
month == 9 || month == 11)
printf(“The month has 30 days.\n”);
CMPE13 8
if’s can be nested
if (x == 3)
if (y != 6)
{
z = z + 1;
w = w + 2;
}
CMPE13 10
Matching else with if
This code
if(x != 10)
Else is always
if(y > 3)
z = z/2; associated with the
else closest unassociated if.
z = z * 2;
is the same as... and is NOT the same as...
if(x != 10) if (x != 10)
{ if(y > 3) { if (y > 3)
z = z/2; z = z/2;
else }
z = z*2; else
} z = z*2;
CMPE13 11
Chaining If’s and Else’s
if (month == 4 || month == 6 || month == 9 ||
month == 11)
printf(“Month has 30 days.\n”);
else if (month == 1 || month == 3 ||
month == 5 || month == 7 ||
month == 8 || month == 10 ||
month == 12)
printf(“Month has 31 days.\n”);
else if (month == 2)
printf(“Month has 28 or 29 days.\n”);
else
printf(“Don’t know that month.\n”);
CMPE13 12
while
while (test) F
loop_body; test
T
loop_body
CMPE13 13
LC-3 code for while
AND R0, R0, #0
STR R0, R5, #0 ; x = 0
; test
LOOP LDR R0, R5, #0 ; load x
x = 0; ADD R0, R0, #-10
while (x < 10) BRzp DONE
; loop body
{ printf(“%d ”, x); LDR R0, R5, #0 ; load x
x = x + 1; ...
} <printf>
...
ADD R0, R0, #1 ; incr x
STR R0, R5, #0
JMP LOOP ; test again
DONE ; next statement
CMPE13 14
Infinite loops
The following loop will never terminate:
x = 0;
while (x < 10)
printf(“%d ”, x);
CMPE13 15
for loops
init
for(init; test; re-init)
loop_body
F
test
T
Executes loop body as long as
loop_body
test evaluates to TRUE (non-zero).
Initialization and re-initialization
code included in loop statement. re-init
CMPE13 16
LC-3 code
for for
; init
AND R0, R0, #0
STR R0, R5, #0 ; i = 0
; test
LOOP LDR R0, R5, #0 ; load i
ADD R0, R0, #-10
BRzp DONE
for(i = 0; i < 10; i++)
; loop body
printf(“%d ”, i);
LDR R0, R5, #0 ; load i
...
<printf>
...
; re-init
(NOTE: This is the same ADD R0, R0, #1 ; incr i
as the while example!) STR R0, R5, #0
JMP LOOP ; test again
DONE ; next statement
CMPE13 17
Examples of for loops
/* -- what is the output of this loop? -- */
for(i = 0; i <= 10; i ++)
printf("%d ", i);
CMPE13 18
Nested loops
CMPE13 20
for vs. while
In general:
for loop is preferred for counter-based loops.
– Explicit counter variable
– Easy to see how counter is modified each loop
CMPE13 21
do-while
do
loop_body;
loop_body
while(test);
T
test
CMPE13 22
Chaining if’s and else’s
if(month == 4 || month == 6 || month == 9 ||
month == 11)
printf(“Month has 30 days.\n”);
else if(month == 1 || month == 3 ||
month == 5 || month == 7 ||
month == 8 || month == 10 ||
month == 12)
printf(“Month has 31 days.\n”);
else if(month == 2)
printf(“Month has 28 or 29 days.\n”);
else
printf(“Don’t know that month.\n”);
CMPE13 23
switch
evaluate
expression
switch(expression)
{
case const1:
= const1? action1
action1; break; T
case const2: F
action2; break;
default: = const2? action2
action3; T
} F
CMPE13 24
Example of switch
/* same as month example for if-else */
switch (month)
{ case 4:
case 6:
case 9:
case 11:
printf(“Month has 30 days.\n”);
break;
case 1:
case 3:
/* some cases omitted for brevity...*/
printf(“Month has 31 days.\n”);
break;
case 2:
printf(“Month has 28 or 29 days.\n”);
break;
default:
printf(“Don’t know that month.\n”);
}
CMPE13 25
More about switch
case expressions must be constant.
case i: /* illegal if i is a variable */
switch (a)
{ case 1: What does it do?
printf(“A”);
case 2:
printf(“B”);
default:
printf(“C”);
}
CMPE13 26
break and continue
break;
– used only in switch statement or iteration statement
– passes control out of the innermost loop or switch statement
containing it to the statement immediately following
– usually used to exit a loop before terminating condition occurs
(or to exit switch statement when case is done)
continue;
– used only in iteration statement
– terminates the execution of the loop body for this iteration
– loop expression is evaluated to see whether another
iteration should be performed
– if for loop, also executes the re-initializer
CMPE13 27
Example
What does the following loop do?
CMPE13 28
Problem Solving in C
• Stepwise Refinement
– as covered in Chapter 6
• ...but can stop refining at a higher level of
abstraction.
CMPE13 29
Problem 1: Calculating Pi
Calculate using its series expansion.
User inputs number of terms.
4 4 4 4
4 ( 1) n 1
3 5 7 2n 1
Start Evaluate
Series
Initialize
Output
Results
Get Input
Stop
CMPE13 30
Pi: 1st refinement
Start Initialize
iteration count
Initialize
for loop
F
count<terms
Get Input
T
Evaluate Evaluate
Series next term
Output
count = count+1
Results
Stop
CMPE13 31
Pi: 2nd refinement
Initialize
iteration count T count F
is odd
F
count<terms
Evaluate
next term
add term
CMPE13 32
Pi: Code for Evaluate Terms
CMPE13 33
Pi: Complete Code
#include <stdio.h>
main() {
double pi = 0.0;
int numOfTerms, count;
CMPE13 34
Problem 2: Finding Prime
Numbers
Print all prime numbers less than 100.
– A number is prime if its only divisors are 1 and itself.
– All non-prime numbers less than 100 will have a divisor
between 2 and 10.
Start
Initialize
Print primes
Stop
CMPE13 35
Primes: 1st refinement
Initialize
num = 2
Start
F
Initialize num < 100
CMPE13 36
Primes: 2nd refinement
Initialize
num = 2
Divide num by
2 through 10
F
num < 100
no F
T divisors?
Print num T
if prime
Print num
num = num + 1
CMPE13 37
Primes: 3rd refinement
Initialize
Divide num by divisor = 2
2 through 10
F
divisor <= 10
no F
divisors?
T
T Clear flag if
num%divisor > 0
Print num
divisor =
divisor + 1
CMPE13 38
Primes: Using a Flag Variable
To keep track of whether number was divisible,
we use a "flag" variable.
– Set prime = TRUE, assuming that this number is prime.
– If any divisor divides number evenly,
set prime = FALSE.
• Once it is set to FALSE, it stays FALSE.
– After all divisors are checked, number is prime if
the flag variable is still TRUE.
CMPE13 39
Primes: Complete Code
#include <stdio.h>
#define TRUE 1
#define FALSE 0
Optimization: Could put
main () { a break here to avoid some work.
int num, divisor, prime;
CMPE13 41
Substring: State machine to
flow chart
no
read char
other match
't' T
match = 0 if 't', match=1
matched F
't' other if 'h', match=2
T
't' match = 1 if 't', match=1
'h' else match=0
F
't'matched
other if 'e', count++
'th'
T and match = 0
match = 2
'e' if 't', match=1
F else match=0
't'
matched other
'the'
increment
CMPE13 count 42
Substring: Code (Part 1)
#include <stdio.h>
main() {
char key; /* input character from user */
int match = 0; /* keep track of characters matched */
int count = 0; /* number of substring matches */
CMPE13 43
Substring: Code (Part 2)
CMPE13 44
Substring: Code (Part 3)
CMPE13 45