we have learnt regarding the essential use of call management instruction victimisation if statement. nowadays we are going to extend that tutorial with some advanced examples.
A bit additional regarding conditions
As I told you earlier that if the statement is employed with some conditions. actually speaking it can even be used with some expression.
So the general kind of if statement is
if(expression)
one
Statement two
. . . . .
. . . . .
}
What is expression?
This expression will be any valid expression together with relative and arithmetic expression. Some common examples are given below.
if(3+2)
printf(“This can print everytime”);
if(6/2+4)
printf(“This can print everytime”);
if(0)
printf(“This can ne’er execute”);
Conclusion
Truly speaking in C language any non-zero range is taken into account to be TRUE whereas zero is considered as FALSE.
During the running of call management instruction, a compiler checks the expression either it evaluates zero or not. If the result’s zero then it’ll ne’er execute the body of if statement. however if the expression value to any non-zero range then it’ll execute the body of if statement.
In the initial 2 examples, expression beneath if evaluates a non-zero price. thus it’ll print the message.
In the last example, I actually have written zero. that the printf() statement isn’t dead.
Note: Usage of expression is incredibly vital in C language. whereas writing massive programs it’s used terribly ofttimes. thus i like to recommend you to know the essential logic behind it properly. If you have got any issues then you’ll be able to conjointly post your queries in comments.
Now let’s take one slightly sophisticated program to know all the fundamentals related to if statement.
Sample Program
Make one program which can show the entire quantity once deducting the discount of 100%, if the entire is quite 2000.
/*Program to show discount*/
#include <stdio.h>
void main()
{
float amnt,famnt,dis;
printf("Enter the total amountn");
scanf("%f",&amnt);
famnt=amnt;
if (amnt>2000)
{
printf("You are eligible for discountn");
dis=0.1*amnt;
famnt=famnt-dis;
}
printf("final amount is %f",famnt);
}
Output
enter the total amount 2100
you are eligible for discount
final amount is 1890.000000
Now let’s try and perceive this program
I hope you need to perceive the essential initial steps of this program. i’ll provide the reason for less than if statement.
In if statement I actually have given the condition of (amnt>2000), it means that it’ll check the number if it’s larger than 2000. Say, if it’s larger than the number then it’ll execute the statements within the body of if. Otherwise, it’ll skip it.
In our check run, we’ve given the worth 2100, because it is larger than 2000. that the management passes within the if statement body and do the following things.
– initial it’ll show the message “You are eligible for discount”
– within the next step, it’ll calculate the discount i.e. 100% of the entire quantity.
– within the final step it’ll deduct the discount from the ultimate quantity.
currently within the last step, it’ll show the ultimate quantity.