0% found this document useful (0 votes)
32 views

LAB 9 C++ Part 2

The document discusses using if-else statements in C++ to make decisions based on logical expressions. It provides examples of comparison operators like ==, !=, <, <=, >, >= and logical operators like !, &&, ||. It includes code for a sample program that tests if a user-input value equals 0, and prints different messages depending on the result. It then lists some exercises for writing programs using if statements to find maximum values, handle division by zero, and classify marks into grades using nested if-else if statements.

Uploaded by

Kareem Abdo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

LAB 9 C++ Part 2

The document discusses using if-else statements in C++ to make decisions based on logical expressions. It provides examples of comparison operators like ==, !=, <, <=, >, >= and logical operators like !, &&, ||. It includes code for a sample program that tests if a user-input value equals 0, and prints different messages depending on the result. It then lists some exercises for writing programs using if statements to find maximum values, handle division by zero, and classify marks into grades using nested if-else if statements.

Uploaded by

Kareem Abdo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Computing Fundamentals CS1150 ______ Lab NO.

9: C++
programming

1- MAKING DECISIONS

The if-else statement provides the capability of making decisions. The general format of the if-
else statement is:

If (expression)
{
Statements

}
else
{
Statements

If expression is true, then the program will execute statements between the curly brackets that
follow if. Otherwise, the program will execute statements between the curly brackets that follow
else.

In C++ we use the following operators to formulate conditions and logical expressions:

Operator Meaning examples Operator Meaning examples


== equal to x == 5 ! not !x
!= not equal to x != 5 !(x > 5)
< Less x<5 && and x > 5 && y < 7
<= less or equal x <= 5 12 <= x && x <= 20
> greater x>5 || or x != 0 || y > 3
>= greater or equal x >= 5 x < 12 || x > 20
2- Please type in the following program, compile and run it. Test the program by entering
3, 0 and -5.

#include <iostream>

using namespace std;

int main()
{

int value;
cout<<"Please enter a value: "<<endl;

cin>>value;

if (value == 0)
{
cout<<"The value was 0\n"<<endl;
cout<<"Thank you for your input\n"<<endl;
} else
{
Page 1 of 2
Computing Fundamentals CS1150 ______ Lab NO. 9: C++ programmin

cout<<"The value was not 0\n"<<endl;


cout<<"Thank you for your input\n"<<endl;
}
cout<<"Bye\n"<<endl;

return 0;
}

3- Writing programs using if statements

 Write a program that reads two numbers a and b. Print the maximum value of the two numbers.

 Write a program that reads two values a and b. If b is 0, output “b is 0”. Otherwise output the result
of the division of a by b (a / b).

NESTED IF STATEMENTS: IF – ELSE IF

 Write a program that reads three numbers a, b, and c. Print the maximum value of the three numbers.

 Write a program that classifies a student mark as follows:

[90 – 100] : A
[80 – 90 ] : B
[70 – 80 ] : C
[60 – 70 ] : D
[50 – 60 ] : E
[0 – 50 ] : F

Page 2 of 2

You might also like