Chapter Six-Control Structures in C
Chapter Six-Control Structures in C
Chapter Six: Control structures in C on which control structure can be used to achieve a given task
appropriately.
6.1 Introduction
6.2 Control structures in C
In most of the programs presented above, the statements have
There are two broad categories of control structures in namely
been sequential, executed in the order they appear in the main
Decision control structures which include control structures
program. Control Statements are meant to control the logic of
program's execution i.e. the order in which the instructions in a such as if statements, if ...Else statements , if…else if…else
program must be executed. They make it possible to make and switch statement
decisions, to perform tasks repeatedly or to jump from one Iterative/Looping/Repetitive control structures which
section of code to another. Just like other programming enables a section of a program to be executed repeatedly
languages, C supports a number of control structures. until a given condition has been met or for a specified
number of times. C supports three loop constructs i.e. do...
While Loop, while loop and the for loop
Programmers may need to test values of variables and
depending on the result, alternative statements may be 6.3 Decision control structures
executed. This facility can be used to select among alternative This type of control structure uses a Boolean expression
courses of action. It can also be used to build loops for the (true/false). Statements are only executed if a condition is true
repetition of basic actions. else the statement is skipped. The Boolean expression is
usually constructed using relational operators and logical
operator
This chapter therefore aims at familiarizing the learners to C
C programming language provides following types of decision
control structures. The chapter also aims at providing insights
control structures.
1
1|CHAPTER SIX:-CONTROL STRUCTURES
CMT 110: PROGRAMMING METHODOLOGY
if (condition) }
The statements within the if shall only be executed if the whether the outcome is true or false).The general syntax is as
the statements in the if blocks or in the else-block in braces, program which is supposed to calculate the roots of a quadratic
equation
whether it is a single statement or a compound statement.
A good example is presented below. It’s a program to
determine if the user has read a negative or positive number
from the keyboard
// testing for real solutions to a quadratic Variable real_roots is of type bool assigned the value true in
In this scenario, it will be possible to use one if or else if We shall rewrite the program for determining the largest number
statement inside another if or else if statement(s). This can be out of three integers numbers entered from the keyboard using
done by placing if statements within other if-else constructions. if…else if…else
break; would be evaluated. In case you forget to put in one of the break
case constant 2: statements, then execution can continue from one case
break;
…… The different between a switch control structure and the others
…… is that braces are not required around multiple statements.
statement; statements.
}
Iterative control structures allow statement (s) to be executed
The expression being switched needs to e of type integer. This repeatedly until a certain condition is met and or a specified
means we can successfully switch an int or a char data number of times. Just like other programming languages, C
expression) is initially false the statements are not executed. their average is %d",evennum,sum,avg);
}
Void main( )
{ 4.4.2 The do…while loop in C
Int i,n,evennum=0,numbers,avg=0,sum=0; This type of loop just like the while loop iterates statement (s)
printf("how many numbers do you want to enter?"); provided a given condition is true. However, the condition is
usually tested at the end of the loop after executing the block of void main()
statements at least once .Its general syntax is as follows {
do inti,n,evennum=0,numbers,avg=0,sum=0;
printf("how many numbers do you want to enter?");
{
scanf("%d",&n);
//statements to be executed;
i=0;
}
do
while(condition);
{
printf("\nEnter the value of integer %d then press enter key " ,i+1);
Note the semicolon which ends the do…while statement. The scanf("%i",&numbers);
difference between while and do…while is that the while loop if (numbers%2==0)
will first test the condition at the beginning of the loop and will {
not execute even a single once if the condition is false, whereas sum=sum+numbers;
the do…while loop tests the condition at the end of the loop after evennum=evennum+1;
}
completing the first iteration.
i++;
While loop is widely used in many applications as it is more
}
natural to test for the continuation of a loop at the beginning
while (i<n);
rather than at the end of the loop.
avg=sum/evennum;
Most programs that work with while can also be implemented printf("The sum of %d numbers is % d and
using do…while. The following program calculates the sum of their average is %d",evennum,sum,avg);
digits in the same manner, except that it uses the do-while loop: }
The do…while statement should be used only in situations Termination expression, which represents a condition
where the loop must execute at least once whether or not the that must be true for the loop to continue execution.
condition is true. For example, we can use a do…while loop Step-value expression is executed after every iteration
while designing an interactive menu-driven program where the to update the value of the looping value. The for
menu is presented at least once, and then depending upon the statement is very versatile and can be written in four
choice of the user, the menu is displayed again or the session different ways:
is terminated. Omitting the Initialization Expression
4.4.3 The For statement In this case, the loop variable is initialized before the for loop.
For loop statement executes statements repeatedly for a Thus, the for loop takes the following form:
specified number of times. Its syntax is as follows. i=0;/*initialization*/
for(initialization; termination; step-value) for(; condition;step-value)
{ {
Statements to be executed //statements to be executed
} }
The while or do-while statements may also be used to specify 6.5 Chapter summary
the condition. Thus the for loop takes the following form: 6.6 Chapter exercise
for (initialization; ; step-value) 1. Write a C program which output odd numbers and their
{ squares in a tabular format. The even numbers being
condition //statements to be executed calculated range from 3 to 15
} No Square
In this case the step-value expression is written inside the body 2. Write a C program which calculates the roots of a quadratic
10 | C H A P T E R F O U R : - C O N T R O L S T R U C T U R E S