You are on page 1of 28

Control Statement in C

The statement which is used to control the flow of execution of the program these
statements are called control statement.

Types of Control Statement

Selection statement
Looping statement

Selection Statement
Selection statements are called conditional statements which executes based on
condition whether the condition is true or false.

If Statement
Switch Statement
Goto Statement

If Statement
If statement in C is used to perform the operations based on some specific condition.

There are various variant of if-statement in C language

Simple if-statement / if-statement


If-else statement
If-else-if ladder statement
Nested if statement

Simple if-statement
The simple if statement is used to check some given condition and perform some
operations depending upon the correctness of that condition. It executes if and only the
condition becomes true.
Practice problem
Syntax:
1. Positive number
2. Negative number
if(expression)
3. Equals to zero
4. Odd number
{
5. Even number
//code to be executed 6. Greatest number among 2 number
} 7. Greatest number among 3 number
8. Smallest number among 2 number
Flowchart of simple if-statement 9. Greatest number among 3 number
10. Person is eligible for voting

#include<stdio.h>
int main(){
int n;
printf("Enter a number:");
scanf("%d",&n);
if(n>0)
{
printf("%d is a positive number",n);
}
return 0;
}
Output
Enter a number: 5
5 is a positive number

Enter a number: -1

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 a if statement using which, we can perform two
different operations, i.e., one is for the true and the other is for false of the condition.

Here, we must notice that if and else block cannot be executed simultaneously. If block
will execute only if the condition becomes true and else block will execute only if the
condition becomes false.

Syntax:

if(expression)

{
Practice problem
//code to be executed if condition is true
 Positive number
}  Negative number
 Equals to zero
else
 Odd number
{  Even number
 Greatest number among 2 number
//code to be executed if condition is false  Greatest number among 3 number
 Smallest number among 2 number
}  Greatest number among 3 number
 Person is eligible for voting

 Prime number
Flowchart
 Composite number
 Leap year
 Check the Number is divible by 3
#include<stdio.h>
int main(){
int n;
printf("enter a number:");
scanf("%d",&n);
if(n>0){
printf("%d is positive number",n);
}
else{
printf("%d is negative number",n);
}
return 0;
}
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.

Syntax:

if(condition1)
{ Practice problem
//code to be executed if condition1 is true
 Check whether a given number is 50,60,80
}
 Print the grade of the student
else if(condition2)
 Largest among three & four number
{
 Smallest among three & four number
//code to be executed if condition2 is true
 Weekday based on given number
}
 Months based on given number
else if(condition3)
 Color of VIBGYOR based on given number
{
 Check a number is between 1-10, 10-20, 20-
//code to be executed if condition3 is true
30,30-40 or above
}
........ ........ …. …….. …… ……
………. ……. ……. ……. …… ….
else
{
//code to be executed if all the conditions are false
}

Flowchart of Ladder if-else


#include<stdio.h>
int main()
{
int n;
printf("enter a number:");
scanf("%d",&n);
if(n==10){
printf("number is equals to 10");
}
else if(n==50){
printf("number is equal to 50");
}
else if(n==100){
printf("number is equal to 100");
}
else{
printf("number is not equal to 10, 50 or 100");
}
return 0;
}
Nested if-Statements
One if-statement inside another if-statement is called nested if-statement.

When an if statement is present inside the body of another if then this is called nested
if-statement.

Syntax:

if(condition)
{
if(condition2)
{
//Statements inside the body of nested "if"
}
else
{
//Statements inside the body of nested "else"
}
}
else
{
//Statements inside the body of "else"
}

Flowchart of nested if-statement


#include <stdio.h>
int main()
{
int var1, var2;
printf("Input the value of var1:");
scanf("%d", &var1);
printf("Input the value of var2:");
scanf("%d",&var2);
if (var1 != var2)
{
printf("var1 is not equal to var2\n");
//Nested if else
if (var1 > var2)
{
printf("var1 is greater than var2\n");
}
else
{
printf("var2 is greater than var1\n");
}
}
else
{
printf("var1 is equal to var2\n");
}
return 0;
}

Output:

Input the value of var1:12


Input the value of var2:21
var1 is not equal to var2
var2 is greater than var1
Switch Statement
The switch statement in C is an alternate to if-else-if ladder statement which allows us to
execute multiple operations for the different possible values of a single variable called
switch variable.

Here, We can define various statements in the multiple cases for the different values of a
single variable.

Syntax:

switch(expression)
{
case value1: //code to be executed;
break;
case value2: //code to be executed;
break; //optional
...... …… …… …… …..
…… …….. ……. ……. …..
default : // code to be executed if all cases are not matched;
}
Flowchart Switch Case
#include<stdio.h>
int main()
{
int number;
printf("enter a number:");
scanf("%d",&number);
switch(number)
{
case 10:
printf("number is equals to 10");
break;
case 50:
printf("number is equal to 50");
break;
case 100:
printf("number is equal to 100");
break;
default:
printf("number is not equal to 10, 50 or 100");
}
return 0;
}

Rules for switch statement in C language


