Script 4 2
Script 4 2
Part - I
Module – 1
Dear friends,
In Today’s discussion we will cover an interesting, very useful and most
frequently used feature of C Programming named control structures. You all know
that a computer program contains a sequence of instructions and ensures that the
instructions are executed in the same order in which they appear in the program.
For Example consider a program segment to find the average of two numbers
int num1,num2,sum;
float avg;
sum=num1+num2;
avg=sum/2;
count = 1;
++i;
printf(...);
But Blocks are used to group declarations and statements together into a com-
pound statement. Braces { and } are used for creating blocks and they are syntacti-
cally equivalent to a single statement. Any variable can be declared only at the begin-
ning of the block.
For example
{ int a, b= 10;
a= b*b;
printf(“%d %d”,b, a);
}
But in practice, it may be necessary to make the sequence of the execution flow to
be transferred from one part of the program to another part. This can be achieved
with the help of control (flow) structures or control constructs.
As the name suggests the ‘control instructions’ determine the ‘flow of control’ in a
program. The control-flow of a language specifies the order in which computations
are performed. C supports the control structures classified under two main categories
Conditional and Unconditional control structures.
In the case of conditional execution, the flow of execution may be transferred from
one part of the program to another part based on the output of the conditional test
carried out.
Under conditional construct we have two categories named as selective construct
and loop construct.
Selective construct
In selective constructs, the statement is selected for execution based on the output of
the conditional test given by the expression. C supports 4 selective constructs
1
including
1. Conditional expression
2. if – else
3. switch-case
Loop construct
Sometimes the execution of certain statements need to be repeated until a given
condition is satisfied or it may have to be repeated for a known number of times.
Such repetitions are carried out by using a loop structure. The loop construct is also
known as iterative construct or a repetitive construct. C supports
1. for
2. while
and
3. do-while
loop constructs.
If the flow of execution is transferred from one part of a program to another part
without carrying out any conditional test, it is known as an unconditional execution. C
supports break statement, continue Statement and goto unconditional control
structures.
Control Structures
Conditional Unconditional
If
if-else
switch-case while
Conditional for
expression
do-while
Now let us go through various control structures one by one in the order of simplicity
with suitable examples.
Module – 2
2
If CONSTRUCT
if (test_expression)
{
True-block statements
}
it allows the computer to evaluate the expression first and then, depending on
whether the value of the expression (relation or condition) is ‘true’ (non zero) or ‘false’
(zero),it transfers the control to a particular statement. This point of program has two
paths to follow one for true condition and the other for false condition. You can see
the flow chart representation of the flow of control in if-statement in the screen
Expression True if
x==y x is equal to y
x!=y x is not equal to y
X<y x is less than y
x>y x is greater than y
X<=y x is less than or equal to y
x>=y x is greater than or equal to y
Let me now show you a sample program to illustrate the use of if-else construct
#include <stdio.h>
3
main( )
{
int mark ;
printf ( "Enter the score " ) ;
scanf ( "%d", &score) ;
if ( num >= 50 )
{
printf ( "Congratulations !!" ) ;
}
In this example, if you enter a score which is greater than or equal to 50 the
statements inside the printf will be displayed.
if-else Construct
We have seen that the if statement by itself will execute a single statement, or a
group of statements, when the expression following if evaluates to true. It does
nothing when the expression evaluates to false. Now a question for you, can we
execute one group of statements if the expression evaluates to true and another
group of statements if the expression evaluates to false? Yes this is what is the
purpose of the if-else statement.
if (test expression)
{
True-block statements
}
else
{
False-block statements
}
statement x;
………………
If the test expression is true, then the true-block statements, immediately following
the if statement are executed; otherwise , the false block statements are executed.
Here either true block or false block will be executed, not both. This control flow is
illustrated in the flow chart representation as shown in the screen.
4
Flow of control in if-else statement
Now let us go through a simple program to find the biggest of two numbers which
clearly shows the use of if-else statement.
#include<stdio.h>
void main()
{
int x,y;
printf("Enter value for x :");
scanf("%d",&x);
printf("Enter value for y :");
scanf("%d",&y);
if ( x > y )
{
printf(" The large number is %d\n", x);
}
else
{
printf("The large number is %d\n", y);
}
}
In this program, if x is the biggest one then statements inside if block is executed
otherwise the block of statements inside else part is executed.
It is obvious that, we cannot solve all problems related to decision making with a
simple if construct or if-else construct, for this we can expand this concept in the
different ways. We will discuss them one by one.
5
Nested if-else statements
When a series of decisions are involved, we may have to use more than one if-else
statement in the nested form. When an if-else construct appears as a statement in
another if-else, it is known as nested if-else construct. General form of if-else
construct is shown in the screen.
if(test condition a)
{
if(test condition b)
{
Statement 1;
}
else
{
Statement 2;
}
}
else
{
Statement 3;
}
Statement x;
To get more clarity let us now see the use of nested if-else construct as a flow chart
as shown in the screen.
6
Now let me show you a quick demo of nested if-else statement with the help of a
simple program to find the largest of three numbers.
#include<stdio.h>
Main()
{
int x,y,z;
printf(“Enter three numbers :”);
scanf(“%d %d %d”, &x , &y , &z);
if(x>y)
{
if(x>z)
{
printf(“x is the largest number”);
}
else
{
printf (“z is the largest number”);
}
}
else
{
if(z>y)
{
printf(“z is the largest number”);
}
else
{
printf (“y is the largest number”);
}
}
}
Next we will see the else if ladder statement (OR if-else if construct) and its
usage.
This method is used when multipath decisions are involved in a problem. A multipath
decision is a chain of if statements in which the statements associated with each
else is an if. The general form is :
if(condition 1)
statement-1;
else if(condition 2)
statement-2;
else if(condition 3)
statement 3;
else if(condition 4)
statement 4;
…………………
………………….
else
default statement;
statement x;
In this case every else is associated with its previous if. The last else goes to
work only if all the conditions fail. Even in else if ladder the last else is optional. If
there is no else part with the respective if, the compiler associates the else part with
the closest inner if construct which doesn’t have an else part. Care must be taken to
use braces appropriately for association of else part with its if.
7
Note that the else if clause is nothing different. It is just a way of rearranging the else
with the if that follows it. This would be evident if you look at the code given in the
screen:
if ( i == 1 )
printf ( "red” ) ; if ( i == 1 )
else printf ( "red" ) ;
{
if ( j == 2 ) else if(j==2)
printf ( "apple" ) ; printf("apple");
}
To get more clarity let us now see the use of if-else ladder statement with the help of
flow chart as shown in the screen.
Now let me show you a quick demo of else-if ladder statement with the help of a
simple C program to check the class obtained by the students from their mark
obtained from five subjects.
#include<stdio.h>
main( )
{
int m1, m2, m3, m4, m5, per ;
printf(“enter the mark of five papers”);
scanf(“%d%d%d%d%d”,&m1,&m2,&m3,&m4,&m5);
per = ( m1+ m2 + m3 + m4+ m5 ) / 5 ;
if ( per >= 60 )
printf ( "First class" ) ;
else if ( per >= 50 )
printf ( "Second class" ) ;
else if ( per >= 40 )
8
printf ( "Third class" ) ;
else
printf ( "fail" ) ;
}
Module – 3
Switch-case construct
In the previous methods when the program needs more and more alternatives the
complexity of the program increases and also it is difficult to read and follow those
codes. To overcome this, C has a built in multi-way decision statement known as
switch-case construct. The switch statement testes the value of a given
expression against a list of case values and the block of statements associated with
the matching case value is executed. Three keywords switch, case, and default, go
together to make up the control statement.
switch (expression )
{
case 1 :
statements;
break;
case 2 :
statements;
break;
case 3 :
statements;
break;
default :
statements ;
break;
}
Now we can see the use of switch-case construct with the help of a program
segment written to find the grade of students.
---------------------------------------------------
---------------------------------------------------
point =mark/total*10;
switch(point)
{
case 10 :
case 9 :
case 8 :
9
grade = ”A+”;
break;
case 7 :
case 6 :
grade = “A”;
break;
case 5 :
grade = “B”;
break;
case 4 :
grade = “C”;
break;
default :
grade = “Fail”;
break;
}
Module – 4
Conditional expression
The conditional operators ? and : named ternary operator since they take three ar-
guments. In fact, they form a kind of foreshortened if-then-else. Their general form is,
expression 1 ? expression 2 : expression 3
What this expression says is: “if expression 1 is true (that is, if its value is non-zero),
then the value returned will be expression 2, otherwise the value returned will be
expression 3”.
Expression 2 Expression 3
1
For example the segment
if(x==1)
flag=1;
else
flag=0;
can be written as
flag = (x==1) ? 1 : 0;
There are many more other features in C programming language are remaining to
explore. So let us wait for the coming sessions. Till then bye.
Summary
Assignments