C if Statement – Part 2

In the last tutorial we have learnt about the basic use of decision control instruction using if statement. Today we will extend that tutorial with some advanced examples.

A bit more about conditions

As I told you earlier that if statement is used with some conditions. Truly speaking it can also be used with some expression.
So the general form of if statement is

if(expression)
{
Statement 1
Statement 2
. . . . .
. . . . .
}

What is expression?

This expression can be any valid expression including relational and arithmetic expression. Some common examples are given below.

if(3+2)
printf(“This will print everytime”);

if(6/2+4)
printf(“This will print everytime”);

if(0)
printf(“This will never execute”);

Conclusion

Truly speaking in C language any non-zero number is considered to be TRUE whereas zero is considered as FALSE.

During the running of decision control instruction, a compiler checks the expression either it evaluates 0 or not. If the result is 0 then it will never execute the body of if statement. But if the expression evaluate to any non-zero number then it will execute the body of if statement.

In the first two examples, expression under if evaluates a non-zero value. So it will print the message.

In the last example, I have written 0. So the printf() statement is not executed. 

Note: Usage of expression is very important in C language. While writing big programs it is used very frequently. So I recommend you to understand the basic logic behind it correctly. If you have any problems then you can also post your queries in comments.

Now lets take one slightly complicated program to understand all the basics associated with if statement.

Sample Program

Make one program which will display the total amount after deducting the discount of 10%, if the total is more than 2000.


Output

C if Statement - Part 2

Now let’s try to understand this program


1. I hope you must understand the basic initial steps of this program. I will give the explanation for only if statement.

2. In if statement I have given the condition of (amnt>2000), it means it will check the amount if it is greater than 2000. Say, if it is greater than the amount then it will execute the statements in the body of if. Otherwise it will skip it.

3. In our test run we have given the value 2100, as it is greater than 2000. So the control passes inside the if statement body and do following things.

– First it will display the message “You are eligible for discount”
– In the next step it will calculate the discount i.e. 10% of the total amount.
– In the final step it will deduct the discount from the final amount.

4. Now in the last step it will display the final amount.

4 thoughts on “C if Statement – Part 2”

  1. int main()
    {
    printf(“enter amt”);
    int n;
    scanf(“%d”,&n);
    if(n>2000)
    {
    int x;
    float y;
    x=n*(1/10);
    y=(n-x);
    printf(“%f”,y);
    }
    getch();
    }
    what is wrong in this.?

Leave a Comment

Your email address will not be published. Required fields are marked *