Forget your life decision, make your program to take the right decision: Conditional Construct in C: Part-2
Previous:
if (or only if):
‘if’ is the most simple decision making statement in C. If we compare it to real life these conditions are the once we do unknowingly every day.
Switching off the light and fan while leaving the room.
Closing the tap after use.
Eating when we are hungry.
Yes, this conditional statement deals with really simple stuff.
Syntax:
Here we provide a condition in () after ‘if’. If the condition is satisfied the statement under the if in {} is executed and if the condition is not satisfied the statements under the if will be ignored and the program will move forward ignoring that.
Let’s see some example:
Here the program will not give any output if the entered character is not ‘x’. if the entered character is ‘x’ it will print “Alphabet is x”.
Here also the program will print “Even no” if the no entered is even. It won’t print anything if the input no is odd.
A point to note here, we have not used {} after the if statement then also it is working. Yes, we can write in this way.
If we are not using the {} after the if statement if will only the consider the next one line as its statement. Basically {} are used when we have to print more than one line after the if statement.
Just see how it is done in the above example. As we required just one line of operation under the if condition.
Let’s look at one more example:
Here as you can see we have used {} as the operation required under the if the condition required 3 lines of code.
So this was if(or only if) Statement in C. The most simple decision making statement.
If else:
Here a new keyword is used, ‘else’. It is not used independently but with an if statement. When the if condition is not satisfied a given condition then it moves to the else part. We don’t have any condition in the else part. When we use this keyword with the final alternative for a situation.
Here one condition is provided but with two possibilities if the condition is satisfied go with first else go with the other.
Let’s see with an example:
If it rains, I will go by car else Bike.
If Rahul gets more than 40%, he will pass else he will fail.
If the exams are near, I will study else play.
See here, one condition is there but we have two choices, one when the condition is satisfied and the other when the condition is not satisfied.
Syntax:
As you see the syntax condition is only provided with ‘if’ statement ‘else’ do not have any condition.
what happens here is that the condition in if statement is checked if the condition is satisfied it executes the statement in the if part (i.e. statement 1 is executed) and if the condition is not satisfied the statement in else part is executed (i.e. statement 2 is executed).
Let us see some examples:
So this was a bit advanced conditional construct than only if.
It has an alternative statement if the condition is not satisfied other than the only if statement.
Previous: