if Statement in C++

if statement is a conditional statement that excute or skip statement or set of ststements on the basis of provided condition.

Syntax

Syntax of if statement with single and multiple statements is as.

if ( condition) statement;

 Here condition is relational expression that always return either True or False.

if (condition) {

Statements ;

}

Example

if statement with single Instructions

Here is the code example that uses single Instructions with if statement. So that there is no need of curly parentheses.

#include <iostream>
using namespace std;

int main(int argc, char** argv) {
	
	int a = 1;
	
	if(a > 0)
	cout<<"Hello";
	
	return 0;
}

if statement with multiple Instructions

Here is another code example that uses multiple Instructions with if statement. So that these statements are enclosed in curly parentheses.

#include <iostream>
using namespace std;

int main(int argc, char** argv) {
	
	int a = 1;
	
	if(a > 0) {
	cout<<"Hello";
	cout<<"World";
	}
	return 0;
}

Limitations of if Statement

There is a limitation of if statement.

There is no alternative message or instructions in case of false condition.

Comments
Login to TRACK of Comments.