C language – CONTROL STRUCTURES
“Challenges and problems are the fuel of success”
Let’s start today’s topic by this wonderful quote…………
Today we are going to learn some of the important control structures which is going to be very interesting…… Soo let’s start quickly……:)
*Decision Making structures:-
For decision making structures the programmer needs to 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 other statements to be executed if the condition is determined to be false.
1) 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.
•Syntax:-
if(expression)
{
//code to be executed
}
•Example:-
Q1) Program to check whether the number is positive or no.
#include<stdio.h>
int main()
{
int number;
printf("Enter a number:");
scanf("%d",&number);
if(number>0)
{
printf("%d is positive number", number);
}
}
2) If-else Statement:-
The if-else statement performs two operations for a single condition one for true and other for false.
The if-else statement is an extension to the if statement using which, we can
perform two different operations.
Using if-else statement is always preferable since it always invokes an otherwise case with every if condition.
•Syntax:-
if(expression)
{
//code to be executed if condition is true
}
else
{
//code to be executed if condition is false
}
•Example:-
Q1) C program to check whether the number is positive or negative.
#include<stdio.h>
int main()
{
int number;
printf("enter a number:");
scanf("%d",&number);
if(number>0)
{
printf("%d is positive number",number);
}
else
{
printf("%d is negative number",number);
}
}
3) If else-if ladder Statement:-
The if-else-if ladder statement is an extension to the if-else statement.
It is used 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)
{
//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
}
•Example:-
Q1) C program to print whether the number is greater than the other number.
#include<stdio.h>
main()
{
int m=40,n=20;
if (m>n)
{
printf("m is greater than n");
}
else if(m<n)
{
printf("m is less than n");
}
else
{
printf("m is equal to n");
}
}
4) Nested If Statement:-
In simple words nested if statement is a statement In which if statement is written inside another if statement.
We use nested if statement when you must test a combination of conditions before deciding on the proper action.
•Syntax:-
if( boolean_expression 1)
{
/* Executes when the boolean expression 1 is true */ if(boolean_expression 2)
{
/* Executes when the boolean expression 2 is true */
}
}
•Example:-
Q1) C program to check whether the number is equal to the given number.
#include<stdio.h>
int main ( )
{
int a = 10;
int b = 20;
if( a == 10 )
{
if( b == 20 )
{
printf("Value of a is 10 and b is 20\n" );
}
}
Now there is one more important topic that is conditional operator.
Let’s see what is exactly a conditional operator.
Hope you all will enjoy and learn this article…:)
The ? : Operator:-
Conditional operator ? : can be used to replace if…else statements.
It has the following general form −Exp1 ? Exp2 : Exp3;
Where Exp1, Exp2, and Exp3 are expressions.
•Syntax:-
The value of a ? expression is determined like this −
• Exp1 is evaluated If it is true, then Exp2 is evaluated and becomes the value of the entire ? expression.
• If Exp1 is false, then Exp3 is evaluated and its value becomes the value of the expression.
•Example:-
Q1) C program to check whether the number is greater than 0 or not.
#include<stdio.h>
main( )
{
int x,y ;
printf("enter value of x);
scanf("%d",&x);
y = ( x>0 ? 1 : 0 ) ;
printf("x is %d\n", x);
printf("y is %d", y);
}
Now we will see some examples of above statements.
Q1) A program to accept a character as input and check whether the character is a digit. (Check if it is in the range ‘0’ to ‘9’ both inclusive)
#include<stdio.h>
main()
{
char ch;
printf(“Enter the character”);
scanf(“%c”, &ch);
if(ch>=’0’ && ch<=’9’)
printf(“ Character is a digit”);
else
printf(“Character is not a digit”);
}
Q2) A program to accept a number and check whether it is divisible by 5 and 7.
#include<stdio.h>
main()
{
int no;
printf(“enter number”);
scanf(“%d”, &no);
if(no%5==0 && no%7==0)
printf(“ No is divisible by 5 and 7”);
else
printf(“No is not divisible by 5 and 7”);
}
Q3) A program to accept the x and y coordinate of a point and find the quadrant in which the point lies.
#include<stdio.h>
void main()
{
int x, y ;
printf("Input the values for X and Y coordinate : ");
scanf("%d %d", &x, &y);
if( x > 0 && y> 0)
printf("The coordinate point lies in the First quandrant.\n");
else if( x < 0 && y > 0)
printf("The coordinate point lies in the Second quandrant.\n");
else if( x < 0 && y < 0) printf("The coordinate lies in the Third quandrant.\n"); else if( x > 0 && y < 0)
printf("The coordinate point lies in the Fourth quandrant.\n");
else if( x == 0 && y == 0)
printf("The coordinate point lies at the origin.\n");
}
Q4) A program to accept the cost price and selling price from the keyboard. Find out if the seller has made a profit or loss and display how much profit or loss has been made.
#include<stdio.h>
main()
{
int cp, sp, profit, loss;
printf(“enter cost price and selling price”);
scanf(“%d %d”, &cp, &sp); if(sp>cp)
{
profit= sp-cp;
printf(“Profit is %d”, profit);
}
else
if(cp>sp)
{
loss= cp-sp;
printf(“Loss is %d”, loss);
}
}
Q5) Write a program to check whether given character is a digit or a character in lowercase or uppercase alphabet. (Hint ASCII value of digit is between 48 to 58 and Lowercase characters have ASCII values in the range of 97 to122, uppercase is between 65 and 90)
#include<stdio.h>
main()
{
char ch;
printf(“Enter the character”);
scanf(“%c”, &ch); if(ch>=65 && ch<=90) printf(“Character is uppercase”);
else
if(ch>=97 && ch<=122) printf(“Character is lower case”);
else
if(ch>=48 && ch<=57)
printf(“Character is a Digit”);
}
On is note will continue to see some more interesting topics in upcoming articles.
Thank you all for your support 🙂
If you have any query please feel free to ask……I will try my best to resolve your queries.
Will meet in next article… till then good bye. Have a good day ahead 🙂
You must log in to post a comment.