PAGEFLOP
FORWARD
SELECTOR
SELAGAIN
WRONGSEL
YOUR SELECTION IS NOT IN THE 0 - #SELIMIT RANGE Please try again
SELRTRN
PAUSE
AANVANG
Copyright CourseWare Technologies Inc., 1985-88
Lesson - 3
C CONTROL CONSTRUCTS
P1
Control Constructs|Topics to Learn|3-0|14,44
# Topic
--- -------
1 - Types of Control Constructs
2 - Sequential Constructs
3 - Arithmetic Conversions
4 - The if and if-else Statements
5 - The switch Statement
6 - First Review
7 - The while Statement
8 - The for Statement
9 - The do-while Statement
10 - The break and continue Statements
11 - Second Review
0 - Return to the Main Menu
P2
Control Constructs|Types of Control Constructs|3-1.1|16,50
A control construct specifies and alters the
logical flow of a program. Control constructs
can be defined as:
Sequential constructs include:
simple statements
compound statements
Conditional constructs include:
the if - else statement
the switch statement
Iterative constructs include:
the while statement
the for statement
the do-while statement
P3
Control Constructs|Sequential Constructs|3-2.1|6,54
A simple statement is an assignment, an
expression, or a function call. For example:
an assignment: x = -127.6;
a function call: printf("this is an example\n");
an expression: x = x + 1;
P4
Control Constructs|Sequential Constructs|3-2.2|11,51
A compound statement is a sequence of
statements that is syntactically equivalent to a
single statement and is delimited by { and } .
The following is a compound statement made up of
three simple statements:
{
a = b - 7;
c = a - 14*b;
d = c * c;
}
P5
Control Constructs|Arithmetic Conversions|3-3.1|14,50
When operands of differing types occur in an
expression, a conversion to the longer of the two
types takes place whenever possible.
OP1 OP2 RESULT USED AS
------- -------- -------- -------
char int
short int
float double
double long int double
short long long
unsigned short unsigned
short int int
int int int
P6
Control Constructs|Arithmetic Conversions|3-3.2| 18 , 56
Increment/decrement operations include:
x++; post_incrementing
++x; pre_incrementing
x--; post_decrementing
--x; pre_decrementing
An assignment operation assigns a value to
a variable.
If e1 and e2 are expressions, then
e1 op = e2 IS EQUIVALENT TO e1 = (e1) op (e2).
P7
Control Constructs|Arithmetic Conversions|3-3.3|14,50
The following example demonstrates the use of pre-
and post- increment/decrement capabilities in C. The
value of the variable x is always 7 before the operation.
Shown are the values of both variables x and
y after the operation.
int x = 7, y; After the Operation:
y = x++; y=7, x=8
y = ++x; y=8, x=8
y = x--; y=7, x=6
y = --x; y=6, x=6
y *= (x*x)-7; IS EQUIVALENT TO: y = y*((x*x)-7);
P8
Control Constructs|The if-else Statement|3-4.1|12,41
The if and if-else statements are used
to make decisions.
SYNTAX: if (test criterion)
statement_1;
SYNTAX: if (test criterion)
statement_1;
else
statement_2;
P9
Control Constructs|The if-else Statement|3-4.2|18,61
The if Statement
|
v
*
* * FALSE
* e? *----->|
* * |
* |
| TRUE |
v |
------------- |
| Statement | |
------------- |
| |
|<-----------|
|
v
The if statement evaluates
expression e.
If the expression is TRUE or NON-ZERO,
the statement will be executed.
If the expression is FALSE or ZERO,
the program will exit from the if
statement.
|
v
*
FALSE * * TRUE
|<----- * e? *----->|
| * * |
| * |
v v
------------- -------------
| statement2 | | statement1 |
------------- -------------
| |
| |
|------------> <-----------|
|
v
if (e?) statement1 else statement2
The if - else statement
evaluates the expression e.
If the expression is TRUE or
NON-ZERO, statement1 will
be executed.
If the expression is FALSE or
ZERO, statement2 will
be executed.
P10
Control Constructs|The if-else Statement|3-4.3|14,54
The following example demonstrates the use of nested
if - else statements. When the value of x is greater
than 0 and the value of k is equal to or smaller than
the value of m, no action will take place. If there
were no braces around the first if statement, then
else would refer to the second if, and the logic
would change entirely.
if (x > 0) {
if (k > m)
y = k;
}
else
y = m;
P11
Control Constructs|The if-else Statement|3-4.4|14,54
The following is another example of the if - else
statement. The algorithm shown counts the number
of WINS and LOSSES. Note that braces are used to
guarantee that both simple statements inside the
if and the else are executed.
if (x) { /* if (x != 0) */
is_a_win++;
printf("WIN!");
}
else {
is_a_loss++;
printf("LOSS!");
}
P12
Control Constructs|The if-else Statement|3-4.5|18,54
A multiway decision is represented as:
if (expression_1)
statement_1;
else if (expression_2)
statement_2;
else if (expression_3)
statement_3;
else
statement_4;
if (expression_1)
statement_1;
else if (expression_2)
------- OR -------- statement_2;
else if (expression_3)
statement_3;
else
statement_4;
P13
Control Constructs|The if-else Statement|3-4.6|11,41
The ternary operator is another form of
an if - else construct:
SYNTAX:
expr_1 ? expr_2 : expr_3;
|__if true__| |
| |
|______if false______|
If expression 1 is TRUE (!=0), then expression 2
is evaluated, else expression 3 is evaluated.
P14
Control Constructs|The if-else Statement|3-4.7|12,54
The following example demonstrates the use of the ternary
operator in a conditional assignment expression. It is
followed by an equivalent if - else statement.
x = (y > (2*z - 7)) ? y : x + 1;
- IS EQUIVALENT TO -
if (y > (2*z -7))
x = y;
else
x = x + 1;
P15
Control Constructs|The if-else Statement|3-4.8|10,54
At this point we'll introduce a mechanism which
will be covered in greater detail in the next lesson:
the return statement.
The return statement is an optional mechanism
which can return both the value and the program
control from the called function to its caller.
SYNTAX: return [expression];
P16
Control Constructs|The switch Statement|3-5.1|8,44
The switch statement is a multiway decision
maker that tests if the switch expression matches
one of the constant values and branches accordingly.
SYNTAX:
switch (expression) {
statement
}
P17
Control Constructs|The switch Statement|3-5.2| 10 , 47
For example,
switch (E) {
case C1: ......;
case C2: ......;
:
case C5: ......;
:
case CN: ......;
[default:....;]
}
P18
Control Constructs|The switch Statement|3-5.3| 18 , 61
The switch statement may be represented graphically as follows:
|
v
*
* *
|<------------ * e? *--------------->|
|C1 --- * *-- |default
| /C2 * \Cn |
v / \ |
----- ----- ----- ---------
| S1 | | S2 | . . . | Sn | ... | default |
----- ----- ----- ---------
| | | |
v v v v
-------------------> <---------------------
|
v
If the expression e matches the case C1, the statement S1 is executed.
S1
S1
If the expression e matches the case C2, the statement S2 is executed.
S2
S2
If the expression e matches case Cn, statement Sn is executed.
Sn
Sn
If the expression e does NOT match any case and
default is present, then the default case is executed.
default
default
P19
Control Constructs|The switch Statement|3-5.4|11,54
The following example demonstrates the use of the
switch construct. The function cost_get() will
determine the total cost of stamps. The argument n
specifies the number of stamps; mail_code specifies
the type of postage desired (first-class, card-rate,
etc.) NOTE: A break is placed at the end of each
case statement to make sure that only the applicable
case will be executed. If mail_code equals either
1 or 2, the postage cost will be calculated and
printed, otherwise an error condition (-1) will
be returned signifying an invalid mail_code.
P20
Control Constructs|The switch Statement|3-5.5|16,51
cost_get (n, mail_code)
int n, mail_code;
{
float local_cost;
switch (mail_code) {
case 1: /* domestic first class */
local_cost = .20 * n; break;
case 2: /* domestic card rate */
local_cost = .15 * n; break;
default:
printf("Invalid Mail Code\n");
return (-1);
}
printf("Postage cost = %10.2f\n", local_cost);
return( local_cost * 100 ); /* in cents */
}
P21
Control Constructs|The while Statement|3-7.1| 18 , 58
|
-------->|
| v
| *
| * * FALSE
| * e? *----->|
| * * |
| * |
| | TRUE |
| v |
| ------------- |
| | Statement | |
| ------------- |
| | |
|<-------| |<--------|
|
v
The while statement diagram
The while statement executes
a group of statements, as
long as the test criterion
is TRUE at the top of the
loop (i.e., NON-ZERO).
SYNTAX:
while (test criteria != 0) {
execute these statement(s)
}
P22
Control Constructs|The while Statement|3-7.2|10,54
The following example demonstrates the use of the
while statement. This section of C code will read
one character at a time from the standard input
until it encounters a non-white space. (White
space includes blanks, newlines and tabs.)
NOTE: The function getchar() gets the next character
from the standard input. Assignment within a test
expression is permitted in C.
P23
Control Constructs|The while Statement|3-7.3|9,33
c = getchar();
while ( c == ' ' || c == '\n'
|| c == '\t')
c = getchar();
EQUIVALENTLY:
while ((c = getchar()) == ' ' ||
c == '\n' || c == '\t')