Short Notes: Control Statements | Short Notes for Computer Science Engineering - Computer Science Engineering (CSE) PDF Download

Download, print and study this document offline
Please wait while the PDF view is loading
 Page 1


CONTROL STATEMENTS
In C, programs are executed sequentially in the order of which they appear. This condition 
does not hold true always. Sometimes a situation may arise where we need to execute a 
certain part of the program. Also it may happen that we may want to execute the same part 
more than once. Control statements enable us to specify the order in which the various 
instructions in the program are to be executed. They define how the control is transferred 
to other parts of the program. Control statements are classified in the following ways:
Fig: 1 Classification of control statements
Page 2


CONTROL STATEMENTS
In C, programs are executed sequentially in the order of which they appear. This condition 
does not hold true always. Sometimes a situation may arise where we need to execute a 
certain part of the program. Also it may happen that we may want to execute the same part 
more than once. Control statements enable us to specify the order in which the various 
instructions in the program are to be executed. They define how the control is transferred 
to other parts of the program. Control statements are classified in the following ways:
Fig: 1 Classification of control statements
SELECTION STATEMENTS
The selection statements are also known as Branching or Decision Control Statements.
Introduction to Decision Control Statements
Sometime we come across situations where we have to make a decision. E.g. I f the 
weather is sunny, I will go out & play, else I will be at home. Here my course of action is 
governed by the kind of weather. If it’s sunny, I can go out & play, else I have to stay 
indoors. I choose an option out of 2 alternate options. Likewise, we can find ourselves in 
situations where we have to select among several alternatives. We have decision control 
statements to implement this logic in computer programming.
Decision making structures require that the programmer specify one or more conditions to 
be evaluated or tested by the program, along with a statement or statements to be executed 
if the condition is determined to be true, and optionally, other statements to be executed if 
the condition is determined to be false.
i f Statement
The keyword if tells the compiler that what follows is a decision control instruction. The if 
statement allows us to put some decision -making into our programs. The general form of 
the if statement is shown Fig 2:
Fig 2: if statement construct
Page 3


