0% found this document useful (0 votes)
2 views

Control Structures in C

The document provides an overview of control structures in the C programming language, emphasizing their importance in determining program flow and making decisions. It categorizes decision-making structures into if-else and switch statements, and outlines loop structures such as for, while, and do-while loops. Additionally, it discusses the break and continue statements for managing loop execution.

Uploaded by

fishatiemay19
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Control Structures in C

The document provides an overview of control structures in the C programming language, emphasizing their importance in determining program flow and making decisions. It categorizes decision-making structures into if-else and switch statements, and outlines loop structures such as for, while, and do-while loops. Additionally, it discusses the break and continue statements for managing loop execution.

Uploaded by

fishatiemay19
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Mekelle University

Mekelle Institute of Technology


Department of Information Technology
Programing in C(IT1202) Lecture Materials
Topic: Control Structures in C
Control Structures in C(1)

 Control structures are fundamental constructs of any programming language


including 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)

 Most algorithms rely on control structures to function correctly. Sorting


algorithms, Searching algorithms, and many other complex algorithms mainly
use control structures

 Without control structures, a program would execute linearly, from top to


bottom, without any ability to make decisions or repeat tasks.

 We can conclude that control structures have an indispensable role in


programming.

 Without control structures, it would be impossible to write meaningful and


functional programs and programming would have been very inefficient and
challenging.
Making Decisions C(1)
Making Decisions C(2)
 One of the essential features of computer programs is their ability to make
decisions.
 Like a train that changes tracks depending on where the train is going and how
the switches are set as indicated in the previous slide, a program can take
different actions depending on inputs and the given conditions.

 Control structures that are used for making decisions can be categorized into two
major categories : If-else statements and Switch statements

 If-else statements - allow the program to execute certain sections of code


based on specific conditions.

 Switch statements - provide a clear and organized way to execute different


blocks of code based on the value of a variable. Such statements are especially
useful for handling multiple conditions that depend on a single variable.
Making Decisions C(3)
 The if-else statements are further divided into three categories:

1. If-statement

2. If-else statement

3. If – else if-else if . . . else statement


The if 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, no statements are executed

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

 Generating repetitive outputs

 Performing calculations over a range of values


The for loop(3)
 The flowchart indicated below shows how the for loop works in C
programming
The for loop(4) -Example
The while loop(1)
 A while loop is used to execute a given statement(s) several times as
long as the given condition is true
 The condition is checked first before execution of any statement begins
 As soon as condition becomes false, the control comes out of the while
loop and continues executing the immediate next statements
 A condition is usually a relational statement, which is evaluated to either
true or false value
 A value equal to zero is treated as false and any non-zero value works
like a true for the while condition
 The following is the general form of a while loop statement:
The while loop(2)
The while loop(3) --- Example
The do …while loop(1)
 A do…while loop is used to execute a given statement(s) many times but
it allows these statements to be executed once without checking the
condition
The do …while loop(2) – Example 1
The do …while loop(2) – Example 2
The do …while loop(2) – Example 3
The break and continue statements(1)

 The break statement allows for immediate exit from a loop or switch
statement

 This can be used to optimize performance by avoiding unnecessary


iterations and improving clarity by breaking out of loops when a
condition is met.

 It is mainly used to terminate a repetitive task and execution resumes at


the next statement following the loop.

 The syntax for a break statement in C is as follows: break;


The break and continue statements(2)
 The continue statement in C programming language works somewhat like the
break 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.

 The syntax for a continue statement in C is as follows: continue;


The break and continue statements(3)
 The break statement can be diagrammatically represented using the following
flow chart on the left and a C program example is shown on the right:
The break and continue statements(4)
 The continue statement can be diagrammatically represented using the
following flow chart on the left and a C program example is shown on
the right :

You might also like