The switch expression must be of an integer or character type.
The case value must be an integer or character constant.
The case value can be used only inside the switch statement.
The break statement in switch case is not must. It is optional. If there is no break
statement found in the case, all the cases will be executed present after the
matched case. It is known as fall through the state of C switch statement.
#include <stdio.h>
int main() {
char operation;
double n1, n2;

printf("Enter an operator (+, -, *, /): ");


scanf("%c", &operation);
printf("Enter two operands: ");
scanf("%lf %lf",&n1, &n2);

switch(operation)
{
case '+':
printf("%.1lf + %.1lf = %.1lf",n1, n2, n1+n2);
break;

case '-':
printf("%.1lf - %.1lf = %.1lf",n1, n2, n1-n2);
break;

case '*':
printf("%.1lf * %.1lf = %.1lf",n1, n2, n1*n2);
break;

case '/':
printf("%.1lf / %.1lf = %.1lf",n1, n2, n1/n2);
break;

default:
printf("Error! operator is not correct");
}

return 0;
}
Output
Enter an operator (+, -, *, /): -
Enter two operands: 32.5
12.4
32.5 - 12.4 = 20.1
Looping/Iterative Statement
The looping can be defined as repeating the same process multiple times until a specific
condition satisfies.
Why we use Looping in C

The looping simplifies the complex problems into the easy ones. we can repeat the
same code for a finite number of times.

Eg:-

If we need to print the first 10 natural numbers then, instead of using the printf()
statement 10 times, we can print inside a loop which runs up to 10 iterations.

There are three types of loops used in the C language.


 Do While loop
 While Loop
 For Loop

Do While Loop
Do while loop is a looping Statement which executes a block of code repeatedly until
the condition becomes false.

It is also called a post tested loop it means condition will check after the statement. The
do-while loop is mainly used in the case where we need to execute the loop at least
once.

Syntax:

do
{
//code to be executed
} while (condition);
Flowchart

Program Exercise
#include<stdio.h>
int main ()  Print your name 100 times
 Print natural number upto 50
{
 Print natural number in reverse order upto 50
int i=1;
 Print the list of odd number upto 50
do  Print the list of even number upto 50
{  Print the list of number which is multiple of 3 upto 20
printf ("Bishal Patel");  Print the list of number which is divisible by 3 upto 20
i++;  Sum of natural number upto 5
 Sum of the even number upto 10
} while (i<=10);
 Sum of the odd number upto 10
return 0;
 Factorial of given number
}  Multiplication of table of given number
 Sum of digit of given number
 Product of digit of given number
 Reverse of given number
Series
 Check number is palindrome or not
1² + 2² + 3² + 4² + 5² +….+ n²  To check number is Armstrong or not
 Print list of Armstrong number upto 20
1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 +……+ 1/n
 Print alphabet from a-z
1 + 1/(2*2) + 1/(3*3) + ….. + 1/(n*n)  Fibonacci Series

2+7+12+17+………n
While Loop
While loop is also a looping Statement which executes a block of code repeatedly until
the condition becomes false.

It is also called a pre tested loop it means condition will check before the statement run.
It doesn’t execute the statement even at once without checking the condition. It only
execute it’s block of code if and only if the condition become true.

Syntax:

while (condition)
{
//code to be executed
}

Flowchart
#include<stdio.h>
int main ()
{
int i=1;
while (i<=10)
{
printf ("Bishal Patel");
i++;
}
return 0;
}

Practice the same program given above with while loop

for loop in C
For loop in C language is also a looping statement which iterate the statements or a part
of the program several times. It is frequently used to traverse the data structures like the
array and linked list.

Syntax:

for (initialization; condition; increment/decrement)


{
//code to be executed
}

for (Expression 1; Expression 2; Expression 3)


{
//code to be executed
}

Flowchart
#include<stdio.h>
int main ()
{
int i=0;
for (i=1; i<=10; i++)
{
printf ("Bishal Patel");
}
return 0;
}

Practice the same program given above with while loop


Properties of Expression 1
o The expression represents the initialization of the loop variable.
o We can initialize more than one variable in Expression 1.
o Expression 1 is optional.
o In C, we cannot declare the variables in Expression 1. However, It can be an
exception in some compilers.

#include<stdio.h>
int main()
{
int a, b, c;
for (a=0, b=12, c=23; a<2; a++)
{
printf ("%d ", a+b+c);
}
return 0;
}

Output
35 36

#include <stdio.h>
int main ()
{
int i=1;
for (; i<5; i++)
{
Printf ("%d ", i);
}
return 0;
}

Output
1234
Properties of Expression 2
o Expression 2 is a conditional expression. It checks for a specific condition to be
satisfied. If it is not, the loop is terminated.
o Expression 2 can have more than one condition. However, the loop will iterate
until the last condition becomes false. Other conditions will be treated as
statements.
o Expression 2 is optional.
o Expression 2 can perform the task of expression 1 and expression 3. That is, we
can initialize the variable as well as update the loop variable in expression 2 itself.
o We can pass zero or non-zero value in expression 2. However, in C, any non-zero
value is true, and zero is false by default.

