Control Structures in C- Conditional Statements
Control Structures in C- Conditional Statements
Definition
Control structures represent the forms by which statements in a program are executed. Flow
of control refers to the order in which the individual statements, instructions or function calls
of a program are executed or evaluated.
IMPORTANCE OF CONTROL STRUCTURES
Generally, a program should execute the statements one by one until the defined end. This
type of a program structure is called sequential structure. The functionality of this type of
program is limited since it flows in a single direction. However, all high-level programming
languages enable the programmer to change the flow of program execution. This is done by
the use of control structures whose main benefits are to enable decision making and
repetition as well as giving the power to do far more complex processing and provide
flexibility with logic. The sophisticated logic is necessary for a program to solve complex
problems.
The kinds of control flow statements supported by different languages vary, but can be
categorized by their effect:
continuation at a different statement i.e. unconditional jump e.g. GoTo statements
executing a set of statements only if some condition is met i.e. choice
executing a set of statements zero or more times, until some condition is met i.e. loop
executing a set of distant statements, after which the flow of control usually returns e.g.
subroutines/functions
TYPES OF CONTROL STRUCTURES
There are three types in C:
1. Sequence structures
Program statements are executed in the sequence in which they appear in the program.
2. Selection structures/Decision Structures
Statement block is executed only if some condition is met. These structures include if, if/else,
and switch. Selection structures are extensively used in programming because they allow the
program to decide an action based upon user's input or other processes for instance in
password checking.
3. Repetition/Iterative structures/ loops
This is where a group of statements in a program may have to be executed repeatedly until
some condition is satisfied. These include while, do/while and for
CONDITIONAL STATEMENTS
b) THE IF/ELSE
While if only performs an action if the condition is true, if/else specifies an action to be
performed both when the condition is true and when it is false. E.g.
Pseudocode:
If student’s grade is greater than or equal to 60
Print “Passed” else
Print “Failed”