KEMBAR78
UNIT 2 Control Structures | PDF | Control Flow | Mathematical Logic
0% found this document useful (0 votes)
9 views21 pages

UNIT 2 Control Structures

This document serves as an introduction to programming with a focus on control structures in the C language, covering conditional statements, loops, and decision-making constructs. It details various types of control statements such as if, if-else, nested if-else, else-if ladder, and switch statements, along with examples and flowcharts. Additionally, it explains loop structures including while and do-while statements, emphasizing the importance of control flow in programming.

Uploaded by

pavancharan576
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views21 pages

UNIT 2 Control Structures

This document serves as an introduction to programming with a focus on control structures in the C language, covering conditional statements, loops, and decision-making constructs. It details various types of control statements such as if, if-else, nested if-else, else-if ladder, and switch statements, along with examples and flowcharts. Additionally, it explains loop structures including while and do-while statements, emphasizing the importance of control flow in programming.

Uploaded by

pavancharan576
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

INTRODUCTION TO PROGRAMMING

UNIT – 2
CONTROL STRUCTURES
SIMPLE SEQUENTIAL PROGRAMS
CONDITIONAL STATEMENTS
 The term flow of control refers to the order in which the program
statements are executed.
 Selection is used to select which statements are to be performed next
based on the condition being true or false.
 Repetition is used to repeat the set of statements.
 Invoking means invoking sequence of instructions using a single
statement in a calling function.
 Iteration means executing a loop of statements once.
 Recursion means calling the function by itself.
 ‘C’ language possess decision making capabilities to support some
statements known as control statements or decision making
statements or branching statements.

if Statement
 The if statement is powerful decision making statement.
 It is used to control the flow of execution of statements.
 The if statement may be complexity of conditions to be tested.
a) Simple if statement (null else)
b) if else statement
c) Nested if - else statement
d) else – if ladder statement

TWO WAY SELECTION


Simple if Statement
 The simple if statement is powerful decision making statement.
 It is called as null else statement.
 It is used to control the flow of execution of statements.
 The general form of simple if statement is
if (test expression)
Dr. Ratna Raju Mukiri Ph.D.,
Dept. of CSE, SACET.
1
INTRODUCTION TO PROGRAMMING

{
statement block;
}
statement - x ;
 The statement block may be a single statement or a group of
statements.
 If the test expression is true then the statement block will be
executed. Otherwise the statement block will be skipped and the
execution will jump to the statement – x.
 If the condition is true both the statement - block and statement - x in
sequence are executed.

Flow Chart

Ex :
if(category = sports)
{
marks = marks + bonus marks;
}
printf(“%d”, marks);

If the student belongs to the sports category then additional bonus


marks are added to his marks before they are printed. For others bonus
marks are not added.
Program to explain about simple if statement
#include<stdio.h>
int main(void)
{
Dr. Ratna Raju Mukiri Ph.D.,
Dept. of CSE, SACET.
2
INTRODUCTION TO PROGRAMMING

int a, b;
clrscr();
printf(“ enter the values of a & b:”);
scanf(“%d%d”, &a, &b);
if(a==b)
{
printf(“a is equal to b”);
}
return 0;
}

if – else Statement
 The if - else statement is an extension of the simple if statement.
 The general syntax for if else statement is as follows.
if (test expression)
{
true-block statements;
}
else
{
false-block statements;
}
statement – x;

Flow Chart

If the test expression is true then true-block statements are executed,


otherwise the false–block statements are executed. In both cases either true-
block or false-block will be executed but not both.
Dr. Ratna Raju Mukiri Ph.D.,
Dept. of CSE, SACET.
3
INTRODUCTION TO PROGRAMMING

Ex:
if (code == 1)
boy = boy + 1;
else
girl = girl + 1;
st-x;

Here if the code is equal to ‘1’ the statement boy=boy+1; is executed


and the control is transferred to the statement st-x, after skipping the else
part. If code is not equal to ‘1’ the statement boy=boy+1; is skipped and the
statement in the else part girl=girl+1; is executed before the control reaches
the statement st-x.

Program to explain about if else statement


#include<stdio.h>
int main(void)
{
int num;
clrscr();
printf(“enter the num value “);
scanf(‘%d”, &num);
if(num>0)
{
printf(“the number is positive”);
}
else
{
printf(“the number is negative”);
}
return 0;
}

