CPRGM Unit6
CPRGM Unit6
CPRGM Unit6
C- Programming
Unit 6: Control Statement
Syllabus Unit: - 6
Control statements
1. Control statements are the statements that enable us to specify the flow of program
control i.e. Control statements alters the flow of execution and provides better control
to the programmer on the flow of execution. They are useful to write better and
complex programs.
Loop:
Loop is a sequence of instructions that is repeated until a certain condition is
reached. it is used to execute the block of code several times according to the condition given
in the loop. It means it executes the same code multiple times so it saves code and also helps
to traverse the elements of an array.
There are 3 types of loop –
• for loop
• while loop
• do – while loop
For loop
The for loop is a control flow statement that iterates a part of the program
multiple times.
Teksan Gharti
BE SEM - I
C- Programming
Here the for loop work as follows
1. The initialization expression is executed only once.
2. Then, the test expression is evaluated.
3. If the test expression is evaluated to true,
(a) Codes inside the body of for loop is executed.
(b) Then the update expression is executed.
(c) Again, the test expression is evaluated.
(d) If the test expression is true, codes inside the body of for loop is executed
and update expression is executed.
(e) This process goes on until the test expression is evaluated to false.
4. If the test expression is evaluated to false, for loop terminates.
return 0;
Teksan Gharti
BE SEM - I
C- Programming
}
Program No : Factorial of given number
#include <stdio.h>
int main()
{
int num, i, facto=1;
printf("Enter a positive integer: ");
scanf("%d", &num);
While loop
The while loop is a control flow statement that executes a part of the programs repeatedly
on the basis of given Boolean condition. The general form of while loop is…
while (testExpression)
{
// body of loop
}
Here the testExpression can be any Boolean expression. The body of the loop
will be executed as long as the testExpression is true. When testExpression becomes false,
control passes to the next line of code immediately following the loop.
Teksan Gharti
BE SEM - I
C- Programming
Program No : simple for loop
#include <stdio.h>
int main()
{
int i=10;
while(i>=1)
{
printf("%d \n",i);
i--;
}
#include<stdio.h>
int main()
{
int i=1,num;
printf("Enter a number:");
scanf("%d",&num);
//printf("\n");
printf("Multiplication table of given number is :- \n");
while(i<=10)
{
printf("%d * %d = %d \n",num,i,num*i);
i++;
}
return 0;
}
Teksan Gharti
BE SEM - I
C- Programming
do-while loop
The do while loop is also a control flow statement that executes a part of the
programs at least once and the further execution depends upon the given Boolean condition. If
the number of iterations is not fixed and you must have to execute the loop at least once, it is
recommended to use do-while loop.
Syntax:
do{
//code to be executed
}while(condition);
Here the do-while loop first executes the body of the loop and then evaluates the
conditional expression. If this expression is true, the loop will repeat. Otherwise, the loop
terminates.
#include<stdio.h>
main()
{
int i=1;
do{
printf("%d \n",i);
i++;
} while(i<=10);
Teksan Gharti
BE SEM - I
C- Programming
Decision making statement:
In real life There might come the situations that when we need to make some decisions
and based on these decisions, we decide what should we do next. Similar situations arise in
programming also, where we need to make some decisions and based on these decisions, we
will execute the next block of code.
if statement
If statement is a conditional branch statement. This is a two-way branch statement.
Depending upon the whether a condition is true or false, the corresponding code is executed.
if(condition)
{
statements;
.............
…. . ….
}
If the condition is true then the statements inside the if block will be executed otherwise the
statements inside the if block will not be executed and the execution will start from the
statements that are next to the if block.
Teksan Gharti
BE SEM - I
C- Programming
Program No : simple if statement
#include<stdio.h>
int main()
{
int a,b;
printf("Enter the first number:");
scanf("%d",&a);
printf("Enter the second number:");
scanf("%d",&b);
if (a > b)
{
printf("first number is greater than second number");
}
}
If-else statement
If the condition is true then the statements inside the if block will be executed and if
the condition is false then the statements inside the else block will be executed.
Syntax
if (condition)
{
first statement;
}
else
{
second statement;
}
Here, if the condition is true then the first statement will be executed and if the condition is
false then the second statement will be executed.
Teksan Gharti
BE SEM - I
C- Programming
Program NO : simple if-else statement
#include<stdio.h>
int main()
{
int a,b;
printf("Enter the first number:");
scanf("%d",&a);
printf("Enter the second number:");
scanf("%d",&b);
if(a > b)
{
printf("first number is greater than second number");
}
else
{
printf("second number is greater than first number");
}
}
Nested If…Else
Placing If Statement inside body of another IF or Else Statement is called as Nested
If…Else statement in C Programming.
Teksan Gharti
BE SEM - I
C- Programming
Output:
Teksan Gharti
BE SEM - I
C- Programming
Output:
#include<stdio.h>
int main()
{
int a,b,c;
clrscr();
printf("Enter the Ages of a,b and c\n");
scanf("%d%d%d",&a,&b,&c);
if(a < b)
{
if(a < c)
{
printf("a is Youngest");
}
else
{
printf("c is Youngest");
}
}
else
{
if(b < c)
{
printf("b is Youngest");
}
else
{
printf("c is Youngest");
}
}
Return 0;
}
Teksan Gharti
BE SEM - I
C- Programming
Output:
#include<stdio.h>
int main()
{
int age;
printf("Please Enter Your Age Here:\n");
scanf("%d",&age);
if ( age < 18 )
{
printf("You are Minor.\n");
printf("Not Eligible to Work");
}
else
{
if (age >= 18 && age <= 60 )
{
printf("You are Eligible to Work \n");
printf("Please fill in your details and apply\n");
}
else
{
printf("You are too old to work as per the Government rules\n");
printf("Please Collect your pension! \n");
}
}
return 0;
}
Output:
Teksan Gharti
BE SEM - I
C- Programming
If else-if ladder Statement
The if-else-if ladder statement is an extension to the if-else statement. It is used in the
scenario where there are multiple cases to be performed for different conditions. In if-else-if
ladder statement, if a condition is true then the statements defined in the if block will be
executed, otherwise if some other condition is true then the statements defined in the else-if
block will be executed, at the last if none of the condition is true then the statements defined
in the else block will be executed. There are multiple else-if blocks possible. It is similar to
the switch case statement where the default is executed instead of else block if none of the
cases is matched.
if (Condition1)
{
Statement1;
}
else if(Condition2)
{
Statement2;
}
.
.
.
else if(ConditionN)
{
StatementN;
}
else
{
Default_Statement;
}
In the above general syntax if the Condition1 is TRUE then the Statement1 will be
executed and control goes to next statement in the program following if-else-if ladder. If
Condition1 is FALSE then Condition2 will be checked, if Condition2 is TRUE then
Statement2 will be executed and control goes to next statement in the program following if-
else-if ladder. Similarly, if Condition2 is FALSE then next condition will be checked and the
process continues. If all the conditions in the if-else-if ladder are evaluated to FALSE, then
Default_Statement will be executed.
Teksan Gharti
BE SEM - I
C- Programming
Flowchart for if-else-if Ladder Statement
Figure:
Working of if-else-if Ladder
Teksan Gharti
BE SEM - I
C- Programming
Output:
switch statement
Switch statement is a multiway branch statement. it executes one statement from
multiple conditions. In other words, the switch statement tests the equality of a variable against
multiple values. The general form of a switch statement:
switch (expression)
{
case value1:
// statement sequence
break;
case value2:
// statement sequence
break;
...
case valueN:
// statement sequence
break;
default:
// default statement sequence
}
Teksan Gharti
BE SEM - I
C- Programming
Flowchart diagram of Switch Statement
#include<stdio.h>
int main()
{
int day;
printf("Please Enter the day number: ");
scanf("%d",&day);
switch (day) {
case 1:
printf( "First day is Sunday");
break;
case 2:
printf("Second day is Monday");
break;
case 3:
printf(" Third day is Tuesday");
break;
case 4:
printf("Forth day is Wednesday");
break;
case 5:
printf("Fifth day is Thursday");
break;
case 6:
printf("Sixth day is Friday");
break;
case 7:
Teksan Gharti
BE SEM - I
C- Programming
printf("Seventh day is Saturday");
break;
default:
printf("Invalid day" );
break;
}
return 0;
}
Break statement
If we want to go out of a loop then we use a break statement. If we use a break statement
in a loop then execution will continue with the immediate next statement outside the loop. After
a break, all the remaining statements in the loop are skipped. The break statement can be used
in a while loop, for loop, do-while loop, and in a switch case.
#include<stdio.h>
int main()
{
int n = 1;
while (n<=10)
{
printf("%d \n",n);
n++;
if (n == 5)
{
break;
}
}
}
goto statement:-
• goto is a jumping statement in c language, which transfer the program’s control from
one statement to another statement where label is defined.
• goto can transfer the program’s control within the same block and there must a label,
where you want to transfer program’s control.
goto label;
... .. ...
... .. ...
label:
statement;
Teksan Gharti
BE SEM - I
C- Programming
How it works:
#include <stdio.h>
int main()
{
int num;
printf("Please Enter the number: ");
scanf("%d",&num);
if (num % 2 == 0)
{
goto even;
}
else
{
goto odd;
}
even:
printf("%d is even number", num);
return 0;
odd:
printf("%d is odd number ", num);
return 0;
}
Teksan Gharti
BE SEM - I
C- Programming
continue statement
The continue statement passes control to the end of the immediately
enclosing while, do, or for statement. The continue statement is equivalent to a goto statement
within an iteration statement that passes control to the end of the loop body. For example, the
following two loops are equivalent:
while(1) while(1)
{ {
. .
. .
. .
goto label_1; continue;
. .
. .
. .
label_1:
; ;
} }
Or in another words, the continue statement skips the remaining statements of the
body of the loop where it is defined but instead of terminating the loop, the control is transferred
to the beginning of the loop for next iteration. The loop continues until the test condition of the
loop becomes false. The continue statement can be used only in loops.
#include <stdio.h>
int main()
{
int i;
for(i=1; i<10; i++)
{
printf("%d \n",i );
if (i%2 == 0)
{
continue;
}
printf("=====");
}
}
Teksan Gharti
BE SEM - I
C- Programming
Output:
#include <stdio.h>
int main()
{
int i,j;
for (i=0; i<5; i++)
{
for(j=0; j<10; j++)
{
if(j > i)
continue;
printf(" %d " ,i * j);
}
printf(" \n");
}
}
Output:
Teksan Gharti