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