Control Structures
Control Structures in 'C' : Language structures which are used to control execution sequence of a program are called control structures of that language.
C language supports following control structures :
1) If statement : If statement is used to control execution sequence of a program by checking particular condition.
Syntax :
if(condition)
{
-------
//statements to be executed if condition is true
-------
}
Note: If there is only one statement after if then curly brackets({}) can be omited(optional).
Example: Let's write C program to check number is even.
#include<stdio.h>
int main()
{
int num;
printf("Enter number:");
scanf("%d",&num);
if(num%2==0)
printf("Number is even");
return 0;
}
Output :
Enter Number: 24
Number is even
Note : % operator return remainder left after division of two numbers e.g. 10%3 will return 1 since after dividing 10 by 3 remainder obtained is 1.
2) If-else statement : It is also used to control execution sequence of a program based on condition. If given condition is true then statements followed by if block are executed where as if condition is false then statements followed by else block will be executed. Curly braces are optional if there is only one statement following a block.
Syntax :
if(condition)
{
-------
//statements to be executed if condition is true
-------
}
-------
//statements to be executed if condition is false
-------
}
C language supports following control structures :
1) If statement : If statement is used to control execution sequence of a program by checking particular condition.
Syntax :
if(condition)
{
-------
//statements to be executed if condition is true
-------
}
Note: If there is only one statement after if then curly brackets({}) can be omited(optional).
Example: Let's write C program to check number is even.
#include<stdio.h>
int main()
{
int num;
printf("Enter number:");
scanf("%d",&num);
if(num%2==0)
printf("Number is even");
return 0;
}
Output :
Enter Number: 24
Number is even
Note : % operator return remainder left after division of two numbers e.g. 10%3 will return 1 since after dividing 10 by 3 remainder obtained is 1.
2) If-else statement : It is also used to control execution sequence of a program based on condition. If given condition is true then statements followed by if block are executed where as if condition is false then statements followed by else block will be executed. Curly braces are optional if there is only one statement following a block.
Syntax :
if(condition)
{
-------
//statements to be executed if condition is true
-------
}
else
{-------
//statements to be executed if condition is false
-------
}
Example : Program to check whether number is even or odd.
#include<stdio.h>
int main()
{
int num;
printf("Enter number:");
scanf("%d",&num);
if(num%2==0)
printf("Number is EVEN");
else
printf("Number is ODD");
return 0;
}
Output :
Enter Number: 27
Number is ODD
3) Switch Case Statement : Switch-case is a multiway decision making statement. Switch variable is compared with case constant if match is found then corresponding block is executed. It makes use of break statement to come out of loop after executing matched case block. If variable does not matched with any of specified case constant then default block is executed. After completing switch program execution continues with statement followed by switch.
Syntax :
switch(variable)
{
case constant_1: Statements to execute;
break;
case constant_2: Statements toexecute;
break;
.
.
.
case constant_n: Statements to execute;
break;
default: Statements to execute;
}
Example : Accept single digit number from user and print it in word.
#include<stdio.h>
int main()
{
int num;
printf("Enter single digit number");
scanf("%d",&num);
switch(num)
{
case 0:printf("ZERO");
btreak;
case 1:printf("ONE");
break;
case 2:printf("TWO");
break;
case 3:printf("THREE");
break;
case 4:printf("FOUR");
break;
case 5:printf("FIVE");
break;
case 6:printf("SIX");
break;
case 7:printf("SEVEN");
break;
case 8:printf("EIGHT");
break;
case 9:printf("NINE");
break;
default: printf("Invalid input...");
}
return 0;
}
Output:
Enter single digit number5
FIVE
4) Conditional Operator: Conditional operator is a ternary operator in C. It is also called as short-hand if statement.
Syntax:
(Expression?Statement1:Statement2)
If expression evaluates to true value then Statement1 is executed otherwise Statement2 is executed.
Example : Program to check whether number is divisible by 5 or not.
#include<stdio.h>
int main()
{
int num=8,result;
result=(num%5==0?1:0);
if(result==1)
printf("Number is divisible by 5");
else
printf("Number is not divisible by 5");
return 0;
};
Output:
int main()
{
int num;
printf("Enter number:");
scanf("%d",&num);
if(num%2==0)
printf("Number is EVEN");
else
printf("Number is ODD");
return 0;
}
Output :
Enter Number: 27
Number is ODD
3) Switch Case Statement : Switch-case is a multiway decision making statement. Switch variable is compared with case constant if match is found then corresponding block is executed. It makes use of break statement to come out of loop after executing matched case block. If variable does not matched with any of specified case constant then default block is executed. After completing switch program execution continues with statement followed by switch.
Syntax :
switch(variable)
{
case constant_1: Statements to execute;
break;
case constant_2: Statements toexecute;
break;
.
.
.
case constant_n: Statements to execute;
break;
default: Statements to execute;
}
Example : Accept single digit number from user and print it in word.
#include<stdio.h>
int main()
{
int num;
printf("Enter single digit number");
scanf("%d",&num);
switch(num)
{
case 0:printf("ZERO");
btreak;
case 1:printf("ONE");
break;
case 2:printf("TWO");
break;
case 3:printf("THREE");
break;
case 4:printf("FOUR");
break;
case 5:printf("FIVE");
break;
case 6:printf("SIX");
break;
case 7:printf("SEVEN");
break;
case 8:printf("EIGHT");
break;
case 9:printf("NINE");
break;
default: printf("Invalid input...");
}
return 0;
}
Output:
Enter single digit number5
FIVE
4) Conditional Operator: Conditional operator is a ternary operator in C. It is also called as short-hand if statement.
Syntax:
(Expression?Statement1:Statement2)
If expression evaluates to true value then Statement1 is executed otherwise Statement2 is executed.
Example : Program to check whether number is divisible by 5 or not.
#include<stdio.h>
int main()
{
int num=8,result;
result=(num%5==0?1:0);
if(result==1)
printf("Number is divisible by 5");
else
printf("Number is not divisible by 5");
return 0;
};
Output:
Comments
Post a Comment
Please do not enter any spam link in message.