Making Decisions: Namiq Sultan
Making Decisions: Namiq Sultan
Making Decisions: Namiq Sultan
Making Decisions
Namiq Sultan
University of Duhok
Department of Electrical and Computer Engineerin
Expression Value
x < y False, because x is not less than y .
Program Output
True is 1
False is 0
Statement(s)
cout << "Enter 3 test scores and I will average them: ";
cin >> score1 >> score2 >> score3;
average = (score1 + score2 + score3) / 3.0;
cout << "Your average is " << average << endl;
if (average > 95)
cout << "Congratulations! That's a high score!\n";
return 0;
}
C++ Programming, Namiq Sultan 10
Program Output with Example Input
Enter 3 test scores and I will average them: 100 100 100 [Enter]
Your average is 100
Congratulations! That's a high score!
if (expression)
{
statement;
statement;
// Place as many statements here as necessary.
}
cout << "Enter 3 test scores and I will average them: ";
cin >> score1 >> score2 >> score3;
False True
expression
Statement(s) Statement(s)
int main()
{
float num1, num2, quotient;
cout << "Enter a number: ";
cin >> num1;
cout << "Enter another number: ";
cin >> num2;
int main()
{
int testScore;
char grade;
int main( )
{
int testScore;
char grade;
cout << "Enter your test score and I will tell you\n";
cout << "the letter grade you earned: ";
cin >> testScore;
#include <iostream>
using namespace std;
int main()
{
int testScore;
cout << "Enter your test score and I will tell you\n";
cout << "the letter grade you earned: ";
cin >> testScore;
C++ Programming, Namiq Sultan 34
if (testScore < 60)
cout << "Your grade is F.\n";
else if (testScore < 70)
cout << "Your grade is D.\n";
else if (testScore < 80)
cout << "Your grade is C.\n";
else if (testScore < 90)
cout << "Your grade is B.\n";
else if (testScore <= 100)
cout << "Your grade is A.\n";
else // Default action
{
cout << testScore << " is an invalid score.\n";
cout << "Please enter scores no greater than 100.\n";
}
return 0;
}
int main()
{
char employed, grad;
cout << "Answer the following questions\n";
cout << "with either Y for Yes or ";
cout << "N for No.\n";
cout << "Are you employed? ";
cin >> employed;
cout << "Have you graduated from college ";
cout << "in the past two years? ";
cin >> grad;
C++ Programming, Namiq Sultan 38
if (employed == 'Y')
if (grad == 'Y') // Nested if
{
cout << "You qualify for the special ";
cout << "interest rate.\n";
}
return 0;
}
int main()
{
char employed, grad;
Expression !(Expression)
True False (0)
False True (1)
!
&&
||
X < 0 ? Y = 10 : Z = 20;
int main(void)
{
char choice;
cout << "Enter A, B, or C: ";
cin >> choice;
Enter A, B, or C: B [Enter]
You entered B.
Enter a A, B, or C: F [Enter]
You did not enter A, B, or C!