#include <stdio.h>
int main ()
{
int i;
for (i=0; i<=4; i++)
{
Printf ("%d ", i);
}
return 0;
}

Output

01234

#include <stdio.h>
int main ()
{
int i, j, k;
for (i=0,j=0,k=0;i<4,k<8,j<10;i++)
{
printf ("%d %d %d\n" ,I, j, k);
j+=2;
k+=3;
}
return 0;
}

Output
000
123
246
369
4 8 12

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

Output
Infinite loop

Properties of Expression 3
o Expression 3 is used to update the loop variable.
o We can update more than one variable at the same time.
o Expression 3 is optional.
#include<stdio.h>
void main ()
{
int i=0, j=2;
for (i = 0; i<5; i++, j=j+2)
{
printf ("%d %d\n", I, j);
}
return 0;
}

Output
02
14
26
38
4 10

#include<stdio.h>
void main ()
{
for (;;)
{
printf ("Bishal Patel");
}
return 0;
}
Infinite loop

Nested Loops in C

One loop inside another loop is called nested loop. Any number of loops can be defined
inside another loop, i.e., there is no restriction for defining any number of loops.
Syntax:

Outer_loop
{
Inner_loop
Program
{  Multiplication of table
// inner loop statements.  Matrix problem
}  Table
// outer loop statements.
}

Exiting from a loop


Exiting from a loop means terminating a loop or come out from the loop.

In C language there are several ways to terminate the loop

 break keyword
 continue keyword
 goto keyword
 exit() function

break keyword
The break is a keyword in C which is used to bring the program control out of the loop.
The break statement is used inside loops or switch statement.

The break statement breaks the loop one by one, i.e., in the case of nested loops, it
breaks the inner loop first and then proceeds to outer loops.

Syntax:

break;

#include<stdio.h>
void main ()
{
int i;
for(i = 0; i<10; i++)
{
printf("%d ",i);
if(i == 5)
break;
}
printf("came outside of loop i = %d",i);

}
Output:
0 1 2 3 4 came outside of loop i=5

#include<stdio.h>
int main()
{
int i=1,j=1;//initializing a local variable
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
printf("%d %d\n",i,j);
if(i==2 && j==2)
{
break; //will break loop of j only
}
}
}
return 0;
}
Output
11
12
13
21
22
31
32
33

#include<stdio.h>
void main ()
{
int n=2, i, choice;
do
{
i=1;
while(i<=10)
{
printf("%d X %d = %d\n" ,n, i, n*i);
i++;
}
printf("do you want to continue with the table of %d , enter any non
zero value to continue.",n+1);
scanf("%d",&choice);

if(choice == 0)
{
break;
}
n++;
}while(1);
}

Output

table of 2 upto 10

do you want to continue with the table of 3 , enter any non-zero value to continue. 1

table of 3 upto 10
do you want to continue with the table of 3 , enter any non-zero value to continue. 0

Continue Keyword
The continue is a keyword in C language which is used to bring the program control to
the beginning of the loop.

The continue statement skips some lines of code inside the loop and continues with the
next iteration. It is mainly used for a condition so that we can skip some code for a
particular condition.

Syntax:

continue;

#include<stdio.h>
void main ()
{
int i = 0;
while(i!=10)
{
printf("%d", i);
continue;
i++;
}
}

Output
Infinite loop
#include<stdio.h>
int main()
{
int i=1;
for(i=1;i<=10;i++)
{
if(i==5)
{
continue;
}
printf("%d",i);
}
return 0;
}
Output
1 2 3 4 6 7 8 9 10

#include<stdio.h>
int main()
{
int i=1,j=1;
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
if(i==2 && j==2)
{
continue;
}
printf("%d %d\n",i,j);
}
}
return 0;
}
Output
11
12
13
21
23
31
32
33

C goto statement
The goto statement is known as jumping statement in C. As the name suggests, goto is
used to transfer the program control to a predefined label.

The goto statement can be used to repeat some part of the code for a particular
condition. It can also be used to break the multiple loops which can't be done by using
a single break statement.

However, using goto is avoided these days since it makes the program less readable and
complicated.

Syntax:

label:
//some part of the code;
goto label;

#include <stdio.h>
int main()
{
int num,i=1;
printf("Enter the number whose table you want to print?");
scanf("%d",&num);
table:
printf("%d x %d = %d\n",num,i,num*i);
i++;
if(i<=10)
goto table;
}
Output
Enter the table whose table you want to print? 5
Table of five upto 10

When should we use goto?


The only condition in which using goto is preferable is when we need to break the
multiple loops using a single statement at the same time.

#include <stdio.h>
int main()
{
int i, j, k;
for(i=0;i<10;i++)
{
for(j=0;j<5;j++)
{
for(k=0;k<3;k++)
{
printf("%d %d %d\n",i,j,k);
if(j == 2)
{
goto out;
}
}
}
}
out:
printf("came out of the loop");
}
Output
000
001
002
010
011
012
020
Came out of the loop

You might also like