C++ → Basics → Arithmetical Operations → Input in C++ → Conditions and Decisions → if/else conditions
The if / else conditions are used to make some very basic decisions. Learn the construction of conditional statements in C Plus Plus step by step.
Construction: The if statement
if(condition)
{
statement
}
For example,
#include <iostream>
using namespace std;
int main()
{
if (10 > 5)
{
cout << "Yes, 10 is greater than 5" << endl;
}
return 0;
}
As, the given condition 10 > 5 is true. It will print the statement.
#include <iostream>
using namespace std;
int main()
{
if (10 < 5)
{
cout << "Yes, 10 is greater than 5" << endl;
}
return 0;
}
As, the given condition 10 < 5 is not true. It will not print the statement.
The following program takes a number from the user, and if the number is greater than 0. It shows “the number is positive”.
#include <iostream>
using namespace std;
int main()
{
int a;
cout << "Please insert a number: "; cin >> a;
if (a > 0)
{
cout << "The number is positive" << endl;
}
return 0;
}
Construction: The if / else statement
if(condition)
{
statement
}
else
{
statement
}
For example,
#include <iostream>
using namespace std;
int main()
{
int a;
cout << "Please insert a number: "; cin >> a;
if (a > 0)
{
cout << "The number is positive" << endl;
}
else
{
cout << "The number is NOT positive" << endl;
}
return 0;
}
Construction: The if / else if / else statement
if(condition)
{
statement
}
else if
{
statement
}
else
{
statement
}
For example,
#include <iostream>
using namespace std;
int main()
{
int a;
cout << "Please insert a number: "; cin >> a;
if (a > 0)
{
cout << "The number is positive" << endl;
}
else if (a < 0)
{
cout << "The number is negative" << endl;
}
else
{
cout << "The number is zero" << endl;
}
return 0;
}
A simple if example
#include <iostream>
using namespace std;
int main()
{
int a;
cout << "Please insert a number: "; cin >> a;
if (a == 0)
{
cout << "The number is zero" << endl;
}
else
{
cout << "The number is NOT zero" << endl;
}
return 0;
}
Remember: We use two == signs for ‘is equal to’.
not equal to
#include <iostream>
using namespace std;
int main()
{
int a;
cout << "Please insert a number: "; cin >> a;
if (a != 0)
{
cout << "The number is NOT ZERO" << endl;
}
else
{
cout << "The number is ZERO" << endl;
}
return 0;
}
Remember: We use != signs for ‘not equal to’.
Q. Write a program in C++ that take a number from the user, and shows whether is number is even or odd.
#include <iostream>
using namespace std;
int main()
{
int a;
cout << "Please insert a number: "; cin >> a;
if (a%2==0)
{
cout << "The number is Even" << endl;
}
else
{
cout << "The number is ODD" << endl;
}
return 0;
}
Remember: We use % sign for Division, and two == signs for ‘is equal to’.
Q. Write a program in C++ that take a number from the user, and shows whether is number is even or odd. And, when the input is zero it shows ‘the number is zero’.
#include <iostream>
using namespace std;
int main()
{
int a;
cout << "Please insert a number: "; cin >> a;
if (a==0)
{
cout << "The number is Zero" << endl;
}
else if (a%2==0)
{
cout << "The number is Even" << endl;
}
else
{
cout << "The number is ODD" << endl;
}
return 0;
}
Q. Write a program in C++ that take username and password from the user. If the username and password matched your username and password, it says “Welcome” otherwise “Incorrect”.
#include <iostream>
using namespace std;
int main()
{
string username = "bilal";
string password = "sussex";
string a;
string b;
cout << "Please insert your User Name: "; cin >> a;
cout << "Please insert your Password: "; cin >> b;
if (a==username && b==password)
{
cout << "You are Welcome!" << endl;
}
else
{
cout << "The incorrect Username or Password!" << endl;
}
return 0;
}
Remember: We use two && signs for ‘And’. And we used string because we do not have to perform arithmetical operations.
NEXT: The switch statement