If Statements
If Statements
You already know that C++ supports the usual logical conditions from
mathematics:
C++ if statement
#include <iostream>
int main () {
int a = 10;
if( a < 20 ) {
return 0;
// When the above code is compiled and executed, it produces the following result -
//value of a is : 10
#include <stdio.h>
int main()
int x = 20;
int y = 22;
if (x<y)
return 0;
#include <stdio.h>
int main()
{
int x, y;
printf("enter the value of x:");
scanf("%d", &x);
printf("enter the value of y:");
scanf("%d", &y);
if (x>y)
{
printf("x is greater than y\n");
}
if (x<y)
{
printf("x is less than y\n");
}
if (x==y)
{
printf("x is equal to y\n");
}
printf("End of Program");
return 0;
}
// In the above example the output depends on the user input.
// Output:
#include <iostream>
int main() {
else {
return 0;
Output 1:
The given test expression age > 18 is not true because the input age is 17. Hence, the
body of the else statement is executed.
#include <stdio.h>
int main()
int a, b, c;
scanf("%d %d %d",&a,&b,&c);
printf("%d is largest",a);
printf("%d is largest",b);
printf("%d is largest",c);
if(a == b && a == c)
// Output
int main() {
int a = 2;
if (a >= 0) {
else {
return 0;
#include <iostream>
int main() {
int x = 20;
int y = 18;
if (x > y)
return 0;
// Example explained
// In the example above we use two variables, x and y, to test whether x is greater than y (using the >
operator). As x is 20, and y is 18, and we know that 20 is greater than 18,
int main() {
int number;
scanf("%d", &number);
if (number%2 == 0) {
else {
return 0;
#include <stdio.h>
int main() {
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
return 0;
}
Program to calculate the grade of the student according to
the specified marks.
#include <stdio.h>
int main()
int marks;
scanf("%d",&marks);
else
Output
#include <iostream>
int main() {
int num;
// outer if condition
if (num != 0) {
// inner(nested) if condition
if ((num % 2) == 0) {
else {
else {
cout << "The number is 0 and it is neither even nor odd." << endl;
return 0;
}
Output 1
Enter an integer: 2
The number is even.
This line is always printed.
Output 2
Enter an integer: 3
The number is odd.
This line is always printed.
Output 3
Enter an integer: 0
The number is 0 and it is neither even nor odd.
This line is always printed.
In the above example,
We take an integer as an input from the user and store it in the variable num .
We then use an if...else statement to check whether num is not equal
to 0 .
o If true , then the inner(nested) if...else statement is executed.
o If false , the code inside the outer else condition is executed, which prints
"The number is 0 and neither even nor odd."
The inner(nested) if...else statement checks whether the input number is
divisible by 2 .
o If true , then we print a statement saying that the number is even.
o If false , we print that the number is odd.
Notice that 0 is also divisible by 2 , but it is actually not an even number. This is
why we first make sure that the input number is not 0 in the outer if condition.
Note: As you can see, nested if...else makes your logic complicated. If
possible, you should always try to avoid nested if...else .
Example: Create a Calculator using the switch Statement
// Program to build a simple calculator using switch Statement
#include <iostream>
using namespace std;
int main() {
char oper;
float num1, num2;
cout << "Enter an operator (+, -, *, /): ";
cin >> oper;
cout << "Enter two numbers: " << endl;
cin >> num1 >> num2;
switch (oper) {
case '+':
cout << num1 << " + " << num2 << " = " << num1 + num2;
break;
case '-':
cout << num1 << " - " << num2 << " = " << num1 - num2;
break;
case '*':
cout << num1 << " * " << num2 << " = " << num1 * num2;
break;
case '/':
cout << num1 << " / " << num2 << " = " << num1 / num2;
break;
default:
// operator is doesn't match any case constant (+, -, *, /)
cout << "Error! The operator is not correct";
break;
}
return 0;
}
Output 1
Output 2
Output 3
Output 4
Output 5
2. We then prompt the user to enter two numbers, which are stored in the float
variables num1 and num2 .
3. The switch statement is then used to check the operator entered by the user:
o If the user enters + , addition is performed on the numbers.
o If the user enters - , subtraction is performed on the numbers.
o If the user enters * , multiplication is performed on the numbers.
o If the user enters / , division is performed on the numbers.
o If the user enters any other character, the default code is printed.
Let’s take the same example but this time with break statement.
#include <iostream>
int main(){
int i=2;
switch(i) {
case 1:
cout<<"Case1 "<<endl;
break;
case 2:
cout<<"Case2 "<<endl;
break;
case 3:
cout<<"Case3 "<<endl;
break;
case 4:
cout<<"Case4 "<<endl;
break;
default:
cout<<"Default "<<endl;
return 0;
Output:
4. Case2
5. Now you can see that only case 2 got executed, rest of the subsequent
cases were ignored.
// C++ program to returns day based on the numeric value.
#include <iostream>
using namespace std;
class Day {
private:
int day = 3;
public:
void set_data()
{
cout << "Enter no of day you want to display: ";
cin >> day;
}
void display_day()
{
switch (day) {
case 1:
cout << "MONDAY";
break;
case 2:
cout << "TUESDAY";
break;
case 3:
cout << "WEDNESDAY";
break;
case 4:
cout << "THURSDAY";
break;
case 5:
cout << "FRIDAY";
break;
case 6:
cout << "SATURDAY";
break;
case 7:
cout << "SUNDAY";
break;
default:
cout << "INVALID INPUT";
break;
}
}
};
main()
{
Day d1;
d1.display_day();
return 0;
}
Output
WEDNESDAY