0% found this document useful (0 votes)
2 views18 pages

If Statements

The document provides an overview of C++ conditions and if statements, detailing various logical conditions and their syntax. It includes multiple examples demonstrating the use of if, else, and nested if statements for different scenarios such as comparing integers, checking eligibility to vote, and determining grades based on marks. Additionally, it explains the use of switch statements for creating a simple calculator and displaying days of the week based on numeric input.

Uploaded by

akalkidan760
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views18 pages

If Statements

The document provides an overview of C++ conditions and if statements, detailing various logical conditions and their syntax. It includes multiple examples demonstrating the use of if, else, and nested if statements for different scenarios such as comparing integers, checking eligibility to vote, and determining grades based on marks. Additionally, it explains the use of switch statements for creating a simple calculator and displaying days of the week based on numeric input.

Uploaded by

akalkidan760
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

C++ Conditions and If Statements

You already know that C++ supports the usual logical conditions from
mathematics:

 Less than: a < b


 Less than or equal to: a <= b
 Greater than: a > b
 Greater than or equal to: a >= b
 Equal to a == b
 Not Equal to: a != b

C++ if statement

#include <iostream>

using namespace std;

int main () {

// local variable declaration:

int a = 10;

// check the boolean condition

if( a < 20 ) {

// if condition is true then print the following

cout << "a is less than 20;" << endl;

cout << "value of a is : " << a << endl;

return 0;

// When the above code is compiled and executed, it produces the following result -

// a is less than 20;

//value of a is : 10
#include <stdio.h>

int main()

int x = 20;

int y = 22;

if (x<y)

printf("Variable x is less than y");

return 0;

//Variable x is less than y


// Explanation: The condition (x<y) specified in the “if” returns true for the
value // of x and y, so the statement inside the body of if is executed.

#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:

enter the value of x:20


enter the value of y:20
x is equal to y
End of Program

#include <iostream>

using namespace std;

int main() {

int age = 17;

if (age > 18) {

cout << "The person is an adult ";

else {

cout << "The person is not an adult ";

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;

printf("Enter three numbers?");

scanf("%d %d %d",&a,&b,&c);

if(a>b && a>c)

printf("%d is largest",a);

if(b>a && b > c)

printf("%d is largest",b);

if(c>a && c>b)

printf("%d is largest",c);

if(a == b && a == c)

printf("All are equal");

// Output

Enter three numbers?


12 23 34
34 is largest
#include <iostream>

using namespace std;

int main() {

int a = 2;

if (a >= 0) {

cout << "This is a whole number ";

else {

cout << "Not a whole number ";

return 0;

#include <iostream>

using namespace std;

int main() {

int x = 20;

int y = 18;

if (x > y)

cout << "x is greater than 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,

// we print to the screen that "x is greater than y".


Example 2: if...else statement
// Check whether an integer is odd or even

// The return value of a scanf call is the number of arguments successfully


read. You can use this to check whether all expected arguments were read.
#include <stdio.h>

int main() {

int number;

printf("Enter an integer: ");

scanf("%d", &number);

// True if the remainder is 0

if (number%2 == 0) {

printf("%d is an even integer.",number);

else {

printf("%d is an odd integer.",number);

return 0;

// When the user enters 7, the test expression number%2==0 is evaluated to


false. Hence, the statement inside the body of else is executed.
Program to check whether a person is eligible to vote or not.
1. #include <stdio.h>
2. int main()
3. {
4. int age;
5. printf("Enter your age?");
6. scanf("%d",&age);
7. if(age>=18)
8. {
9. printf("You are eligible to vote...");
10. }
11. else
12. {
13. printf("Sorry ... you can't vote");
14. }
15. }
16. Output
17. Enter your age?18
18. You are eligible to vote...
19. Enter your age?13
20. Sorry ... you can't vote
Example 3: C if...else Ladder
// Program to relate two integers using =, > or < symbol

#include <stdio.h>
int main() {
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);

//checks if the two integers are equal.


if(number1 == number2) {
printf("Result: %d = %d",number1,number2);
}

//checks if number1 is greater than number2.


else if (number1 > number2) {
printf("Result: %d > %d", number1, number2);
}

//checks if both test expressions are false


else {
printf("Result: %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;

printf("Enter your marks?");

scanf("%d",&marks);

if(marks > 85 && marks <= 100)

printf("Congrats ! you scored grade A ...");

else if (marks > 60 && marks <= 85)

printf("You scored grade B + ...");

else if (marks > 40 && marks <= 60)

printf("You scored grade B ...");

else if (marks > 30 && marks <= 40)

printf("You scored grade C ...");

else

printf("Sorry you are fail ...");


}

Output

Enter your marks?10


Sorry you are fail ...
Enter your marks?40
You scored grade C ...
Enter your marks?90
Congrats ! you scored grade A ...
Example 4: C++ program to find if an integer is even or odd or
neither (0) using Nested if
// C++ program to find if an integer is even or odd or neither (0)

// using nested if statements

#include <iostream>

using namespace std;

int main() {

int num;

cout << "Enter an integer: ";

cin >> num;

// outer if condition

if (num != 0) {

// inner(nested) if condition

if ((num % 2) == 0) {

cout << "The number is even." << endl;

// inner(nested) else condition

else {

cout << "The number is odd." << endl;

// outer else condition

else {

cout << "The number is 0 and it is neither even nor odd." << endl;

cout << "This line is always printed." << 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

Enter an operator (+, -, *, /): +


Enter two numbers:
2.3
4.5
2.3 + 4.5 = 6.8

Output 2

Enter an operator (+, -, *, /): -


Enter two numbers:
2.3
4.5
2.3 - 4.5 = -2.2

Output 3

Enter an operator (+, -, *, /): *


Enter two numbers:
2.3
4.5
2.3 * 4.5 = 10.35

Output 4

Enter an operator (+, -, *, /): /


Enter two numbers:
2.3
4.5
2.3 / 4.5 = 0.511111

Output 5

Enter an operator (+, -, *, /): ?


Enter two numbers:
2.3
4.5
Error! The operator is not correct.

How This Program Works


1. We first prompt the user to enter the desired operator. This input is then
stored in the char variable named oper .

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>

using namespace std;

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

You might also like