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.





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.