3 Selection
3 Selection
3
Compound Statements
A Compound statement or a Code Block is written as a group of statements
bracketed by { and } and is used to specify sequential flow.
{
Statement_1;
Statement_2;
Statement_3;
}
Example: the main function is surrounded by { }, and its statements are
executed sequentially.
Function body also uses compound statements.
4
Conditions
A program chooses among alternative statements by testing the values of
variables.
0 means false
Any non-zero integer means true, Usually, we’ll use 1 as true.
if (a>=b)
printf(“a is larger”);
else
printf(“b is larger”);
5
Relational and Equality Operators
Most conditions that we use to perform comparisons will have one of these forms:
variable relational-operator variable: e.g. a < b
variable relational-operator constant: e.g. a > 3
variable equality-operator variable: e.g. a == b
variable equality-operator constant: e.g. a != 10
Operator Meaning Type
< less than relational
> greater than relational
<= less than or equal to relational
>= greater than or equal to relational
== equal to equality
6
!= not equal to equality
Logical Operators
logical expressions - expressions that use conditional statements and logical
operators.
&& (and)
A && B is true if and only if both A and B are true
|| (or)
A || B is true if either A or B are true
! (not)
!(condition) is true if condition is false, and false if condition is true
This is called the logical complement or negation
Example
(salary < 10000 || dependents > 5)
(temperature > 90.0 && humidity > 90)
!(temperature > 90.0)
7
Truth Table for (&&, ||, ! ) Operators
A B A && B A || B
A !A
8
Operator Precedence
Operator’s precedence determine the
order of execution. Use parenthesis to ! + - & (unary
clarify the meaning of expression. operations)
Relational operator has higher
precedence than the logical operators.
*, /, %
Ex: followings are different. +, -
(x<y || x<z) && (x>0.0)
x<y || x<z && x>0.0 <, >, <=, >=
==, !=
&&
||
=
9
One-way selection (if-statement)
Examples:
Examples:
Simple statement Compound statement
if(condition) if(condition)
statement1; compound_statement1
else else
statement2; compound_statement2
Multi-way selection with an else option
(if- else if – else)
Used to execute the first statement or the first
compound_statement whose corresponding condition
is true. The statement in the else part is executed if
each condition is false.
20
ASCII Table
Characters are actually represented
in C as integer values.
Each character is represented by its
ASCII code (e.g A = 65. B = 66, etc).
The table after the program below
shows the printable ASCII characters
and their corresponding ASCII codes.
21
Character Comparison
Printing a char variable
using “%c” will print the
character but printing it
with “%d” will print the
ASCII code.
Similarly, printing an
integer variable with “%c”
will also print the character
provided the value is within
the range of character
values.
C allows character
comparison using relational
and equality operators.
During comparison, orders 22
based on ASCII table is
followed.
Example
Given a person’s salary, we want to calculate the tax due by adding the base tax to the
product of the percentage times the excess salary over the minimum salary for that range.
Salary Range Base tax Percentag
e of Excess
0.00 – 14,999.99 0.00 15
15,000.00 – 2,250.00 18
29,999.99
30,000.00 – 5,400.00 22
49,999.99
50,000.00 – 11,000,00 27
79,999.99
80,000.00 – 21,600.00 33
150,000.00
23
Example (Divisible by 3 and 5)
Write a C program that prompts and reads an integer number x. It then checks the number for
divisibility by 3 and 5 and prints one of the following messages accordingly:
The number x is divisible by both 3 and 5
The number x is divisible by 3 but not 5
The number x is divisible by 5 but not 3
The number x is neither divisible by 3 nor by 5
24
Example (Divisible by 3 and 5)
More efficient method (why?)
25
Some Common Errors in if Statements
1. Using wrong conditions; The following condition is true for all values of x. The correct
condition is (x >= 0 && x <= 4)
26
Some Common Errors in if Statements
4. Using conditions without surrounded parentheses ( )
No parentheses ( ) around the Parentheses ( ) surrounds the
condition condition
5. A semicolon after the condition. C will interpret this as there is nothing to do if the condition
is true.
27
Switch statements
28
Switch statements
The case constants must be distinct char or int constants; otherwise there is a
syntax error.
The default label together with its statementList is an optional.
The default label need not be the last label if it is followed by break statement.
The break statement following a statementList is an optional. (What is the effect
of removing break?)
The switch expression is evaluated and then the statementList of the case value
that equals to the expression is executed. If there is a break statement, control
passes to the statement after the switch; otherwise, the following statementLists
are executed until a break statement is encountered, control then passes to the
statement after the switch statement.
If switch expression is not equal to any case value, the statementList for the
default label is executed, and if the default label is the last one or if its statement
list is followed by a break statement, control passes to the statement after the
switch statement.
29
If switch expression is not equal to any case value and there is no default label,
control passes to the statement after the switch without executing any switch
Example of a switch Statement with char Case Labels
Write a C program to choose the class of a ship
according to the first character in the word,
where classes as follows: (B or b for Battleship),
(C or c for Cruiser), (D or d for Destroyer), and
(F or f for Frigate)
Suppose the user enter the letter ‘D’:
This takes the value of the variable
classID, which is D, and compares it to
each of the cases in a top down
approach.
It stops at the first case that is equal
to the value of the variable classID,
which is at line 16.
It then starts to execute each line of
the code following the matching case
till it finds a break statement or the
end of the switch statement.
Finally, the control passes to the 30
statement after the switch body.
What will happen if the user enters the letter
Example of a switch Statement with int Case Labels
Write a C program to display the
name of a digit; For example; if the
user enter 3, the program should
display three, if the user enters 8,
the program should display eight,
and so on. If the user enters any
other character rather than a digit,
the program should display an error
message.
31