CPRGM Unit6

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

BE SEM - I

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.

2. They change the flow of execution based on some logical condition.

3. Control statements make possible to take decisions, to perform tasks repeatedly or to


jump from one section of code to another. Control statements like if, if-else, switch,
while loop, break, continue, etc control the flow program execution.

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.

It has a general form…

for (initialization expr; test expr; update expr)


{
// body of the loop
// statements we want to execute
}

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.

Flowchart diagram of For loop

Fig: Flowchart diagram of for loop

Program No -: simple for loop


#include<stdio.h>
int main()
{
int i=0;
for(i = 1; i<= 10; i++)
{
printf("%d \n",i);
}

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);

for(i = 1; i <= num; ++i)


{
facto=facto*i;
}
printf("factorial of given number is = %d", facto);
return 0;
}

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.

Flowchart diagram of While loop

Fig: Flowchart diagram of while 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--;
}

Program No : Multiplication table of given number

#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.

Flowchart diagram of Do While loop

Fig: Flowchart diagram of do while loop

Program No :Do while loop

#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.

In Programming language decision making statement decides the block of statement


is to be executed or not which is depend on the condition. If the condition is "true" statement
block will be executed, if condition is "false" then statement block will not be executed. Here
the conditional stament are IF,IF ELSE, Nested IF….ELSE etc

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.

Here is the general form of the if statement:

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.

Flowchart diagram of If statement

Fig: Flowchart diagram of If statement

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.

Flowchart diagram of If Else Statement

Fig: Flowchart diagram of If Else Statement

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.

Program No 15: simple Nested If…else statement


int main()
{
int num1, num2;
printf("Input the value of num1:");
scanf("%d", &num1);
printf("Input the value of num2:");
scanf("%d",&num2);
if (num1 != num2)
{
printf("num1 is not equal to num2\n");
if (num1 > num2)
{
printf("num1 is greater than num2\n");
}
else
{
printf("num2 is greater than num1\n");
}
}
else
{
printf("num1 is equal to num2\n");
}
return 0;
}

Teksan Gharti
BE SEM - I
C- Programming
Output:

Program No 16: Classification of age using Nested If…else statement


#include<stdio.h>
int main()
{
int age;
printf("Please Enter you Age:");
scanf("%d",&age);
if(age<=60)
{
if(age<=50)
{
if(age<=23)
{
if(age<=19)
{
if(age<13)
{
printf("You are child \n");
}
else{
printf("You are teenage \n ");
}
}
else{
printf("You are young \n ");
}
}
else{
printf ("You are adult \n ");
}
}
else{
printf ("You are in oldage \n ");
}
}
else
{
printf ("Your age is greater then 60 \n");
}
}

Teksan Gharti
BE SEM - I
C- Programming
Output:

Program: program to determine the youngest of the three

#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:

Program: program to check whether person is eligible for work or not.

#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.

General syntax for if-else-if statement is:

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

Program: Program to print grade of a student using If Else Ladder Statement.


#include<stdio.h>
int main()
{
int marks;
printf("Enter your marks between 0-100\n");
scanf("%d", &marks);
if(marks >= 90)
{
printf("YOUR GRADE : A\n");
}
else if (marks >= 70 && marks < 90)
{
printf("YOUR GRADE : B\n");
}
else if (marks >= 50 && marks < 70)
{
printf("YOUR GRADE : C\n");
}
else
{
printf("YOUR GRADE : Failed\n");
}
return 0;
}

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
}

Working of switch statement:


• The expression is evaluated once and compared with the values of each case value.
• If there is a match, the corresponding statements after the matching case value are
executed.
• If there is no match, the default statements are executed.

Rules for switch statements


• Duplicate case values are not allowed.
• The value for a case must be of the same data type as the variable in the switch.
• The value for a case must be a constant or a literal. Variables are not allowed.
• The break statement is used inside the switch to terminate a statement sequence.
• The break statement is optional. If omitted, execution will continue on into the next
case.
• The default statement is optional and can appear anywhere inside the switch block.

Teksan Gharti
BE SEM - I
C- Programming
Flowchart diagram of Switch Statement

Fig: Flowchart diagram of Switch Statement


Program NO : Day number using 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.

Program No : break statement

#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.

Syntax of goto Statement

goto label;
... .. ...
... .. ...
label:
statement;

Teksan Gharti
BE SEM - I
C- Programming
How it works:

Program No : goto statement

#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.

Program No : continue statement

#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:

Program No: continue statement

#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:

======================== End unit 6 ==================

Teksan Gharti

You might also like