Unit 2
Unit 2
Control statements
⚫ The statement of C are broadly classified into following categories:
1. Sequence – Statements are executed one after another.
2. Selection – Selects the path of execution based on a condition.
3. Looping – Permits repetitive execution of statements till a condition is satisfied.
4. Unconditional or Jump Statements
Types of Control Statements:
Control Statements
Conditional Unconditional
C has some kinds of statements that permit the execution of a single statement, or a block of statements, based
on the value of a conditional expression or selection among several statements based on the value of a conditional
expression or a control variable.
Statement 1;
Statement n;
}
Next Statement;
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int m,n,a;
clrscr();
printf(“Enter 2 numbers:”);
scanf(“%d%d”,&m,&n); if(m>n)
{
a=m;
m=n;
n=a;
}
printf(“The interchanged values are: “%d%d”,m,n);getch();
}
(ii) if-else statement:
It is otherwise known as Two-way decisions. It is handled with if-else statements. Thedecision is based
on a ‘test expression or condition’ that evaluates to either true or false.
✓ If the test condition is true, the true block will be executed then control goes to the next executable
statement.
✓ If the test condition is false, the false block will be executed control goes to the next executable
statement.
Syntax: Flowchart
if(condition is true)
{
True block;
}
else
{
false block;
}
Next statement;
Program:
#include<stdio.h>
main()
{
int a,b;
printf(“Enter two numbers:”);
scanf(“%d%d”,&a,&b); if(a>b)
{
printf(“A is largest”);
}
else
{
printf(“B is largest”);
}
getch();
}
OUTPUT:
Syntax: Flowchart
if(condition 1)
{
if(condition 2)
{
True statement 2
}
else
{
False statement 2;
}
}
else
{
False statement 1;
}
Next Statement;
Program:
#include<stdio.h>
main()
{
int a b c;
printf(“Enter the value for A, B and C:”);
scanf(“%d%d%d”,&a,&b,&c);
if((a>b)&&(a>c))
{
printf(“A is largest”);
}
else
{
if(b>c)
{
printf(“B is largest”);
}
else
{
printf(“C is largest”);
}
}
}
Output:
Syntax Flowchart
if(condition1)
statement 1;
else if(condition 2)
statement 2;
else if(condition 3)
statement 3;
else
default statement;
Example:
--------
If a case match is NOT found, then the default statement is executed, and the control
goes out of the switch block.
Syntax
switch( expression )
{
case value-1:
Block-1;
Break;
case value-2:
Block-2;
Break;
case value-n:
Block-n;
Break;
default:
default Block;
Break;
}
Statement-x;
How switch works
Program: // C program to print the day using switch
#include <stdio.h>
int main()
{
int day;
The following steps are should need to construct the looping concept in a program.
➢ Initialization of a counter variable
o It is a variable used in the loop
➢ Test condition
➢ Body of the loop
o Block of statements depends on the test condition
➢ Updating the counter variable
o Example: Increment/Decrement
There are three types of looping, namely:
1. while loop
2. do-while loop
3. for loop
Example
Program :
….
#include<stdio.h>
main()
{
int i=1;
while(i<=10)
{
}
Output:
p intf(“%d\t”,i);i++;
r }
1 2 3 4 5 6 7 8 9 10
The do while loop is a post tested loop. Using the do-while loop, we can repeat the execution of several parts of the
statements. The do-while loop is mainly used in the case where we need to execute the loop at least once.
Syntax:
Example Program:
#include<stdio.h>
int main(){
int i=1;
do{
printf("%d \n",i);
i++;
}while(i<=5);
return 0;
}
Output
1
2
3
4
5
iii)for loop:
For loop is another repetitive control structure and it is used to execute set ofinstructions
repeatedly until the condition becomes false.
Syntax of for loop:
1
2
3
UNCONDITIONAL (or) JUMP STATEMENTS IN C
(i) Break
(ii) Continue
(iii) Goto
(iv)exit
1.break statement:
1. It is used to terminate the loop. When the keyword break is used inside any‘C’
loop,control automatically transferred to first statement after the loop.
2. A break is usually associated with an if statement.
Syntax:
break;
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int i; for(i=1;i<=10;i++)
{
if(i==6) break;
printf(“%d”,i);
}
}
Output:
12345
2.Continue statement
When the statements continue is used inside any ‘C’ loop, control automatically passes to the beginning of
the loop.
It is also associated with if statement.
Syntax:
continue;
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int i,n,sum==0;
for(i=1;i<=5;i++)
{
print(“Enter any number…\n”);
scanf(“%d”,&n);
if(n<0)
continue;
else
sum=sum+n;
}
printf(“sum is…%d”,sum);
}
Output:
3.Goto statement
✓ It is used to transfer control unconditionally from one place to another place.
✓ A goto statement can cause program control almost anywhere in the program
unconditionally.
✓ It requires a label to identify the place to move the execution.
✓ A label is a valid variable name and must be ended with colon (:).
Syntax:
goto label;
………
………
label:
label:
…….
…….
goto label;
Program:
#include<stdio.h>
main()
{
int a,b;
printf(“Enter the numbers”);
scanf(“%%d”,&a,&b); if(a==b)
goto equal;
else
{
printf(“\n A and B are not equal”);
exit(0);
}
The exit function in c is defined under the stdlib. h header file is used to
terminate the function currently running. Parameters of exit() in C
The exit() function in C only takes a single parameter status which is the exit code that is
returned to the caller i.e. either the operating system or parent process. There are two valid
values we can use as the status each having a different meaning. They are as follows:
• 0 or EXIT_SUCCESS: 0 or EXIT_SUCCESS means that the program has been
successfully executed without encountering any error.
• 1 or EXIT_FAILURE: 1 or EXIT_FAILURE means that the program has encountered an
error and could be executed successfully.’
Example :
#include <stdio.h>
#include <stdlib.>
int main ()
{
printf ( " Start the execution of the program. \n");
printf (" Exit from the program. \n ");
// use exit (0) function to successfully execute the program
exit (0);
printf ( "Terminate the execution of the program.\n ");
return 0;
}