8.0 Control Structure
8.0 Control Structure
1 [EEE]
Lecture 8: Control Structure
M. Arifur Rahman
Algorithm Development
• So far, we considered very simple programs
(read, compute, print)
• Top-down Design
• Start from the big picture
• Use a process called divide-and-conquer
• Keep dividing the problem until steps are detailed enough to convert to
a program
• Refinement with Pseudo-code (English like statements) and Flowchart
(diagram, graph)
2
Pseudo-code Notation and Flowchart
Symbols
3
Structured Programming
Use simple control structures to organize the
solution to a problem
Sequence
no yes
Selection
no
Repetition
yes
4
Selection Statements
if
if else
switch
5
if statement
if(Boolean expression)
statement; /* single statement */
if(Boolean expression) {
/* more than one statement */
statement1;
…
statement n;
}
6
if statement - examples
• if (x > 0)
k++;
• if(x > 0) {
x = sqrt(x);
k++;
}
• if(x > 0) /* a common mistake */
x = sqrt(x);
k++;
7
if else statement
if(Boolean expression)
statement;
else
statement;
if(Boolean expression) {
statement block
} else {
statement block
}
8
The switch Statement
A cascaded if statement can be used to
compare an expression against a series of
values:
if (grade == 4)
printf("Excellent");
else if (grade == 3)
printf("Good");
else if (grade == 2)
printf("Average");
else if (grade == 1)
printf("Poor");
else if (grade == 0)
printf("Failing");
else
printf("Illegal grade"); 9
The switch Statement
The switch statement is an alternative:
switch (grade) {
case 4: printf("Excellent");
break;
case 3: printf("Good");
break;
case 2: printf("Average");
break;
case 1: printf("Poor");
break;
case 0: printf("Failing");
break;
default: printf("Illegal grade");
break;
}
10
The switch Statement
• A switch statement may be easier to read
than a cascaded if statement.
• switch statements are often faster than if
statements.
• Most common form of the switch
statement:
switch ( expression ) {
case constant-expression : statements
…
case constant-expression : statements
default : statements
} 11
The switch Statement
12
The switch Statement
• Each case begins with a label of the form
case constant-expression :
• A constant expression is much like an
ordinary expression except that it can’t
contain variables or function calls.
5 is a constant expression, and 5 + 10 is a
constant expression, but n + 10 isn’t a constant
expression (unless n is a macro that represents a
constant).
• The constant expression in a case label
must evaluate to an integer (characters are
acceptable).
13
The switch Statement
14
The switch Statement
• Duplicate case labels aren’t allowed.
• The order of the cases doesn’t matter, and the
default case doesn’t need to come last.
• Several case labels may precede a group of
statements:
switch (grade) {
case 4:
case 3:
case 2:
case 1: printf("Passing");
break;
case 0: printf("Failing");
break;
default: printf("Illegal grade");
break;
}
15
The switch Statement
• To save space, several case labels can be put
on the same line:
switch (grade) {
case 4: case 3: case 2: case 1:
printf("Passing");
break;
case 0: printf("Failing");
break;
default: printf("Illegal grade");
break;
}
• If the default case is missing and the
controlling expression’s value doesn’t match
any case label, control passes to the next
statement after the switch. 16
The Role of the break
Statement
• Executing a break statement causes the
program to “break” out of the switch
statement; execution continues at the next
statement after the switch.
• The switch statement is really a form of
“computed jump.”
• When the controlling expression is evaluated,
control jumps to the case label matching the
value of the switch expression.
• A case label is nothing more than a marker
indicating a position within the switch.
17
The Role of the break
Statement
• Without break (or some other jump
statement) at the end of a case, control will
flow into the next case.
• Example:
switch (grade) {
case 4: printf("Excellent");
case 3: printf("Good");
case 2: printf("Average");
case 1: printf("Poor");
case 0: printf("Failing");
default: printf("Illegal grade");
}
• If the value of grade is 3, the message
printed is 18
The Role of the break
Statement
• Omitting break is sometimes done
intentionally, but it’s usually just an
oversight.
• It’s a good idea to point out deliberate
omissions of break:
switch (grade) {
case 4: case 3: case 2: case 1:
num_passing++;
/* FALL THROUGH */
case 0: total_grades++;
break;
}
• Although the last case never needs a break
statement, including one makes it easy to
add cases in the future. 19
Loop (Repetition) Structures
Problem: Conversion table
degrees radians
Degrees to Radians
0 0.000000
10 0.174533
20 0.349066
30 0.523599
…
340 5.934120
radians = degrees * PI / 180;
350 6.108653
360 6.283186
21
#include <stdio.h>
#define PI 3.141593
degrees = 10;
radians = degrees*PI/180;
printf(“%d %f \n", degrees, radians);
degrees = 20;
Not a good solution radians = degrees*PI/180;
printf(“%d %f \n", degrees, radians);
…
degrees = 360;
radians = degrees*PI/180;
printf(“%d %f \n", degrees, radians);
}
22
Loop Solution #include <stdio.h>
#define PI 3.141593
int main(void)
{
int degrees=0;
degrees = ??? double radians;
radians = degrees*PI/180;
printf(“%d %f \n", degrees, printf("Degrees to Radians \n");
radians);
while (degrees <= 360) {
radians = degrees*PI/180;
printf(“%d %f \n", degrees, radians);
degrees += 10;
}
}
23
Loop (Repetition) Structures
• while statement
• do while statement
• for statement
• Two new statements used with loops
• break and continue
24
while statement
while(expression)
statement;
while(expression) {
statement;
statement;
.
}
25
The while Control Structure
Example:
x = 1; i = 2;
while (i <= 9) {
x = x * i;
i = i + 1;
} i <= 9
true
x = x * i; i = i + 1;
false
26
Example
#include <stdio.h>
#define PI 3.141593
int main(void)
{
int degrees=0;
double radians;
return 0 ;
27
}
do while
do
statement;
while(expression);
do {
statement1;
statement2;
.
} while(expression);
true
condition
false
29
Example
#include <stdio.h>
#define PI 3.141593
int main(void)
{
int degrees=0;
double radians;
30
for statement
for(initialization; test; increment or decrement)
statement;
int main(void)
{
int degrees;
double radians;
33
The for Repetition Statement
• For loops can usually be rewritten as while loops:
initialization;
while ( loopContinuationTest ) {
statement;
increment;
}
• Initialization and increment
• Can be more than one, comma-separated lists of statements
• Can even add the counter variable declaration in initialization
• Example:
for (int i = 0, j = 0; j + i <= 10; j++, i++)
printf( "%d\n", j + i );
34
Example
• What will be the output of the following program, also show how
values of variables change in the memory?
int sum1, sum2, k;
sum1 = 0; 0 2 6 sum1
sum2 = 0;
for( k = 1; k < 5; k++) { 0 1 4 sum2
if( k % 2 == 0) 1 2 3 4 5 k
sum1 =sum1 + k;
else
sum2 = sum2 + k; sum1 is 6
} sum2 is 4
printf(“sum1 is %d\n”, sum1);
printf(“sum2 is %d\n”, sum2);
35
break statement
break;
terminates loop
execution continues with the first statement following the loop
sum = 0;
for (k=1; k<=5; k++) {
scanf(“%lf”,&x);
if (x > 10.0)
break;
sum +=x;
}
printf(“Sum = %f \n”,sum);
36
continue statement
continue;
forces next iteration of the loop, skipping any remaining statements in the
loop
sum = 0;
for (k=1; k<=5; k++) {
scanf(“%lf”,&x);
if (x > 10.0)
continue;
sum +=x;
}
printf(“Sum = %f \n”,sum);
37
Exercise
What is the output of the following
program?
for (i=1; i<=5; i++) { Output
38
End of
Lecture
39