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

Control Structures in C- Conditional Statements

Control structures in programming dictate the flow of execution of statements, allowing for decision making and repetition. In C, there are three main types: sequence structures, selection structures (like if, if/else, and switch), and repetition structures (like loops). These structures enable complex problem-solving by altering the linear flow of execution based on conditions and user input.

Uploaded by

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

Control Structures in C- Conditional Statements

Control structures in programming dictate the flow of execution of statements, allowing for decision making and repetition. In C, there are three main types: sequence structures, selection structures (like if, if/else, and switch), and repetition structures (like loops). These structures enable complex problem-solving by altering the linear flow of execution based on conditions and user input.

Uploaded by

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

CONTROL STRUCTURES

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

a) THE IF SELECTION STRUCTURE


– Used to choose among alternative courses of action i.e. the if statement provides a junction
at which
the program has to select which path to follow. The if selection performs an action only if the
condition is true,
General form
If (expression) statement
Pseudocode:
If student’s marks is greater than or equal to 600 Print “Passed”
As in
if (marks>=600) printf(“Passed”);
If condition is true
– Print statement executed and program goes on to next statement
– If false, print statement is ignored and the program goes onto the next statement
NB/ Indenting makes programs easier to read
Flow chart for the if selection structure
NB/ The statement in the if structure can be a single statement or a block (Compound
statement). If it’s a block of statements, it must be marked off by braces

Flow chart for the if selection structure


NB/ The statement in the if structure can be a single statement or a block (Compound
statement). If it’s a block of statements, it must be marked off by braces.
if (expression) {
Block of statements
}
As in
If (salary>5000) {
tax_amount = salary * 1.5;
printf(“Tax charged is %f”, tax_amount); }

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”

Flow chart for the if/else selection structure


Example
if (x >=100) {
printf(“Let us increment x:\n”); x++;
}
else
printf(“x < 0 \n);
(c) THE IF...ELSE IF...ELSE STATEMENT
Test for multiple cases/conditions.
– Once a condition is met, the other statements are skipped
– Deep indentation usually not used in practice
Pseudocode for an if..else if..else structure
If student’s grade is greater than or equal to 90 Print “A”
Else If student’s grade is greater than or equal to 80 Print “B”
else If student’s grade is greater than or equal to 70 Print “C”
else If student’s grade is greater than or equal to 60
Print “D” else
Print “F”
Example
#include <stdio.h>
main() { int marks;
printf("Please enter your MARKS:"); scanf("%d", &marks);
if (marks>=90 && marks <=100) printf("Your grade is A\n");
else if (marks>=80 && marks <=89) printf("Your grade is B\n");
else if (marks>=70 && marks <=79) printf("Your grade is C\n");
else if (marks>=60 && marks <=69) printf("Your grade is D\n");
else if (marks >100)
printf("Marks out of range\n"); else
printf("Your grade is F\n"); }
(d) NESTED IF STATEMENTS
One if or else if statement can be used inside another if or else if statement(s).
Syntax
The syntax for a nested if statement is as follows: if (boolean_expression 1)
{
/* Executes when the boolean expression 1 is true */
if(boolean_expression 2)
{
/* Executes when the boolean expression 2 is true */ }
}
You can nest else if...else in the similar way as you have nested if statement.
Example #include <stdio.h> int main ()
{
/* local variable definition */ int a = 100;
int b = 200;
/* check the boolean condition */ if( a = = 100 )
{
/* if condition is true then check the following */
if( b = = 200 )
{
/* if condition is true then print the following */ printf("Value of a is 100 and b is 200\n" );
} } return 0;
}
When the above code is compiled and executed, it produces the following result:
Value of a is 100 and b is 200
Exact value of a is : 100
Exact value of b is : 200
(e) SWITCH STATEMENT
A switch statement allows a variable to be tested for equality against a list of values. Each
value is called a case, and the variable being switched on is checked for each switch case.
Syntax
The syntax for a switch statement in C programming language is as follows:
switch(expression)
{
case constant-expression statement(s);
break;
case constant-expression : statement(s);
break;
/* you can have any number of case statements */ default :
statement(s); }
The following rules apply to a switch statement:
1) You can have any number of case statements within a switch. Each case is followed by the
value
to be compared to and a colon.
2) The constant-expression for a case must be the same data type as the variable in the switch
3) When the variable being switched on is equal to a case, the statements following that case
will
execute until a break statement is reached.
4) When a break statement is reached, the switch terminates, and the flow of control jumps to
the
next line following the switch statement.
5) Not every case needs to contain a break. If no break appears, the flow of control will fall
through
to subsequent cases until a break is reached.
6) A switch statement can have an optional default case, which must appear at the end of the
switch.
The default case can be used for performing a task when none of the cases is true. No break is
needed in the default case.
Sample Switch Statement
#include<stdio.h> void main()
{
char grade;
printf("Enter your grade:"); scanf("%c", &grade);
switch (grade) {
case 'A': printf("Excellent!\n"); break;
case 'B':
printf("Very Good!\n");
break;
case 'C': printf("Good!\n"); break;
case 'D':
printf("Work harder!\n"); break;
default: printf("Fail!\n");
}}
(f) NESTED SWITCH STATEMENTS
It is possible to have a switch as part of the statement sequence of an outer switch. Even if
the case constants of the inner and outer switch contain common values, no conflicts will
arise.
Syntax
The syntax for a nested switch statement is as follows: switch(ch1) {
case 'A':
printf("This A is part of outer switch" );
switch(ch2) { case 'A':
printf("This A is part of inner switch" ); break;
case 'B':
} break; case 'B': }
Example
#include <stdio.h> int main ()
{
/* local variable definition */ int a = 100;
int b = 200; switch(a) { case 100:
printf("This is part of outer switch\n", a );
switch(b) { case 200:
printf("This is part of inner switch\n", a );
printf(“A is equals to %d and B is equals to %d”, a, b); }
}
printf("Exact value of a is : %d\n", a ); printf("Exact value of b is : %d\n", b ); return 0;
}
When the above code is compiled and executed, it produces the following result:
This is part of outer switch
This is part of inner switch
A is equals to 100 and B is equals to 200
Exact value of a is : 100
Exact value of b is : 200

You might also like