Control flow statement in C (If – Else) – Part 1

This article is all about the control flow statement in C (If – else). It means if you want to control the certain execution of the program according to the condition described by you. For example, you want to check if the number entered by the user is odd or even, you have to use a certain type of control flow statement.

“If-else” in C

The syntax of “if else” statement in C is –

if (condition) {

//put some code when the condition is true
}
else {
//code when the condition is false
}

The condition is written inside the parenthesis of if condition. If the condition is true, then the block inside the “if statement” is going to execute and if the condition is false, then the block inside the else statement is used. Coming to the problem mentioned above, here is the code of that problem.

#include<stdio.h>

int main()
{
	int num1, num2;
	printf("Enter two numbers\n");
	scanf("%d%d",&num1, &num2);
	if(num1 > num2) {
		printf("Number %d is greator than %d", num1, num2);
	}
	else {
		printf("Number %d is less than or equal to %d", num1, num2);
	}
	return 0;
}

 

I think the code is pretty much self-explanatory. If the number entered by the user is divisible by two, then the remainder of the number should be 0. “==” is used to check if the LHS equals RHS.

Now you get the bit of hang of about if else statement, let us know some properties of it.

Can we use “if” without the else?

Yes, you can. If you just want to check the certain condition you can directly use if without the else.

if(condition) {
//put some code
}

What if there are multiple conditions?

Now, there are different ways to implement this. One is nested if else in which you write if block inside the existing if block. Something like this –

if (condition ) {
    if(condition) {
    }
    else {
            }
}
else {

}

Or you can also write like this –

if (condition) {

}

else if (condition) {

}

else {

}

Here you can write if statement after else. Something like if –> else if –> .. and so on. Or you can write like this.

if (condition) {

}

if( condition) {

}

Here there is no else, just some if conditions are written sequentially. There is a difference in method 2 and 3 in execution. Method two will not work if there is a condition which is true in both the if blocks. It’s going to execute the first condition and will not execute the other one because it will consider it in else condition. Method 3 must be used in this case.

Bonus Information about if else statements in C/ C++

In C and C++, if you write any number greater than 0 inside the if condition, it’s going to take as if the condition is true. If you write 0 or less than 0, it’s going to take as false. For example :

if(1) {
//this will execute 
}
if (0) {
//this will not execute
}

Also, if you just want to execute one line of code inside the if condition, you don’t need to put the parenthesis.

if (condition)
//one line of code for true condition

I think that’s it for this article. Please do let us know in the comment section about what you think. Check out other articles.

Share This