Dr. Ratna Raju Mukiri Ph.D.,


Dept. of CSE, SACET.
4
INTRODUCTION TO PROGRAMMING

Nested if – else statement


 When a series of decisions are involved we may have to use more than
one if-else statement in nested form. This is known as nested if – else
statement.
 It is also called as chain of if’s.
 The syntax is as follows.
if(test expression1)
{
if(test expression2)
{
st –1;
}
else
{
st – 2;
}
}
else
{
st – 3;
}
st – x;

If the test expression1 is true then the inner if is evaluated, if the


inner if is true then st-1 is executed otherwise st-2 is executed. If the test
expression1 is false then st-3 is executed. Later finally st-x is executed.

Ex
if(sex ==Male)
{
if(pay>10000)
{
bonus=0.5*pay;

Dr. Ratna Raju Mukiri Ph.D.,


Dept. of CSE, SACET.
5
INTRODUCTION TO PROGRAMMING

else
bonus=0.2*pay;
}
}
else
{
bonus=0.6*pay;
}
pay=pay+bonus;
printf(“%f”, pay);

Flow Chart

If the test expression1 is false then st-3 will be executed otherwise it


continues to perform the nested if – else structure (inner part). If the
condition1 is true the st-1 will be executed otherwise the st-2 will be
evaluated and then the control is transferred to the st-x

Program to explain about nested if else statement


#include<stdio.h>
int main(void)
{
int a, b, c;
clrscr();
printf(“enter the values of a, b, c”);
scanf(“%d%d%d”, &a, &b, &c);
if(a>b)

Dr. Ratna Raju Mukiri Ph.D.,


Dept. of CSE, SACET.
6
INTRODUCTION TO PROGRAMMING

{
if(a>c)
{
printf(“%d a is big”, a);
}
else
{
printf(“%d c is big”, c);
}
}
else
if(c>b)
{
printf(“%d c is big”, c);
}
else
{
printf(“%d b is big”, b);
}
return 0;
}

MULTI WAY SELECTION


else - if ladder
 A multi path decision is chain of if’s in which the statement is
associated with each else is an if.
 It takes the following general form.
if (condition1)
St –1;
else if (condition2)
St –2;
else if (condition 3)
St –3;

Dr. Ratna Raju Mukiri Ph.D.,


Dept. of CSE, SACET.
7
INTRODUCTION TO PROGRAMMING

else
default – st;
St –x;

If the condition1 is found to be true then st-1 is executed then st-x is


executed in sequence. If it is found to be false then else part containing the
condition2 is evaluated and if found true then st-2 is executed and so on…..

Flow Chart
 This construct is known as the else-if ladder.
 The conditions are evaluated from the top of the ladder to
downwards.
 As soon as a true condition is found the statement associated with
it is executed and the control is transferred to the st-X (i.e. skipping
the rest of the ladder).
 When all the n-conditions become false then the final else
containing the default – st will be executed.

Ex:
if (code = = 1)
grade = ‘A’;
else if ( code = = 2)

Dr. Ratna Raju Mukiri Ph.D.,


Dept. of CSE, SACET.
8
INTRODUCTION TO PROGRAMMING

grade = ‘B’;
else if (code = = 3)
grade = ‘C’;
else if (code = = 4)
grade = ‘D’;
else
grade = ‘F’;

If code number is other than 1, 2, 3 and 4 then grade is “F”.

Program to explain about if else ladder


#include<stdio.h>
int main(void)
{
float s1, s2, s3, total, per;
clrscr();
printf(“ enter the values for s1,s2, s3:”);
scanf(“%f%f%f”,&s1, &s2, &s3);
total = s1 + s2 + s3;
per = total / 3;
if(per >= 60)
printf(“first class”);
else if(per >= 50)
printf(“second class”);
else if(per >= 40)
printf(“third class”);
else
printf(“fail”);
return 0;
}

Dr. Ratna Raju Mukiri Ph.D.,


Dept. of CSE, SACET.
9
INTRODUCTION TO PROGRAMMING

Switch Statement
 The another built-in multi-way decision statement in C language is
known as a switch.
 The general form of the switch statement is as follows.
switch (expression)
{
case value1: block1;
break;
case value 2 : block 2;
break;

default : default block;


}
st – x;

