Programming in C Lecture 5: Selection Structures
Programming in C Lecture 5: Selection Structures
Control Structures
Conditions
Relational Operators
Logical Operators
if statements
Two-Alternatives
One-Alternative
Nested If Statements
switch Statement
BY:- AZHAR HUSSAIN (MCA) – tecgain.com, ANSO House, Gudri Road, Hajipur, Bihar
2
Control Structures
Control structures –control the flow
of execution in a program or
function.
Three basic control structures:
Sequential Flow - this is written as a
group of statements bracketed by { and
}where one statement follows another. C
Y N
Selection control structure - this
chooses between multiple statements to
execute based on some condition.
Repetition – this structure executes a Y
C
block of code multiple times. N
BY:- AZHAR HUSSAIN (MCA) – tecgain.com, ANSO House, Gudri Road, Hajipur, Bihar
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 statement.
BY:- AZHAR HUSSAIN (MCA) – tecgain.com, ANSO House, Gudri Road, Hajipur, Bihar
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”);
BY:- AZHAR HUSSAIN (MCA) – tecgain.com, ANSO House, Gudri Road, Hajipur, Bihar
5
Relational and Equality Operators
BY:- AZHAR HUSSAIN (MCA) – tecgain.com, ANSO House, Gudri Road, Hajipur, Bihar
6
Relational and Equality Operators
BY:- AZHAR HUSSAIN (MCA) – tecgain.com, ANSO House, Gudri Road, Hajipur, Bihar
8
Truth Table && Operator
A B A && B
BY:- AZHAR HUSSAIN (MCA) – tecgain.com, ANSO House, Gudri Road, Hajipur, Bihar 9
Truth Table || Operator
A B A || B
BY:- AZHAR HUSSAIN (MCA) – tecgain.com, ANSO House, Gudri Road, Hajipur, Bihar 10
Operator Table ! Operator
A !A
BY:- AZHAR HUSSAIN (MCA) – tecgain.com, ANSO House, Gudri Road, Hajipur, Bihar 11
Remember!
BY:- AZHAR HUSSAIN (MCA) – tecgain.com, ANSO House, Gudri Road, Hajipur, Bihar
12
Operator Precedence
Operator’s precedence function calls
determine the order of ! + - & (unary operations)
execution. Use parenthesis
to clarify the meaning of *, /, %
expression. +, -
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
BY:- AZHAR HUSSAIN (MCA) – tecgain.com, ANSO House, Gudri Road, Hajipur, Bihar 13
Writing English Conditions in C
BY:- AZHAR HUSSAIN (MCA) – tecgain.com, ANSO House, Gudri Road, Hajipur, Bihar
14
Character Comparison
BY:- AZHAR HUSSAIN (MCA) – tecgain.com, ANSO House, Gudri Road, Hajipur, Bihar 15
Logical Assignment
You can assign an int type variable a non zero
value for true or zero for false.
Ex: even = (n%2 == 0)
if (even) { do something }
Some people prefer following for better
readability.
#define FALSE 0
#define TRUE !FALSE
even = (n%2 == 0)
if (even == TRUE) { do something }
BY:- AZHAR HUSSAIN (MCA) – tecgain.com, ANSO House, Gudri Road, Hajipur, Bihar 16
if statement : Two alternatives
if (condition)
{compound_statement_1 } // if condition is true
else
{ compound_statement_2 } // if condition is false
Example:
BY:- AZHAR HUSSAIN (MCA) – tecgain.com, ANSO House, Gudri Road, Hajipur, Bihar
17
Nested if Statements
So far we have used if statements to code decisions with one
or two alternatives.
A compound statement may contain more if statements.
In this section we use nested if statements (one if statement
inside another) to code decisions with multiple alternatives.
if (x > 0)
num_pos = num_pos + 1;
else
if (x < 0)
num_neg = num_neg + 1;
else
num_zero = num_zero + 1;
BY:- AZHAR HUSSAIN (MCA) – tecgain.com, ANSO House, Gudri Road, Hajipur, Bihar
18
Comparison of Nested if and Sequences of ifs
if (x > 0)
num_pos = num_pos + 1;
if (x < 0)
num_neg = num_neg + 1;
if (x == 0)
num_zero = num_zero +1;
if ( condition_1 )
statement_1
else if ( condition_2 )
statement_2
.
.
.
else if ( condition_n )
statement_n
else
statement_e
BY:- AZHAR HUSSAIN (MCA) – tecgain.com, ANSO House, Gudri Road, Hajipur, Bihar 20
Common if statement errors
if crsr_or_frgt == ’C’
printf("Cruiser\n");
if (crsr_or_frgt == ’C’);
printf("Cruiser\n");
BY:- AZHAR HUSSAIN (MCA) – tecgain.com, ANSO House, Gudri Road, Hajipur, Bihar 21
Switch statements
BY:- AZHAR HUSSAIN (MCA) – tecgain.com, ANSO House, Gudri Road, Hajipur, Bihar
22
Example of a switch Statement with Type char Case Labels
/* Determines the class of Ship given its class ID */
#include <stdio.h>
int main(void) {
char classID;
printf("Enter class id [a, b, c or d]: ");
scanf("%c", &classID);
switch (classID) {
case 'B':
case 'b':
printf("Battleship\n");
break;
case 'C':
case 'c':
printf("Cruiser\n");
break;
case 'D':
case 'd':
printf("Destroyer\n");
break;
case 'F':
case 'f':
printf("Frigate\n");
break;
default:
printf("Unknown ship class %c\n", classID);
}
return 0; 23
}
Explanation of Example
This takes the value of the variable class and
compares it to each of the cases in a top down
approach.
It stops after it finds the first case that is equal to the
value of the variable class.
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.
If no case is equal to the value of class, then the
default case is executed.
default case is optional. So if no other case is equal to the
value of the controlling expression and there is a default case,
then default case is executed. If there is no default case, then
the entire switch body is skipped.
BY:- AZHAR HUSSAIN (MCA) – tecgain.com, ANSO House, Gudri Road, Hajipur, Bihar 24
Remember !!!
The statements following a case label may be one or more C
statements, so you do not need to make multiple statements
into a single compound statement using braces.
You cannot use a string such as ”Cruiser” or ”Frigate” as a
case label.
It is important to remember that type int and char values may
be used as case labels, type double values cannot be used.
Another very common error is the omission of the break
statement at the end of one alternative.
In such a situation, execution ”falls through” into the next
alternative.
Forgetting the closing brace of the switch statement body is
also easy to do.
In the book it says that forgetting the last closing brace will
make all following statements occur in the default case, but
actually the code will not compile on most compilers.
BY:- AZHAR HUSSAIN (MCA) – tecgain.com, ANSO House, Gudri Road, Hajipur, Bihar 25
Nested if versus switch
Advantages of if:
It is more general than a switch
It can be a range of values such as x < 100
A switch can not compare doubles
Advantages of switch:
A switch is more readable
BY:- AZHAR HUSSAIN (MCA) – tecgain.com, ANSO House, Gudri Road, Hajipur, Bihar 26
Common Programming Errors
Consider the statement:
if (0 <= x <= 4)
This is always true!
First it does 0 <= x, which is true or false so it evaluates to 1 for true and 0
for false
Then it takes that value, 0 or 1, and does 1 <= 4 or 0 <= 4
Both are always true
In order to check a range use (0 <= x && x <= 4).
BY:- AZHAR HUSSAIN (MCA) – tecgain.com, ANSO House, Gudri Road, Hajipur, Bihar 27
More Common Errors
BY:- AZHAR HUSSAIN (MCA) – tecgain.com, ANSO House, Gudri Road, Hajipur, Bihar 28