Programming in C: Flow of Control
Programming in C: Flow of Control
Programming in C
Flow of Control
Flow of control
The order in which statements are executed
Transfer of control
When the next statement
executed is not the next
one in sequence
Flow of Control
Control structures
combination of individual statements into a logical unit
that regulates the flow of execution in a program or
function
Sequence
Selection (Making Decisions)
Repetition (Looping)
111 Ch 06 1
V3 1/3/2015
Boolean Expressions
Evaluate to true or false
Forms
Relational expression: <expr> <relational operator> <expr>
Examples:
7 < 5
a + b > 6
Logical expression: <Boolean expr> <logical operator> <Boolean expr>
Examples:
(x < 7) && (y > 3)
Relational Operators
Standard Algebraic C Relational C Condition
Relational Operator Operator Example Meaning of C Condition
Inequality
< < x<y x is less than y
<= x <= y x is less than or equal to y
> > x>y x is greater than y
>= x >= y x is greater than or equal to y
Equality
= == x == y x is equal to y
!= x != y x is not equal to y
4th: Ch 4 p. 46
3rd: Ch 5 p. 46
|| (logical OR)
Returns true if either of its conditions is true
111 Ch 06 2
V3 1/3/2015
Precedence of Operators
1. (), []
2. Unary +, unary -, !, ++, --
3. Type casting
4. * , / , %
5. + , -
6. <, <=, >, >=
7. ==, !=
8. &&
9. ||
10. =
111 Ch 06 3
V3 1/3/2015
true
Boolean
Expression
10
if (Boolean Expression)
{
statement1;
statement2;
...
}
11
if
Only performs an action if the condition is true
if-else
A different action is performed when condition is
true and when condition is false
12
111 Ch 06 4
V3 1/3/2015
false true
Boolean
Expression
13
15
111 Ch 06 5
V3 1/3/2015
16
18
111 Ch 06 6
V3 1/3/2015
19
• The statement,
21
111 Ch 06 7
V3 1/3/2015
22
if-else Construct
To avoid confusion, and possible errors, it is best to
use braces even for single statements.
However, code will be longer
23
Conditionals
C uses an integer to represent Boolean values
Zero is interpreted as false
Any other integer value is interpreted as true
24
111 Ch 06 8
V3 1/3/2015
Conditionals
is not a syntax error in C.
The expression, n = 0, assigns zero to n and the value of
the expression is 0. Zero is interpreted as false, and the
false branch of the if statement will be taken.
is not a syntax error in C.
The expression assigns 5 to n. 5 is interpreted as true,
and the true branch of the if statement will be taken.
25
Conditionals
Remember to use the == operator to test for equality.
To help catch the error when the equality check
involves a constant, put the constant on the left hand
side of the ==.
For example, use
instead of
Since is not a valid assignment in C, the compiler
will detect this error when == is intended.
26
27
111 Ch 06 9
V3 1/3/2015
default action(s)
28
default action(s)
29
30
111 Ch 06 10
V3 1/3/2015
switch Statement
The switch_expression is compared against the values
constant1, constant2, …, constantN
constant1, constant2, …, constantN must be simple
constants or constant expressions.
Can be a char or an int
Best to use the same type constant as the switch expression
If not, a type conversion will be done.
31
32
Example of
switch
33
111 Ch 06 11
V3 1/3/2015
Programming in C
THE END
34
111 Ch 06 12