Week 5 Lab
Sign in to lab!
Q&A on Material
if
,else if
, andelse
(conditional) statements
Tracing
Trace the execution of this program and predict what this program will output when the users types in 3
. Then repeat the exercise if the user types in 9
.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int i;
char color = 'r';
cin >> i;
if (i % 5 != 0)
{
i++;
color = 'b';
}
if (i % 5 == 0)
{
i -= 2;
}
else if (i % 4 == 0)
{
i++;
if ((color == 'r') || (i == 9))
{
color='c';
}
}
else
{
i += 5;
color='g';
}
return 0;
}
Idioms Related to Conditional Statements
Take a look at the idioms related to if
(conditional) statements:
Exercises
Walk through these exercises and complete them as a group. Identify what idiom is most appropriate for each exercise.
cpp/cs102/practice6/tippingCalculator
cpp/cs102/decision-tree/menu
cpp/cs102/lookup-table/sales-tax
HW05 Review
- We may not have covered functions yet, so for now consider the logic of finding the leap year without using functions.
- Review how to examine the submission report.
- 20% of your score will be awarded based on code inspection.
Q&A on Material
Open time to get help on HW05 or any other material.