Computer Programming
Dr Khaleda Ali
[email protected]
CHAPTER OBJECTIVES
To become familiar with the three kinds of control structures: sequence,
selection, and repetition
To understand compound statements
To learn how to compare numbers and characters
To learn how to use the relational, equality, and logical operators to write
expressions that are true or false
To learn how to write selection statements that choose between two
alternatives in a program using the if statement
To learn how to implement decisions in algorithms using the if statement
To understand how to select among more than two alternatives by nesting if
statements
To learn how to use the switch statement as another technique for selecting
among multiple alternatives
2
Control Structures
All programs can be written in terms of three control
structures (like building blocks)
Sequence
‘Built-in’ to C
Unless otherwise directed, one statement after the next is executed
Selection (three types)
Depending on a condition, select between one statement or
another
If var1 is greater than 10, do this…, else do that…
Repetition (three types)
Depending on a condition, execute one or more statements
repeatedly
3
Truth Values
Be careful of the value returned/evaluated by a
relational operation.
Since the values 0 and 1 are the returned values for
false and true respectively, we can have codes like
these:
int a = 12 + (5 >= 2); // 13 assigned to a
int b = (4 > 5) < (3 > 2) * 6; // 1 assigned to b
int c = ( (4 > 5) < (3 > 2) ) * 6; // 6 assigned to c
You are certainly not encouraged to write such
convoluted codes!
Control Structures
Three kinds of selections structures
if (also called, ‘single-selection’)
if condition is true
Perform action
if condition is false, action is skipped, program continues
if/else (also called, ‘double-selection’)
if condition is true
Perform action
else (if condition is false)
Perform a different action (this will be skipped if condition is true)
switch (also called ‘multiple-selection’)
Allows selection among many actions depending on the integral
value of a variable or expression
5
Conditions
A condition can be specified by an operator between two (variable or
constant) values.
Relational Operators
Used to compare numbers to determine relative order
Important for constructing the decision expression
Operators Meaning
< Less than
> Grater than
<= Less than or equal to
>= Greater than or equal to
== Equal to
!= Not equal to
6
Conditions
Relational Operators Practice
7
Conditions
Logical Operators
we can form more complicated conditions or logical expressions.
Operators Meaning
&& And
|| Or
! Not
8
Conditions
Logical AND operator && (double ampersand)
The condition evaluates to TRUE if and only if BOTH expressions on either
side of && evaluate to TRUE Otherwise condition evaluates to FALSE
Note operator precedence
Beware of ‘short circuit evaluation’
Make the condition most likely to be FALSE the left-most condition
Operand1 Orerand2 AND (&&)
nonzero (true) nonzero (true) 1 (true)
nonzero (true) 0 (false) 0 (false)
0 (false) nonzero (true) 0 (false)
0 (false) 0 (false) 0 (false)
9
Conditions
Logical OR operator || (double vertical bar)
The condition evaluates to TRUE if one or the other or both expressions on
either side of || evaluate to TRUE
Note operator precedence
Otherwise condition evaluates to FALSE
Beware of ‘short circuit evaluation’
Make the condition most likely to be TRUE the left-most condition
Operand1 Orerand2 OR (||)
nonzero (true) nonzero (true) 1 (true)
nonzero (true) 0 (false) 1 (true)
0 (false) nonzero (true) 1 (true)
0 (false) 0 (false) 0 (false)
10
Conditions
Logical NOT operator ! (exclamatory sign)
The condition evaluates to TRUE if expressions after ! evaluate to FALSE
Note operator precedence (An operator’s precedence determines its order of
evaluation)
Otherwise condition evaluates to FALSE
Operand NOT (!)
0 (false) 1 (true)
nonzero (true) 0 (false)
11
Example
Slide 12
Short circuit evaluation
C evaluates only part of the expression. An expression of the form a || b
must be true if a is true. Consequently, C stops evaluating the expression
when it determines that the value of !flag is 1 (true). This technique of
stopping evaluation of a logical expression as soon as its value can be
determined is called short-circuit evaluation .
We can use short-circuit evaluation to prevent potential run-time errors.
Slide 13
Conditions
Comparing characters
We can also compare characters in C using the relational and equality operators.
Expression Value
‘9’ > ‘0’ 1 (true)
‘a’ < ‘e’ 1 (true)
‘B’ <= ‘A’ 0 (false)
‘a’ <= ch && ch <= 1 (true) if ch is a lowercase
‘z’ letter
14
The if Statement
Allows statements to be conditionally executed or skipped over
Models the way we mentally evaluate situations:
"If it is raining, take an umbrella."
"If it is cold outside, wear a coat."
if Statement (One Alternative)
if ( condition ) product = 10, x = 2;
statement; if (x != 0)
product = product * x;
if (score > 90)
grade = 'A';
if (score > 90)
grade = 'A';
15
The if Statement
if (12 < 12)
if Statement (Two Alternatives) printf("less");
if ( condition ) else
statement T; var1printf("not
= 25.12; less");
else var2 = 15.00;
statement F; if (var1 <= var2)
If the condition is true, printf("less or equal");
then statement T is executed else
and statement F is skipped. printf("greater than");
If the condition is false, scanf(“%d”, &num);
then statement T is skipped if (num % 2 == 0)
and statement F is executed. printf(“Even\n”);
else
16
printf(“Odd\n”);
if Statements with Compound
Statements
To execute more than one statement as part of an if statement,
enclose them in { }. { } creates a block of code
if (score > 90)
{
grade = 'A';
printf(“Good Job!\n”);
}
17
Nested if Statements
evencount = oddcount = 0;
if ( (num % 2) == 0 )
{
printf(“%d is an even number.\n”, num);
An if statement that is nested ++evencount;
inside another if statement if( (num % 4) == 0 )
printf(“It is divisible by 4\n”);
Nested if statements can be if( num == 0 )
used to test more than one {
printf(“It is zero.\n”);
printf(“That isn’t interesting.\n”);
condition. }
}
else
if( marital_status == ‘S’ ) {
{ printf(“%d is an odd number.\n”, num);
if( gender == ‘M’ ) ++oddcount;
{
if( age >= 18 && age <= 26 ) if( (num % 9) == 0 )
{ printf(“It is divisible by 9\n”);
printf("All criteria are met.\n"); else
} printf(“It is not divisible by 9\n”);
} }
}
18