Flow Chart

 The expression is an integer expression.


 The character value1, value-2------ are constants or constant
expressions known as case labels.
 Each of the values should be a unit within a switch statement and
may contain zero or more statements.
 When the switch is executed the value of the expression is
successively compared against the values value-1, value-2-------.
 If a case is found whose value matches with the expression then the
block of statements that follows the case are executed.

Dr. Ratna Raju Mukiri Ph.D.,


Dept. of CSE, SACET.
10
INTRODUCTION TO PROGRAMMING

 The break statement at the end of each block signal the end of a
particular case and causes an exit from the switch statement
transferring the control to the st-x following the switch.
 The default is an optional case. It will be executed if the value of the
expression doesn’t match with any of the case values and then control
goes to the St-x.
Ex:
switch (number)
{
case 1 : printf(“ONE”);
break;
case 2 : printf(“TWO”);
break;
case 3 : printf(“THREE”);
break;
case 4 : printf(“FOUR”);
break;
case 5 : printf(“FIVE”);
break;
case 6 : printf(“SIX”);
break;
case 7 : printf(“SEVEN”);
break;
case 8 : printf(“EIGHT”);
break;
case 9 : printf(“NINE”);
break;
default : printf(“ZERO”);
break;
}
Program to explain about the arithmetic operators using switch
statement
#include<stdio.h>
Dr. Ratna Raju Mukiri Ph.D.,
Dept. of CSE, SACET.
11
INTRODUCTION TO PROGRAMMING

int main(void)
{
int a , b , choice;
float c;
clrscr();
printf(“enter the values of a & b”);
scanf(“%d%d”, &a, &b);
printf(“enter the choice”);
scanf(“%d”, &choice);
switch(choice)
{
case 1 : c=a+b;
printf(“%f”, c);
break;
case 2 : c=a-b;
printf(“%f”, c);
break;
case 3 : c=a/b;
printf(“%f”, c);
break;
case 4 : c=a*b;
printf(“%f”, c);
break;
case 5 : c=a%b;
printf(“%f”, c);
break;
default: printf(“Invalid option”);
break;
}
return 0;
}

BASIC LOOP STRUCTURES


Dr. Ratna Raju Mukiri Ph.D.,
Dept. of CSE, SACET.
12
INTRODUCTION TO PROGRAMMING

The below diagram shows the concept of loop. In this diagram, the
loop is repeated again and again it will never stop.

 But we want the loop to be stopped the loop after our work is over.
 To do this we need to put a condition to control the loop. This means
that we have to design the loop so that after each iteration the
condition must be checked.
 On checking the condition if it is true we repeat the loop again or if it
is false we terminate the loop. This test condition is called loop
control expression and the variable in the test expression that is
used to control the loop is called loop control variable.

The ‘C’ language provides three loop constructs for performing loop
operations they are:
 The while statement
 The do-while statement
 The for statement

While Statement
 This a called pretest looping construct.
 This type of loop is also called an entry controlled loop construct.
 This type of loop is also called an entrance controlled loop construct.
 It is executed and if is true then the body of the loop is executed this
process is repeated until the Boolean expression becomes false.
 Ones it becomes false the control is a transferred out of the loop.
 The general form of the while statement is

Dr. Ratna Raju Mukiri Ph.D.,


Dept. of CSE, SACET.
13
INTRODUCTION TO PROGRAMMING

Flow Chart

Ex:

In the above example the loop with be executed until the condition is
false.

Program to illustrate about while statement


#include<stdio.h>
main()
{
int n, i=1,c=0;
printf(“enter the value of n”);
scanf(“%d”, &n);
while(i<=n)
{

Dr. Ratna Raju Mukiri Ph.D.,


Dept. of CSE, SACET.
14
INTRODUCTION TO PROGRAMMING

if(n%i==0)
c++;
i++;
}
if(c==2)
printf(“ the given number is prime %d”, n);
else
printf(“ the given number is not prime %d”, n);
}

Do-While Statement
 This type of loop is called post test loop.
 This type of loop is also called an exit controlled loop statement. i.e.,
the Boolean expression evaluated at the bottom of the loop and if it is
true then body of the loop is executed again and again until the
Boolean expression becomes false.
 Ones it becomes false the control is transferred out of the loop
executing the next statement. The general form of do-while statement
is

Flow Chart

Dr. Ratna Raju Mukiri Ph.D.,