CONTROL STATEMENTS
In C, programs are executed sequentially in the order of which they appear. This condition 
does not hold true always. Sometimes a situation may arise where we need to execute a 
certain part of the program. Also it may happen that we may want to execute the same part 
more than once. Control statements enable us to specify the order in which the various 
instructions in the program are to be executed. They define how the control is transferred 
to other parts of the program. Control statements are classified in the following ways:
Fig: 1 Classification of control statements
SELECTION STATEMENTS
The selection statements are also known as Branching or Decision Control Statements.
Introduction to Decision Control Statements
Sometime we come across situations where we have to make a decision. E.g. I f the 
weather is sunny, I will go out & play, else I will be at home. Here my course of action is 
governed by the kind of weather. If it’s sunny, I can go out & play, else I have to stay 
indoors. I choose an option out of 2 alternate options. Likewise, we can find ourselves in 
situations where we have to select among several alternatives. We have decision control 
statements to implement this logic in computer programming.
Decision making structures require that the programmer specify one or more conditions to 
be evaluated or tested by the program, along with a statement or statements to be executed 
if the condition is determined to be true, and optionally, other statements to be executed if 
the condition is determined to be false.
i f Statement
The keyword if tells the compiler that what follows is a decision control instruction. The if 
statement allows us to put some decision -making into our programs. The general form of 
the if statement is shown Fig 2:
Fig 2: if statement construct
Syntax of if statement: 
if (condition )
{
Statement 1;
Statement n;
}
//Rest of the code
If the condition is true(nonzero), the statement will be executed. If the condition is 
false(0), the statement will not be executed. For example, suppose we are writing a billing 
program.
if (total_purchase >=1000)
printf(” You are gifted a pen drive. \n” );
Multiple statements may be grouped by putting them inside curly braces {}. For example:
if (total_purchase>=1000)
{
gift_count++;
printf(” You are gifted a pen drive. \n” );
}
For readability, the statements enclosed in {} are usually indented. This allows the 
programmer to quickly tell which statements are to be conditionally executed. As we will 
see later, mistakes in indentation can result in programs that are misleading and hard to 
read.
Programs:
1. Write a program to print a message if negative no is entered.
#include<stdio.h> 
int main()
{
int no;
printf(” Enter a no : ” );
Page 4


CONTROL STATEMENTS
In C, programs are executed sequentially in the order of which they appear. This condition 
does not hold true always. Sometimes a situation may arise where we need to execute a 
certain part of the program. Also it may happen that we may want to execute the same part 
more than once. Control statements enable us to specify the order in which the various 
instructions in the program are to be executed. They define how the control is transferred 
to other parts of the program. Control statements are classified in the following ways:
Fig: 1 Classification of control statements
SELECTION STATEMENTS
The selection statements are also known as Branching or Decision Control Statements.
Introduction to Decision Control Statements
Sometime we come across situations where we have to make a decision. E.g. I f the 
weather is sunny, I will go out & play, else I will be at home. Here my course of action is 
governed by the kind of weather. If it’s sunny, I can go out & play, else I have to stay 
indoors. I choose an option out of 2 alternate options. Likewise, we can find ourselves in 
situations where we have to select among several alternatives. We have decision control 
statements to implement this logic in computer programming.
Decision making structures require that the programmer specify one or more conditions to 
be evaluated or tested by the program, along with a statement or statements to be executed 
if the condition is determined to be true, and optionally, other statements to be executed if 
the condition is determined to be false.
i f Statement
The keyword if tells the compiler that what follows is a decision control instruction. The if 
statement allows us to put some decision -making into our programs. The general form of 
the if statement is shown Fig 2:
Fig 2: if statement construct
Syntax of if statement: 
if (condition )
{
Statement 1;
Statement n;
}
//Rest of the code
If the condition is true(nonzero), the statement will be executed. If the condition is 
false(0), the statement will not be executed. For example, suppose we are writing a billing 
program.
if (total_purchase >=1000)
printf(” You are gifted a pen drive. \n” );
Multiple statements may be grouped by putting them inside curly braces {}. For example:
if (total_purchase>=1000)
{
gift_count++;
printf(” You are gifted a pen drive. \n” );
}
For readability, the statements enclosed in {} are usually indented. This allows the 
programmer to quickly tell which statements are to be conditionally executed. As we will 
see later, mistakes in indentation can result in programs that are misleading and hard to 
read.
Programs:
1. Write a program to print a message if negative no is entered.
#include<stdio.h> 
int main()
{
int no;
printf(” Enter a no : ” );
scanf("%d", &no); 
if(no<0)
{
printf ("no entered is negative"); 
no = -no;
}
printf("value o f no is %d \n",no); 
return 0;
}
Output:
Enter a no: 6 
value o f no is 6
Output:
Enter a no: -2 
value o f no is 2
2. Write a program to perform division of 2 nos
#include<stdio.h> 
int main()
{
int a,b; 
float c;
printf("Enter 2 nos : "); 
scanf("%d %d", &a, &b);
if(b == 0)
{
printf("Division is not possible");
}
c = a/b;
printf("quotient is % f \n",c); 
return 0;
Page 5


CONTROL STATEMENTS
In C, programs are executed sequentially in the order of which they appear. This condition 
does not hold true always. Sometimes a situation may arise where we need to execute a 
certain part of the program. Also it may happen that we may want to execute the same part 
more than once. Control statements enable us to specify the order in which the various 
instructions in the program are to be executed. They define how the control is transferred 
to other parts of the program. Control statements are classified in the following ways:
Fig: 1 Classification of control statements
SELECTION STATEMENTS
The selection statements are also known as Branching or Decision Control Statements.
Introduction to Decision Control Statements
Sometime we come across situations where we have to make a decision. E.g. I f the 
weather is sunny, I will go out & play, else I will be at home. Here my course of action is 
governed by the kind of weather. If it’s sunny, I can go out & play, else I have to stay 
indoors. I choose an option out of 2 alternate options. Likewise, we can find ourselves in 
situations where we have to select among several alternatives. We have decision control 
statements to implement this logic in computer programming.
Decision making structures require that the programmer specify one or more conditions to 
be evaluated or tested by the program, along with a statement or statements to be executed 
if the condition is determined to be true, and optionally, other statements to be executed if 
the condition is determined to be false.
i f Statement
The keyword if tells the compiler that what follows is a decision control instruction. The if 
statement allows us to put some decision -making into our programs. The general form of 
the if statement is shown Fig 2:
Fig 2: if statement construct
Syntax of if statement: 
if (condition )
{
Statement 1;
Statement n;
}
//Rest of the code
If the condition is true(nonzero), the statement will be executed. If the condition is 
false(0), the statement will not be executed. For example, suppose we are writing a billing 
program.
if (total_purchase >=1000)
printf(” You are gifted a pen drive. \n” );
Multiple statements may be grouped by putting them inside curly braces {}. For example:
if (total_purchase>=1000)
{
gift_count++;
printf(” You are gifted a pen drive. \n” );
}
For readability, the statements enclosed in {} are usually indented. This allows the 
programmer to quickly tell which statements are to be conditionally executed. As we will 
see later, mistakes in indentation can result in programs that are misleading and hard to 
read.
Programs:
1. Write a program to print a message if negative no is entered.
#include<stdio.h> 
int main()
{
int no;
printf(” Enter a no : ” );
scanf("%d", &no); 
if(no<0)
{
printf ("no entered is negative"); 
no = -no;
}
printf("value o f no is %d \n",no); 
return 0;
}
Output:
Enter a no: 6 
value o f no is 6
Output:
Enter a no: -2 
value o f no is 2
2. Write a program to perform division of 2 nos
#include<stdio.h> 
int main()
{
int a,b; 
float c;
printf("Enter 2 nos : "); 
scanf("%d %d", &a, &b);
if(b == 0)
{
printf("Division is not possible");
}
c = a/b;
printf("quotient is % f \n",c); 
return 0;
Output:
Enter 2 nos: 6 2 
quotient is 3
Output:
Enter 2 nos: 6 0 
Division is not possible
if-else Statement
The if statement by itself will execute a single statement, or a group of statements, when 
the expression following if evaluates to true. By using else we execute another group of 
statements if the expression evaluates to false.
if (a > b)
{ z = a;
printf(‘‘ value o f z is :% d” ,z);
}
else
{ z = b;
printf(‘‘ value o f z is :% d” ,z);
}
The group of statements after the if is called an ‘if block’. Similarly, the statements after 
the else form the ‘else block’.
Programs:
3. Write a program to check whether the given no is even or odd
#include<stdio.h> 
int main()
{
int n;
printf("Enter an integer\n "); 
scanf("%d", &n); 
if ( n%2 == 0 )
printf("Even\n ");
else
Read More
90 docs

Top Courses for Computer Science Engineering (CSE)

Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Short Notes: Control Statements | Short Notes for Computer Science Engineering - Computer Science Engineering (CSE)

,

study material

,

MCQs

,

practice quizzes

,

Extra Questions

,

Sample Paper

,

pdf

,

ppt

,

Short Notes: Control Statements | Short Notes for Computer Science Engineering - Computer Science Engineering (CSE)

,

Important questions

,

video lectures

,

Exam

,

Free

,

past year papers

,

mock tests for examination

,

Objective type Questions

,

Summary

,

Short Notes: Control Statements | Short Notes for Computer Science Engineering - Computer Science Engineering (CSE)

,

Semester Notes

,

Previous Year Questions with Solutions

,

shortcuts and tricks

,

Viva Questions

;