Unit 2 (Control Statement)
Unit 2 (Control Statement)
Unit 2 (Control Statement)
→ Control Flow
→ Conditional Branching Statements: if, if-else, if-else-if, switch Statements.
→ Basic Loop Structures: while, for loop, do-while loops, nested loops.
→ Unconditional Statements: break, continue and goto Statements.
Control Flow:
C language supports:
if-else-if Statement
if Statement (else-if ladder) switch Statement
Looping / Iteration
while Loop
continue
break goto
Conditional statements in the programming are used to control the flow of execution
in the program based on certain conditions (true or false).
1. Simple if Statement:
This is a unidirectional conditional control statement.
A set of statements is executed if the condition is true. If the condition is false,
the code inside the if block is skipped.
Syntax:
if(condition)
{
Statement inside if block;
}
Statement outside if block;
false
condition
true
Statement
outside if block
Statement inside
if block
Example1:
#include<stdio.h>
int main()
{
int a, b;
printf("Enter a and b: ");
scanf("%d %d", &a, &b);
if(a==b)
{
printf("a is equal to b\n");
}
printf("Understanding the concept of if statement...");
return 0;
}
Example2:
#include<stdio.h>
int main()
{
int a, b;
printf("Enter a and b: ");
scanf("%d %d", &a, &b);
if(a==b)
{
printf("a is equal to b\n");
}
if(a!=b)
{
printf("a is not equal to b\n");
}
if(a>=b)
{
printf("a is greater than or equal to b\n");
}
printf("This is a simple if program...");
return 0;
}
2. if-else Statement:
This is a bi-directional conditional control statement.
This statement is used to test a condition and take only one out of the two possible
results.
If the condition is true, then a statement inside if block is executed.
If the condition is false, then a statement inside else block is executed.
Syntax:
if(condition)
{
Statement inside if block; //true block
}
else
{
Statement inside else block; //false block
}
true false
condition
Statement outside
if-else block
true false
condition1
true false
statement2
condition3
statement3 statement4
Example1: Program to find largest number from three given numbers.
Code:
#include<stdio.h>
int main()
{
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
if(a>b && a>c)
{
printf("%d is greater", a);
}
else if(b>a && b>c)
{
printf("%d is greater", b);
}
else
{
printf("%d is greater", c);
}
return 0;
}
Example2: program to find out the grade of a student when the marks of 4
subjects are given. The method of assigning grade is as follows:
per>=85 grade=A
per>=70 and per<85 grade=B
per>=55 and per<70 grade=C
per>=40 and per<55 grade=D
per<40 grade=E
Code:
#include<stdio.h>
int main()
{
float m1,m2,m3,m4,total,per;
char grade;
printf("Enter marks of 4 subjects: ");
scanf("%f %f %f %f", &m1, &m2, &m3, &m4);
total = m1+m2+m3+m4;
per=total/4.0;
if(per>=85)
{
grade='A';
}
else if(per>=70 && per<85)
{
grade='B';
}
else if(per>=55 && per<70)
{
grade='C';
}
else if(per>=40 && per<55)
{
grade='D';
}
else
{
grade='E';
}
printf("Percentage is %.2f\nGrade is %c\n", per,grade);
return 0;
}
Or
Code:
#include<stdio.h>
int main()
{
float m1,m2,m3,m4,total,per;
char grade;
printf("Enter marks of 4 subjects: ");
scanf("%f %f %f %f", &m1, &m2, &m3, &m4);
total = m1+m2+m3+m4;
per=total/4.0;
if(per>=85)
{
grade='A';
}
else if(per>=70)
{
grade='B';
}
else if(per>=55)
{
grade='C';
}
else if(per>=40)
{
grade='D';
}
else
{
grade='E';
}
printf("Percentage is %.2f\nGrade is %c\n", per,grade);
return 0;
}
4. nested if-else:
Writing if-else statement inside another if statement and else statement is called
nested if-else statement.
Syntax:
if(condition1)
{
if(condition2)
{
statement1;
}
else
{
statement2;
}
}
else
{
if(condition2)
{
statement3;
}
else
{
statement4;
}
}
The switch statement contains one or more cases and each case has a constant
value associated with it.
At first switch statement compares the first case constant value with the switch
value, if it gets matched the statements executed from the first case.
If it doesn't match the switch statement compares the second case constant value
with the switch value and if it is matched the statements executed from the second
case.
This process continues until it finds a match. If no case constant value matches
with the switch value specified in the switch statement, then a special case
called default is executed.
Note:
switch block expression can be any integer or character constant also.
The constants following the case keywords should be of integer or character type.
These constants must be different from one another.
We can't use floating-point or string constants.
Multiple constants in a single case are not allowed; each case should be followed
by only one constant.
Writing a switch statement inside another is called nesting of switches.
Example: Program to perform arithmetic calculations on float.
Code:
#include<stdio.h>
int main()
{
float num1, num2;
char op;
printf("Enter num1 & num2: ");
scanf("%f %f", &num1, &num2);
printf("Enter an operator (+, -, *, /): ");
scanf(" %c", &op);
switch(op)
{
case '+':
printf("%.1f + %.1f = %.2f", num1, num2, num1 + num2);
break;
case '-':
printf("%.1f - %.1f = %.2f", num1, num2, num1 - num2);
break;
case '*':
printf("%.1f * %.1f = %.2f", num1, num2, num1 * num2);
break;
case '/':
printf("%.1f / %.1f = %.2f", num1, num2, num1 / num2);
break;
default:
printf("Error! Operator is not correct.");
break;
}
return 0;
}
Or
Code:
#include<stdio.h>
int main()
{
float num1, num2;
char op;
printf("Enter num1 & num2: ");
scanf("%f %f", &num1, &num2);
printf("Enter an operator (+, -, *, /): ");
scanf(" %c", &op);
switch(op)
{
case '+':
printf("result of addition= %.2f", num1 + num2);
break;
case '-':
printf("result of subtraction= %.2f", num1 - num2);
break;
case '*':
printf("result of multiplication= %.2f", num1 * num2);
break;
case '/':
printf("result of division= %.2f", num1 / num2);
break;
default:
printf("Error! Operator is not correct.");
break;
}
return 0;
}
Looping / Iteration:
Types of Loops:
Entry Controlled Loops:
In entry-controlled loops the test condition is checked before entering the main
body of the loop.
1. while Loop:
The while statement is used to execute a single statement or block of statements
repeatedly as long as the given condition is true.
Syntax:
variable initialization;
while(condition)
{
block of statements;
variable update;
}
false
condition
true
Example1:
#include<stdio.h>
int main()
{
int i=1, n;
scanf("%d", &n);
while(i<=n)
{
printf("i=%d\n",i);
i++;
}
return 0;
}
Example2:
#include<stdio.h>
int main()
{
int i=1, n;
scanf("%d", &n);
while(i<=n)
{
i++;
}
printf("i=%d\n", i);
return 0;
}
Example3:
#include<stdio.h>
int main()
{
int i=1;
while(i<=10)
{
printf("%d\t", i);
i++;
}
return 0;
}
2. for Loop:
The for statement is used to execute a single statement or block of statements
repeatedly as long as the given condition is true.
Syntax:
for(variable initialization; condition; variable update)
{
block of statements;
}
statements outside the loop
Flowchart to understand for loop:
variable
initialization
false
condition
true
block of statements
statements outside
variable update
the loop
Example1:
#include<stdio.h>
int main()
{
int i;
for(i=1; i<=10; i++)
{
printf("%d\t", i);
}
return 0;
}
Example2:
#include<stdio.h>
int main()
{
int n;
for(n=0; n<=10; n=n+2)
{
printf("%d\t", n);
}
return 0;
}
Or
#include<stdio.h>
int main()
{
int n;
for(n=0; n<=10; n++)
{
if(n%2==0)
{
printf("%d\t", n);
}
}
return 0;
}
Example3:
#include<stdio.h>
int main()
{
int n;
for(n=1; n<=10; n=n+2)
{
printf("%d\t", n);
}
return 0;
}
Or
#include<stdio.h>
int main()
{
int n;
for(n=0; n<=10; n++)
{
if(n%2!=0)
{
printf("%d\t", n);
}
}
return 0;
}
Exit Controlled Loops:
In exit-controlled loops the test condition is checked after entering the main body
of the loop. The loop body will be executed at least once, whether the condition
is true or false.
3. do-while Loop:
The do-while loop that executes the body of loop at least once and checks the
condition at the end. This process is repeated till the condition is true.
Syntax:
do
{
body of loop;
}while(condition);
statement out of loop;
variable initialization
false
do - block of
statements
variable update
statements outside
condition
the loop
true
Example1:
#include<stdio.h>
int main()
{
int n=1;
do
{
printf("%d\t", n);
n++;
}while(n<=10);
return 0;
}
Example2:
#include<stdio.h>
int main()
{
int n=0;
do
{
printf("%d\t", n);
n+=2;
}while(n<=10);
return 0;
}
Or
#include<stdio.h>
int main()
{
int n=0;
do
{
if(n%2==0)
{
printf("%d\t", n);
}
n++;
}while(n<=10);
return 0;
}
Example3:
#include<stdio.h>
int main()
{
int n=1;
do
{
printf("%d\t", n);
n+=2;
}while(n<=10);
return 0;
}
Or
#include<stdio.h>
int main()
{
int n=0;
do
{
if(n%2!=0)
{
printf("%d\t", n);
}
n++;
}while(n<=10);
return 0;
}
Nested Loops:
When a loop is written inside the body of another loop, then it is known as
nesting of loops. Any type of loop can be nested inside any other type of loop.
For example, a 'for' loop may be nested inside another 'for' loop or inside a
'while' or 'do-while' loop.
Syntax:
outer_loop
{
inner_loop
{
// Inner loop statement/s
}
// Outer loop statement/s
}
Example1:
#include<stdio.h>
int main()
{
int i, j, rows;
printf("Enter how many rows you want: ");
scanf("%d", &rows);
{
for(i=1; i<=rows; i++)
{
for(j=1; j<=rows; j++)
{
printf("* ");
}
printf("\n");
}
}
return 0;
}
Output:
Enter how many rows you want: 5
*****
*****
*****
*****
*****
Example2:
#include<stdio.h>
int main()
{
int i, j, rows;
printf("Enter how many rows you want: ");
scanf("%d", &rows);
{
for(i=1; i<=rows; i++)
{
for(j=1; j<=i; j++)
{
printf("* ");
}
printf("\n");
}
}
return 0;
}
Output:
Enter how many rows you want: 5
*
**
***
****
*****
Example3:
#include<stdio.h>
int main()
{
int i, j, rows;
printf("Enter how many rows you want: ");
scanf("%d", &rows);
{
for(i=1; i<=rows; i++)
{
for(j=1; j<=i; j++)
{
printf("%d ", j);
}
printf("\n");
}
}
return 0;
}
Output:
Enter how many rows you want: 5
1
12
123
1234
12345
Infinite Loops:
The loop condition never becomes false and the body of loop is executed
repeatedly and never terminate is known as infinite loop.
Let us take some examples and see what types of mistakes lead to infinite loops.
Example1:
#include<stdio.h>
int main()
{
int i;
for(i=0; i<=5; i--)
{
printf("%d", i);
}
return 0;
}
Note: This loop will execute till the value of 'i' is less than or equal to 5, i.e., the loop
will terminate only when 'i' becomes greater than 5. The initial value of 'i' is 0, and after
each iteration, its value is decreasing, hence it will never become greater than 5. So, the
loop condition will never become false, and the loop will go on executing infinitely.
Example2:
#include<stdio.h>
int main()
{
int k=1, sum;
do
{
printf("%d",k);
sum=sum+k;
}while(k<5);
return 0;
}
Note: Here, we are not changing the value of 'k' inside the loop body, and hence the
loop becomes infinite.
Example3:
#include<stdio.h>
int main()
{
int n;
while(n=2)
{
printf("%d",n);
}
return 0;
}
Note: This loop will produce output 2 repeatedly and will go on executing infinitely.
Example4:
#include<stdio.h>
int main()
{
int i=1;
while(i<=5);
{
printf("%d",i++);
}
return 0;
}
Note: This loop will produce no output and will go on executing infinitely. The mistake
here is that we have put a semicolon after the condition.
The above are the some of the examples where infinite loops occur due to mistakes.
Below examples are the infinite loops that are intentionally used in programs.
Example1:
#include<stdio.h>
int main()
{
while(1)
{
printf("This is an infinite loop...\n");
}
return 0;
}
Example2:
#include<stdio.h>
int main()
{
do
{
printf("This is an infinite loop...\n");
}while(1);
return 0;
}
Example3:
#include<stdio.h>
int main()
{
for( ; ; )
{
printf("This is an infinite loop...\n");
}
return 0;
}
1. break statement:
In C, the break statement is used to perform the following two things.
break statement is used to terminate the switch case statement.
break statement is also used to terminate looping statements like while, do-while
and for.
Note: Sometimes it becomes necessary to come out of the loop even before the loop
condition becomes false. In such a situation, the break statement is used to terminate the
loop.
Example1:
#include<stdio.h>
int main()
{
int n;
for(n=1; n<=5; n++)
{
if(n==3)
{
printf("I understand the use of break\n");
break;
}
printf("n=%d\n", n) ;
}
return 0;
}
Example2:
#include<stdio.h>
int main()
{
int i;
for(i=1; i<50; i++)
{
if(i%2==0)
{
break;
}
printf("i= %d\n", i);
}
return 0;
}
2. continue statement:
When a continue statement is encountered inside a loop, control jumps to the
beginning of the loop for the next iteration, skipping the execution of statements
inside the body of the loop for the current iteration.
Example1:
#include<stdio.h>
int main()
{
int n;
for(n=1; n<=5; n++)
{
if(n==3)
{
printf("I understand the use of continue\n");
continue;
}
printf("n= %d\n", n) ;
}
return 0;
}
3. goto statement:
The goto statement is used to jump from one line to another line in the program.
Using goto statement we can jump from top to bottom or bottom to top.
The goto statement can be used as:
forward goto statement: If the label is after the goto, then the control is
transferred forward, and it is known as a forward jump or forward goto.
goto label;
....
....
label:
statement;
....
....
backward goto statement: If the label is before the goto, then the control is
transferred backward, and it is known as a backward jump or backward goto.
label:
....
....
goto label;
....
....
Here label is any valid C identifier and it is followed by a colon (:).
Example1:
#include<stdio.h>
int main()
{
int num;
printf("Enter Number: ");
scanf("%d", &num);
if(num%2==0)
{
goto even;
}
else
{
goto odd;
}
even:
printf("%d is an even number...\n", num);
goto end;
odd:
printf("%d is an odd number...\n", num);
goto end;
end:
printf("I understand how to use goto statement...\n");
return 0;
}
Practice program from unit-2:
Program1: Program to print the larger and smaller of the two numbers.
Code:
#include<stdio.h>
int main()
{
int a,b;
printf("Enter a & b: ");
scanf("%d %d", &a, &b);
if(a>b)
printf("larger=%d\t\tgreater=%d\n", a,b);
else
printf("larger=%d\t\tgreater=%d\n", b,a);
return 0;
}
Or
#include<stdio.h>
int main()
{
char ch;
printf("Enter character: ");
scanf("%c", &ch);
switch(ch)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
printf("%c is vowel...\n", ch);
break;
default:
printf("%c is consonant...\n", ch);
break;
}
return 0;
}
Program5: Write a C program to input week number and print week day.
Code:
#include<stdio.h>
int main()
{
int day_of_week;
printf("Enter value to print day of week: ");
scanf("%d", &day_of_week);
switch(day_of_week)
{
case 1:
printf("---Monday---\n");
break;
case 2:
printf("---Tuesday---\n");
break;
case 3:
printf("---Wednesday---\n");
break;
case 4:
printf("---Thurday---\n");
break;
case 5:
printf("---Friday---\n");
break;
case 6:
printf("---Saturday---\n");
break;
case 7:
printf("---Sunday---\n");
break;
default:
printf("Entered an invalid value to print day of week...\n");
break;
}
return 0;
}
Or
#include<stdio.h>
int main()
{
int i;
for(i=1; i<20; i++)
{
if(i%2!=0)
{
printf("%d ", i);
}
}
return 0;
}
return 0;
}
Or
#include<stdio.h>
int main()
{
int i;
for(i=10; i>1; i--)
{
if(i%2==0)
{
printf("%d ", i);
}
}
return 0;
}
Program11: Write a C program to print reverse of a given number.
Code:
#include<stdio.h>
int main()
{
int num, rem, rev=0;
printf("Enter Number: ");
scanf("%d", &num);
int o_num = num;
while(num!=0)
{
rem=num%10;
rev=rev*10+rem;
num=num/10;
}
printf("The reverse of given number %d is %d", o_num, rev);
return 0;
}
Program14: Write a C program to print Hello world for 10 times, but skip
printing if the number of iterations is multiple of 3.
Code:
#include<stdio.h>
int main()
{
int i;
for(i=1; i<=10; i++)
{
if(i%3==0)
{
continue;
}
printf("Hello world\n");
}
return 0;
}
Program15: Write a C program to display odd numbers from 1 to 100, stop the
process if the current number is divisible by 77.
Code:
#include<stdio.h>
int main()
{
int n;
for(n=1; n<=100; n+=2)
{
if(n%77==0)
{
break;
}
printf("%d ", n);
}
return 0;
}
Or
#include<stdio.h>
int main()
{
int num, i, flag=0;
printf("Enter Number: ");
scanf("%d", &num);
if(num==0 || num==1)
{
flag=1;
}
for(i=2; i<=num/2; i++)
{
if(num%i==0)
{
flag=1;
break;
}
}
if(flag==0)
{
printf("%d is Prime Number", num);
}
else
{
printf("%d is not Prime Number", num);
}
return 0;
}