Dept. of CSE, SACET.
15
INTRODUCTION TO PROGRAMMING

Ex :

Program to illustrate about do while statement


#include<stdio.h>
main()
{
int n, r,rev=0;
printf(“enter the value of n”);
scanf(“%d”, &n);
do
{
r=n%10;
rev=rev*10+r;
n=n/10;
}while(n>0);
printf(“ the reverse of the given number is %d”, rev);
}

For Statement
 This a called pretest looping construct.
 This type of loop is also called an entry controlled loop construct.
 This type of loop is also called an entrance controlled loop construct.
 The for loop provides a more concise loop control structure.
 The general form of the for loop is

Dr. Ratna Raju Mukiri Ph.D.,


Dept. of CSE, SACET.
16
INTRODUCTION TO PROGRAMMING

 Where initialization is used to initialize some parameter that


controls the looping action,
 ‘test condition’ represents if that condition is true the body of the
loop is executed, otherwise the loop is terminated.
 After evaluating the information the new value of the control
variable is again tested with the loop condition. If the condition
is satisfied the body of the loop is again executed. This process
continues until the value of the control variable becomes false to
satisfy the condition.

Flow Chart

Ex :

Program to illustrate about for statement


#include<stdio.h>
main()
{
int n, i, sum=0;
printf(“enter the value of n”);
scanf(“%d”, &n);
for(i=1;i<n;i++)
{
if(n % i == 0)
sum=sum+i;

Dr. Ratna Raju Mukiri Ph.D.,


Dept. of CSE, SACET.
17
INTRODUCTION TO PROGRAMMING

}
if(sum == n)
printf(“ the given number is perfect number”);
else
printf(“ the given number is not perfect number”);

Nested Loops
 There are several situations where we might have one loop to be
contained in another loop. Such loops are called nested loops.
 The syntax is as follows
for( ; ; )
{
-----------------
-----------------
for( ; ; )
{
---------------
---------------
}
--------------------
--------------------
}
 For example
for(i=1;i<=5;i++)
{
printf(“%d”, i);
for(j=1;j<=4;j++)
printf(“%d”, j);
}
 The first loop is controlled by “i” is called outer loop.
 The second loop is controlled by “j” is called inner loop.

Dr. Ratna Raju Mukiri Ph.D.,


Dept. of CSE, SACET.
18
INTRODUCTION TO PROGRAMMING

 We have used different variables to control each loop. For each


iteration of the outer loop, the inner loop is executed for its entire
sequence. Thus each time “i” increases the inner loop executes
completely. It is shown as follows:

When i=1 as the first iteration


j=1, j=2, j=3, j=4
When i=2 as the second iteration
j=1, j=2, j=3, j=4
When i=3 as the third iteration
j=1, j=2, j=3, j=4
When i=4 as the fourth iteration
j=1, j=2, j=3, j=4
When i=5 as the fifth iteration
j=1, j=2, j=3, j=4

Program to illustrate about nested loop statement


main()
{
int i, j;
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
printf(“*”);
printf(“\n”);
}
getch();
}
Output
*
* *
* * *
* * * *
Dr. Ratna Raju Mukiri Ph.D.,
Dept. of CSE, SACET.
19
INTRODUCTION TO PROGRAMMING

Dr. Ratna Raju Mukiri Ph.D.,


Dept. of CSE, SACET.
20
INTRODUCTION TO PROGRAMMING

BREAK AND CONTINUE


BREAK STATEMENT
 The break statement can be accomplished by using to exit the loop.
 When break is encountered inside a loop, the loop is immediately
exited and the program continues with the statement which is
followed by the loop.
 In nested loops, if we use the break statement inside one loop will
transfer the control to the next outer loop.

Ex:
for (i=1; i<5; i++)
{
if ( i == 4)
break;
printf(“%d”,i);
}

CONTINUE STATEMENT
 The continue statement which is like break statement.
 Its work to skip the present statement and continues with the next
iteration of the loop.

Ex:
for (i=1; i<5; i++)
{
if ( i == 3)
continue;
printf(“%d”,i);
}

In the above example when I=3 then the continue statement will rise
and skip statement in the loop and continues for the next iteration i.e.., I=4.

Dr. Ratna Raju Mukiri Ph.D.,


Dept. of CSE, SACET.
21

You might also like