Chapter 3 - Control Structures
Chapter 3 - Control Structures
Structures
3.1 Control Structures
• In a program, a control structure determines the order in which statements
are executed.
• Control structure refers to the structure of the program that may be
sequential or conditional or iterative.
• In sequential execution each statement in the source code will be executed
one by one in a sequential order. This is the default mode of execution.
• The conditional or selection statement is used for making decisions and
branching statements.
• The iterative or loop structure are used for repetitively executing a block of
code multiple times.
3.2 Control Statement
• The statement that alters the flow of execution of the program is
known as control statement.
• These control statements divert the control flow from one point to
other point in a program
• There are four types of control statements in C programming
• Decision making (Conditional) statements
• Looping statements
• Loop control Statements
3.2.1 Decision making (Conditional)
statements
• A statement that is executed only when a certain condition within a
program has been fulfilled is called conditional statement.
• Conditional statements are used to check if the conditions is TRUE or
False and then branch the execution.
• They are used for decision making.
• Following statements are used to perform the task of decision making
• if statement
• switch statement
• Ternary statement
if statements
• It is used to control the flow of execution of statements.
• It evaluates the condition first and then depending upon whether the
value is true or false, it transfers the control to a particular
statements.
• It has two paths to follow, one for true condition and other for false
condition.
If statements
• If statements are implemented in different form depending on the
complexity of condition to be tested.
• The different forms of statements are:
• Simple if statement
• if…. else statement
• if …. else if ….else ladder
• Nested if…. else statement
Simple if statement
• The simple if statement in C language is used to
execute the code if condition is true.
• If the condition is true, the statement block will
be executed, otherwise the statement block will
be skipped.
• General form:
if (condition) {
// Code to be executed if the condition is true
}
if….else statement
• The if….else statement is used to execute the code if condition is true
or false.
• If the condition is true, the statement inside if is executed.
• If the condition is false, the statement inside else is executed.
• General form:
if (condition) {
// Statement
} else {
// Statement
}
if …. else if ….else ladder
• if …. else if ….else ladder is used to executed code where conditions are
tested sequentially from top to bottom.
• If the first condition is found true then the statement associated with it will
be executed.
• But if the condition is found false, the control goes to second condition in
the else if .
• Similarly, if this second condition is true, the statement associated with it
will be executed. But if is false, the control goes down sequentially to test
other conditions.
• If none of the conditions aree true, the program statement in the else is
executed.
if …. else if ….else ladder
• if …. else if ….else ladder is used to executed code where conditions are
tested sequentially from top to bottom.
• If the first condition is found true then the statement associated with it will
be executed.
• But if the condition is found false, the control goes to second condition in
the else if .
• Similarly, if this second condition is true, the statement associated with it
will be executed. But if is false, the control goes down sequentially to test
other conditions.
• If none of the conditions are true, the program statement in the else is
executed.
if …. else if ….else ladder
• General form :
if (condition1) {
// Statement 1
} else if (condition2) {
// Statement
} else if (condition2) {
// Statement
} else {
// Statement
}
Nested if…. else statement
• Nested if…. else statement is a structure which have another if…else
body of structure within the body.
• In other words, if there is an if…else statement inside another if….else
statement then it is called nested if….else statement.
Nested if…. else statement
• General form:
if (condition1) {
if (condition2){
//Statement 1
} else {
//Statement 2
}
} else {
if (condition3){
//Statement 3
} else {
//Statement 4
}
}
3.2.1.2 Switch statements
• A switch statement allows a variable to be tested for equality against
a list of values.
• Each values is called a case, and the variable being switched on is
checked for each switch case.
• It is similar to if….else ladder. The only difference is that it tests only
for the true value.
• If none of the cases in the switch statement are matching, the default
case will be executed.
• In each block of case there is a break statement which signals the end
of a particular case and causes exit from the switch statement.
3.2.1.2 Switch statements
• Syntax:
3.2.1.2 Switch statements
3.2.1.2 Switch statements
3.2.1.3 Ternary statements
• It’s general format is
conditional_expression ? exp1 :exp2;
• If the conditional_expression is true, exp1 is evaluated and if the
contional expression is false, exp2 is evaluated.
• It is similar to if….else statement.
• E.g.,
x = (a>b) ? a : b;
• In the above example,
• if a > b is true, then x = a
• if a > b is false then x = a
3.2.1.3 Ternary statements
3.2.1.3 Ternary statements
Prog 12. Using ternary statement, write a program to enter and display
the result depending upon following case:
even = 2n odd =2n+1
3.2.2 Looping Statements
• Loop statements are the control statements that allow us to execute a block of
statements repeatedly until a certain termination condition is met.
• C support following looping statements
• while loop
• do-while loop
• for loop
while loop
• A while loop statement executes a block of statements repeatedly as long
as the given condition is found true.
• It is an entry-controlled loop.
• Syntax:
do-while loop
1. The condition is checked before 1. The condition is checked after the loop
entering the loop body is executed.
2. Statements inside the loop may not be 2. Statements inside the loop executes at
executed even once, if the condition is least once, at the beginning, regardless of
false initially. the condition.
3. E.g., for loop, while loop 3. E.g., do-while loop