Hello friends, in this tutorial we will discuss the if ...else and Nested if statement. In a previous post, I have already discussed the simple if statement used to test a condition, if it is true then the code inside the if statement is executed otherwise the code is not executed.
if...else statement
The if...else statement having the ability to make a decision among alternative paths means we can execute one group of statements if the condition is true and another group of statements if the condition is false.
Syntax
1. if (logical condition)
program statement 1; // if the condition is true
else
program statement 2; // if the condition is false
2. if (logical condition)
{ group of statements; //if the condition is true }
else
{ group of statements; // if the condition is false }
Examples
1. //program to determine whether the person is eligible for driving or not
int age =25 ;
if (age > 18)
printf("\n The person is eligible for driving"); //true
else
printf("\n The person is not eligible for driving"); //false
In this example, age = 25 so the condition is true and that's why we will get the result " The person is eligible for driving".
2. // program to determine if you entered 5 or any other number.
int i;
printf ("Enter any number");
scanf ( "%d", &i);
if (i==5)
printf("You entered 5");
else
printf(You entered something other than 5")'
In this case, if i =5 then only you will get the result " You entered 5" and if i = 45 then you will get the result "You entered something other than 5".
3. //Program to determine if a number is either even or odd
main ()
{
int number, remainder;
printf ("Enter your number to be tested:");
scanf("%d", &number);
remainder = number %2;
if (remainder ==0)
printf(" The number is even. \n");
else
printf("The number is odd. \n");
}
output 1
Enter your number to be tested: 56
the number is even
output 2
Enter your number to be tested: 59
the number is odd
Here in the above example, we have to determine whether the given no is either even or odd by using the mod (%) function. the mod function stores the remainder value.
Nested if statement
The if..else statement constructs within either the body of the if statement or the body of the else statement. this means if the condition in the first if statement is false, then the condition in the second if statement is checked. if it is false as well the final statement is executed.
Syntax
if (condition)
//code or group of statements
else if (condition)
//code or group of statements
else if (condition)
// code or group of statements
else
//code or group of statements
Example
1. Program to evaluate simple arithmetic expressions of the form number operator number form
main()
{
float number1, number2;
char operator;
printf("Give arithmetic values for arithmetic expressions like "23 + 34" \n)'
scanf("%f %c%f", &number1, &operator, &number2);
if (operator == '+')
printf("%f", number1 + number2);
else if (operator == '-')
printf("%f", number1 - number2);
else if (operator == '*')
printf("%f", number1 * number2);
else (operator == '/')
printf("%f", number1 / number2);
}
Output
Give arithmetic values for arithmetic expressions like "23 + 34"
123 + 59
182
Output (Re-run)
Give arithmetic values for arithmetic expressions like "23 + 34"
198.7 /26
7.64
Output (Re-run)
Give arithmetic values for arithmetic expressions like "23 + 34"
89.3 *2.5
223.25
Here we are getting different arithmetic functions to result according to their input values.
Comments
Post a Comment
Your comment will inspire me, Please leave your comment