Home » Syntax References

if statement syntax in C/C++ language

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

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

if(test_condition)
{
	//statement(s);
}

If the value of test_condition is true (non zero value), statement(s) will be executed. There may one or more than one statement in the body of if statement.

  • If there is only one statement, then there is no need to use curly braces.
  • But, if there are multiline code within the if statement body, you must use curly braces.
  • If there is multiline code and you do not use curly braces, then only first statement of the body is considered as if statement body.

Be sure, when you have to use curly braces or not?

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");
	
	if(b)
		printf("Second condition is true\n");
	
	if(c)
		printf("Third condition is true\n");
	
	return 0;
	
}

Output

First condition is true 
Second condition is true 

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.



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.