if and if-else statement in C programming language
If and if-else statement in C programming language
In todays blog post we will cover the concept of conditional statements in C programming with simple examples, so that our audience can understand it in an easy manner. These conditional or control statements are used to control the flow of execution, based on some predifined conditions. Conditional statements are one of the basic builiding blocks of any programming language and the same is true with respect to C programming language.
Today we will cover two of the most basic decision making statements or control statements which are known as,
1) if statement
2) if-else statement
Before diving deep into C programming coding examples, let us look at the basic syntax and basic working of the two conditional statements.
Basic syntax of if statement in C programming language
if (condition) { // code to be written here. }
If we look at the above if statement coding syntax we can see that, we started with the if key word followed by the round brackets () with condition written between them. After these round brackets () we have opening and closing curly brackets {}. These opening and closing curly brackets defines the body of if statement in C programming language. So this is the main basic syntax of if statement in C programming language. And between these curly brackets we can write the coding part or lines. Now if the given condition is true that code will simply run and execute and if the condition is false then the if condition will not run.
Basic syntax of if-else statement in C programming language
if (condition) { // code to be written here. } else { // code to be written here. }
Now we are looking at the syntax of if-else statement in C programming language. In this coding syntax the starting point is again the if statement or condition. And it will work exactly the same way as we discussed earlier in the if statement syntax part. So when the if condition is true the specific code will run, but how the program will behave if the given if condition is false.
For this case we simply use else block to handle or tackle this issue for us. When the if condition is false, then the else part will run with its body between the opening and closing curley brackets and it will handle the scenarios when the given if condition is false then simply the else part will run in C programming language.
The main difference between the if and else part is that with the if statement we have condition written between opening and closing round brackets, where as in the else part there is no condition specified with respect to else block. So this is the basic syntax of if-else statement in C programming language.
Simple coding example of if statement in C programming language
#include<stdio.h> void main() { int num = 5; if (num == 5) { printf("num is equal to 5"); } _getch(); }
Let us look at the above C programming coding example in which at the start of the program with in our main() method or function, we simply declared and initialized an integer variable named as num and we assigned the value of 5 to this num variable like this,
int num = 5;after that we simply used the if statement and with in the if statement condition part we simply write a condition that if num is equal equal (==) to 5, which simply means that the value insided the num variable is equal to 5 like this,
if (num == 5) { printf("num is equal to 5"); }
We are simply comparing the value in num variable with 5 and trying to say that if this condition is true then simply print the message the num is equal to 5. And if the condition is false then don't run the if statement.
Now in this case we know that the if condition is true because we initialized the value of 5 in num variable at the time of declaration.
Now on the other hand what would happen if the given condition is false, how will we tackle or handle that scenario. For that purpose we will use if-else statement in C programming language.
Simple coding example of if-else statement in C progamming language
#include<stdio.h> void main() { int num = 6; if (num == 5) { printf("num is equal to 5"); } else { printf("num is not equal to 5"); } _getch(); }
In the above coding example of if-else statement in C programming language we have used if-else statement to better understand the concept, with simple coding example. Now in the above C programming code we have again declared and initialized num variable with value = 6. The if condition will work in exactly the same manner as discussed earlier in the previous section.
Now consider the scenario when the if condition is false for example let us initialize the value of 6 to variable num,
int num = 6;and use the same if condition which says that num == 5 i.e,
if (num == 5) { printf("num is equal to 5"); }
Now this time the if condition is false, because we initialized the value to 6. So in this case the else part will run and print the message that num is not equal to 5 i.e,
else { printf("num is not equal to 5"); }
So this is how the if-else statement works in C programming language.
Congratulations! like always we have successfully implemented the if statement and if-else statement in C programming language.
Very helpful post for beginner programmers.
ReplyDelete