Home » Syntax References

if else statement syntax in C/C++ language



Syntax of if else statement in C/C++ programming language, this article contains syntax, examples and explanation about the if else statement in C/C++ language.

Here, is the syntax of if else statement in C or C++ programming language:

if(test_condition)
{
	//statement(s)/true block;
}
else
{
	//statement(s)/false block;	
}

If the value of test_condition is true (non zero value), statement(s) written in true/if block will be executed and if it is false (zero) statesmen(s) written in false/else block will be executed.

Curly braces rule is same as we have discussed in last chapter (if statement syntax in C/C++), curly braces are not required, if there is only one statement.

Consider the given example:

#include <stdio.h>

int main()
{
	int a=10;
	int b=-10;
	int c=0;
	
	if(a==10)
		printf("First condition is true\n");
	else
		printf("First condition is false\n");
	
	if(b)
		printf("Second condition is true\n");
	else 
		printf("Second condition is false\n");
	
	if(c)
		printf("Third condition is true\n");
	else
		printf("Third condition is false\n");
	
	return 0;	
}

Output

First condition is true 
Second condition is true
Third condition is false 

In this program there are three conditions
if(a==10)
This test condition is true because the value of a is 10.
if(b)
This test condition is true because the value of b is -10 that is a non zero value.
if(c)
This test condition is not true because the value of c is 0, so, else part is executed and the statement written in else part is printed.






Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.