Exercise
Part 1:
Evaluate the logical (Boolean) expression
- int num1 = 3, num2 = 2;
(num1 > num2)
- double hours = 12.8;
(hours > 40.2)
- int funny = 7;
(funny !=1)
- char letter = 'A';
('a' < letter)
- short count = 1;
count <=4;
- double y = -2.3;
y >=0.0;
Part 2:
Evaluate the logical (Boolean) expression
- ! (false || false) || true
- false & true & false
- ! false || (false || ! true)
- false || (true & (false || false) )
- 1 || ! true & false
- ! (false || false) || false
- true & false & false
- ! false || (false || ! true)
- false || (false & (false || true) )
- 0 || ! false & false
- 7 + 6 > 15 & 6 + 2 == 7
- 8 > 15 || 6 < 5 + 3 & 8 == 2/2
- if num1 has the value of 16 and num2 has the value of 18:
num1 % 8 == num2 % 6 & num1 < num2
Part 3:
Evaluate the logical (Boolean) expression
- string name1 = “Aaron”;
string name2 = “aaron”;
(name1 == name2)
- string name1 = “Aaron”;
string name2 = “Aardvark”;
(name1 <= name2)
- bool flag = true;
int a = 2, b = 5, c = 10;
(a * b <= c & ! flag)
- char letter = 'A';
string word = “A”;
(letter == word)
Part 4:
Using the Selection Control Structure if
Write what the following statements display as output after the program executes. Use the following
two assignment statements for Exercises 1 through 7.
intx=6;
boolfound=false;
1. if(x>10)
cout“xisgreaterthan10\n”;
cout“Selectionallowsdecisionmaking.\n”;
2. if(x==6)
cout“Amatchisfound.\n”;
cout“Sequencecontinuesafterselectioniscomplete\n”;
3. if(x8)
{
cout“xiswithintherange\n”;
cout“Thisisatruestatement\n”;
}
cout“And,afterselection,thenextstatementisexecuted.\n”;
4. if(x==6||found)
cout“Problem4istrue\n”;
cout“EndofProblem4\n”;
5. if(x==6found)
cout“Problem5istrue\n”;
cout“EndofProblem5\n”;
6. if(x!=6!found)
cout“Problem6istrue\n”;
cout“EndofProblem6\n”;
7. if(x0x10)
cout“xisin range\n”;
cout“Thevalueofxis“+x;
Part 5:
Using the Selection Control Structure if...else
Use the following two assignment statement for Exercises 8 through 14 of this lab.
intx=6;
boolfound=false;
8. intx=6;
if(x10)
cout“xisgreaterthan10\n”;
else
cout“xislessthanorequalto10\n”;
cout“Selectionallowsdecisionmaking.\n”;
9. if(x==6)
cout“Amatchisfound.\n”;
else
cout“Amatchwasnotfound.\n”;
cout“Sequencecontinuesafterselectioniscomplete\n”;
10. if!(x8)
{
cout“xiswithintherange\n”;
cout“Thisisatruestatement\n”;
}
else
{
cout“xisoutofrange\n”;
cout“Thisisafalsestatement\n”;
}
cout“Afterselection,thenextstatementisexecuted.\n”;
11. if(x>=5)
{}
else
cout“Haveagoodday\n”;
cout“xislargerthan5\n”;
12. if(x==6found)
cout“Problem12istrue\n”;
else
{
cout“Problem12isfalse\n”;
cout“Bothconditionsmustbetrue\n”;
}
cout“EndofProblem12\n”;
13. if(x!=6!found)
cout“Problem13istrue\n”;
else
cout“Problem13isfalse\n”;
cout“EndofProblem13\n”;
14. if(x0x10)
cout“xisinrange\n”;
else
cout“xisnotinrange\n”;
Part 6:
Nested if-else structure: Write what the following statements display as output after the program executes.
1a. Write what the following statements display as output after the program executes.
inttemperature=78;
intmonth=6;
stringname=“PatBoone”;
if(temperature>=70month>=6)
cout“Wearwhiteshoes.\n”;
elseif(name==“PatBoone”)
cout“Wearwhiteshoes.\n”;
else
cout“Wearblackshoes.\n”;
1b. What is the output of the program in Exercise 1a when temperature = 70, month = 5, and
name = “Pat Boone”?
1c. What is the output of the program in Exercise 1a when temperature = 60, month = 5, and
name = “Pat Boone”?
1d. What is the output of the program in Exercise 1a when temperature = 60, month = 5, and
name = “Your name”?