Unit 2 Notes Autonomous
Unit 2 Notes Autonomous
Academic Year:2023-24
Unit – II
Control Statements:Selection- simple if, if-else, else-if ladder, nested if-else and switch-case
statements.Iteration with - while, do-while, for statements. Unconditional statements: break,
continue, goto andreturn statements
Control Statements:
In C programming, a control statement is a statement that alters the flow of execution of a program.
We use control statements to determine the order in which the instructions within a program are
executed. They allow us to make decisions, repeat actions, and control the flow of our program
based on certain conditions.
o If statement
o If-else statement
o If else-if ladder
o Nested if
If Statement
The if statement is used to check some given condition and perform some operations depending
upon the correctness of that condition. It is mostly used in the scenario where we need to perform
the different operations for the different conditions. The syntax of the if statement is given below.
1. if(expression){
2. //code to be executed
3. }
Flowchart of if statement in C
Example program
#include <stdio.h>
int main() {
if (25 > 17) {
printf("25 is greater than 17");
}
return 0;
}
#include<stdio.h>
int main(){
int number=0;
printf("Enter a number:");
scanf("%d",&number);
if(number%2==0){
printf("%d is even number",number);
return 0;
}
Program to find the largest number of the three.
#include<stdio.h>
int main()
{
int a, b, c;
printf("Enter three numbers?");
scanf("%d %d %d",&a,&b,&c);
if(a>b && a>c)
{
printf("%d is largest",a);
}
if(b>a && b > c)
{
printf("%d is largest",b);
}
if(c>a && c>b)
{
printf("%d is largest",c);
}
if(a == b && a == c)
{
printf("All are equal");
}
}
If-else Statement
The if-else statement is used to perform two operations for a single condition. The if-else statement
is an extension to the if statement using which, we can perform two different operations, i.e., one is
for the correctness of that condition, and the other is for the incorrectness of the condition. Here,
we must notice that if and else block cannot be executed simiulteneously. Using if-else statement
is always preferable since it always invokes an otherwise case with every if condition. The syntax of
the if-else statement is given below.
if(expression)
{
//code to be executed if condition is true
}
Else
{
//code to be executed if condition is false
}
Flowchart of the if-else statement in C
Let's see the simple example to check whether a number is even or odd using if-else statement in
C language.
#include<stdio.h>
int main(){
int number=0;
printf("enter a number:");
scanf("%d",&number);
if(number%2==0){
printf("%d is even number",number);
}
else{
printf("%d is odd number",number);
}
return 0;
}
Program to check whether a person is eligible to vote or not.
#include <stdio.h>
int main()
{
int age;
printf("Enter your age?");
scanf("%d",&age);
if(age>=18)
{
printf("You are eligible to vote...");
}
else
{
printf("Sorry ... you can't vote");
}
}
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)
{
//code to be executed if condition1 is true
}
else if(condition2)
{
//code to be executed if condition2 is true
}
else if(condition3)
{
//code to be executed if condition3 is true
}
...
Else
{
//code to be executed if all the conditions are false
}
#include <stdio.h>
int main()
{
int marks;
printf("Enter your marks?");
scanf("%d",&marks);
if(marks > 85 && marks <= 100)
{
printf("Congrats ! you scored grade A ...");
}
else if (marks > 60 && marks <= 85)
{
printf("You scored grade B + ...");
}
else if (marks > 40 && marks <= 60)
{
printf("You scored grade B ...");
}
else if (marks > 30 && marks <= 40)
{
printf("You scored grade C ...");
}
else
{
printf("Sorry you are fail ...");
}
}
Nested If
A nested if in C is an if statement that is the target of another if statement. Nested if statements
mean an if statement inside another if statement. Yes, both C and C++ allow us to nested if
statements within if statements, i.e, we can place an if statement inside another if statement.
if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
else
{
// Executes when condition2 is false
}
int main()
{
int i = 20;
if (i == 10)
printf("i is 10");
else if (i == 15)
printf("i is 15");
else if (i == 20)
printf("i is 20");
else
printf("i is not present");
}
#include <stdio.h>
int main() {
if(number1 == number2) {
printf("Result: %d = %d",number1,number2);
else {
return 0;
}
<100 1.50
100-200 2.00
200-300 2.50
>300 3.00
/* program to implement switch */
#include<stdio.h>
main()
int marks,index;
char grade[10];
scanf(“%d”,&marks);
index=marks/10;
switch(index)
case 10 :
case 9:
case 8:
case 7:
case 6: grade=”first”;
break;
case 5 : grade=”second”;
break;
case 4 : grade=”third”;
break;
break;
printf(“%s”,grade);
}
Iterative/Repetitive Statements(Loops)
Example Program | Program to display even numbers upto 10.
#include<stdio.h>
#include<conio.h>
void main(){
int n = 0;
clrscr() ;
printf("Even numbers upto 10\n");
while( n <= 10 )
{
if( n%2 == 0)
printf("%d\t", n) ;
n++ ;
}
getch() ;
}
#include<stdio.h>
Void main()
Int i=1;
While(i<=10)
Printf(“%d”,i);
I++;
Output:
1 2 3 4 5 6 7 8 9 10
/* C program to print multiplication table */
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= 10; ++i) {
return 0;
Enter an integer: 9
9*1=9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
9 * 10 = 90