Control Structures in C
Control Structures in C
They determine the flow of the program's execution and enable the program to
make decisions, repeat actions, break executions when it is found to be
necessary, skip and continue executions and choose between different paths of
execution based on a given condition.
Control structures play a great role in organizing code logically, making it more
readable maintainable and easier to understand.
Control Structures in C(2)
Control structures that are used for making decisions can be categorized into two
major categories : If-else statements and Switch statements
1. If-statement
2. If-else statement
float mark;
Enter mark;
If(mark>=95)
print “Excellent”;
If(mark<= 30)
print “Poor”;
The if…else statement(1)
The if …statement allows a program to carry out different actions
depending on the given condition
If the condition is true, the statements in the block are executed
If the condition is false, another block of statements are executed
Example 1:
float mark;
printf(“Enter your mark:”);
if(mark>=95)
printf(“Excellent”);
else
printf(“Not Excellent”);
The if…else statement(2) – Example 2
The if…else if … else if … else statement(1)
The if …else if…else if…else statement allows a program to carry
out different actions depending on the given condition.
It is used when we have more than one condition to be checked
if condition1 is true, the statements in block 1 are executed
else if condition2 is true, the statements in block 2 are executed
.
.
.
else if condition N is true, the statements in block N are executed
else the remaining statements will be executed
The if…else if …else if …else statement(2) –Example 1
Switch statement(1)
The switch statement is an alternative to the if …else if…else statement
It is used when we have more than one condition to be checked
Each value is called a case, and the variable being switched on is checked for
each switch case
The loop statements(1)
Loops are used to execute a given statement(s) repeatedly.
Loops are divided into the following categories:
For loop – executes a given block of code for a specific number of
times.
While loop – continues to execute a given block of code as long as a
given condition is true. The number of iterations is not known
beforehand.
Do… while loop – this is also useful to executes repetitive block of
code repeatedly but it ensure that a block of code is executed at least
once before the condition is tested.
The loop statements(2)
The following is the general form of a loop statement in most of
the programming languages:
The for loop(1)
A for loop is used to execute certain blocks of code repeatedly until the
condition is false. It is used in performing repetitive task in programming.
Syntax:
for(initialization; test expression; increment statement)
{
statement(s) to be executed;
}
The initialization statement is executed only once at the beginning of the
for loop
Then the test expression is checked by the program. If the test expression
is false, for loop is terminated
The for loop(2)
But if test expression is true then the statement(s) inside the body of the
for loop is(are) executed and then the increment statement is updated
This process repeats as long as the test expression is true and terminates
when the test expression is false
This is essential for tasks that require iteration such as:
Processing of arrays
The break statement allows for immediate exit from a loop or switch
statement
It skips the current iteration of a loop and continues with the next iteration.
This is useful for skipping specific cases without terminating the loop.
Instead of forcing termination, however, continue forces the next iteration of the
loop to take place, skipping any code when the condition is true.