A program consists of many statements, which are to be executed based on some conditions. For example, a user wants to check whether a number is even or odd. Similarly, a user may check whether a year is a leap year or not? and many more.
To perform these tasks, we need to write a program and execute the statements based on some conditions. C includes two conditional statements, if-else, and switch.
Conditional Statement - if - else
To check, whether a number is even or odd, if the number is divided by 2 and the remainder is 0 (zero), the number is even. If the remainder is not 0, the number is odd. Programmatically we can verify this number using the following code.
#include <stdio.h>
int main (void)
{
int num;
printf("This a program to check even or odd number!\n");
printf("Enter an integer: ");
scanf("%d", &num);
if(num % 2 == 0)
{
printf("The number is even.");
}
else
{
printf("The number is odd.");
}
return 0;
}
In the above example, if the condition is true, the statement printf("The number is even."); is executed otherwise the statement printf("The number is odd."); is executed.
Based on the problem statement, sometimes if-else may be used within if or else or both. Writing if-else within another if-else is known as nesting. In the following example, we are going to check leap-year.
#include <stdio.h>
int main (void)
{
int year;
printf("This a program to check leap year!\n");
printf("Enter a year: ");
scanf("%d", &year);
if(year % 4 == 0)
{
if(year % 100 == 0)
{
if(year % 400 == 0)
{
printf("Leap Year!\n");
}
else
{
printf("Not Leap Year!\n");
}
}
else
{
printf("Leap Year!\n");
}
}
else
{
printf("Not Leap Year!\n");
}
return 0;
}
Another approach is to check leap year, where multiple conditions are checked together.
#include <stdio.h>
int main() {
int year;
printf("\n Enter a year :");
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
printf("\n The year is a leap year.");
else
printf("\n The year is not a leap year.");
return (0);
}
Conditional Statement - switch - case
C programming language has a built-in branch selection statement, which executes against a list of integer or character constants. These branches are known as cases. When a match is found to a case, the statements associated with that case are executed. Let us consider the following example of a basic calculator.
#include <stdio.h>
int main(void)
{
int x, y, z, choice;
printf("Calculator Program!\n");
printf("Enter value of x and y: ");
scanf("%d %d", &x, &y);
printf("Menu: 1 = add, 2 = sub, 3 = mul, 4 = div\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1: z = x + y;
break;
case 2: z = x - y;
break;
case 3: z = x * y;
break;
case 4: z = x / y;
break;
default:
printf("You have entered wrong choice!");
}
printf("The result is %d \n\n", z);
return 0;
}
In this program, first, we are taking two integer inputs for the variables x and y. Then we are taking the choice of the operation, which the user wants to perform. If the user enters 1 for the choice variable, case 1 will be executed. Similarly, other cases will be executed based on the value of choice.
Please look at the switch-case block very carefully. At the end of every case (except the last case), we are using a break statement. If we don't use a break statement, all the statements followed by the case will also be executed until there is a break statement. This is not expected at all. After executing statements of the matching case, the control should go to the end of the switch case. Hence we should use a break statement at the end of every case.
If none of these cases are matched, the statements of default are executed.
Ternary Operator
Ternary operator checks if a condition is true or false and executes the expressions accordingly. Note that a ternary operator can be applied if the number of statements in a block is only one. If a block contains more than one statement, you have to use if - else blocks. Let us try to compare the ternary operator and if-else block.
#include <stdio.h>
int main()
{
int score;
printf("Enter your score:");
scanf("%d", &score);
if (score >= 40)
{
printf("You are qualified!");
}
else
{
printf("You are not qualified!");
}
return 0;
}
#include <stdio.h>
int main()
{
int score;
printf("Enter your score:");
scanf("%d", &score);
(score >= 40) ? printf("You are qualified!") : printf("You are not qualified!");
return 0;
}
Let us try another example, where a value is being assigned to a variable.
#include <stdio.h>
int main()
{
int x, y, z;
printf("Enter an integer value for x:");
scanf("%d", &x);
printf("Enter an integer value for y:");
scanf("%d", &y);
if (x >= y)
{
z = x;
}
else
{
z = y;
}
printf("Larger value is %d", z);
return 0;
}
#include <stdio.h>
int main()
{
int x, y, z;
printf("Enter an integer value for x:");
scanf("%d", &x);
printf("Enter an integer value for y:");
scanf("%d", &y);
z = (x >= y) ? x : y;
printf("Larger value is %d", z);
return 0;
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.