If Statement in C++
if statement:
The if
statement is used to express the conditional expressions. If the given condition is true then it will execute the statements otherwise it will execute the optional statements.
The basic simple structure of the if
statement is shown below:
if (expression) { //set of statements }
The expression must be placed in round brackets as shown above. In this form, the set of statements would be executed only if the expression has a non-zero value (i.e., if expression is true). If the expression has a value zero (i.e., if expression is false), then the set of statements would be ignored by C++ compiler. The set of statements are skipped and the execution continues with the next statements.
Example:
Given below is a program which reads two numbers and then prints greater one using if statement.
#include <iostream.h> void main() { int a, b; cout << "Enter Two Numbers:"; cin >> a >> b; if (a > b) { cout << "Greater is " " << a; } if (b > a) { cout << "Greater is " " << b; } }
1 Response
[…] if statement […]