Professional Documents
Culture Documents
Section 3.3 - CS 010A: Introduction To Computer Science For Science, Mathematics & Engineering I
Section 3.3 - CS 010A: Introduction To Computer Science For Science, Mathematics & Engineering I
!3.2 If-else
Students:
"
Section 3.3 is a part of 2 assignments: Week 3 Participation Activities
% Requirements: PA
This assignment's due date has passed. Activity will still be recorded, but will not
count towards this assignment (unless the due date is changed). See this article for Entire class was due: 01/18/2021, 10:00 PM PST
more info.
A branch's statements can include any valid statements, including another if-else statement, which are known as nested if-else statements.
if (userChoice == 1) { // userChoice is 1
...
}
else if (userChoice == 2) {
if (numItems < 0) { // userChoice is 2 and numItems < 0
...
}
else { // userChoice is 2 and numItems >= 0
...
}
}
else { // userChoice is not 1 or 2
...
}
Feedback?
PARTICIPATION
ACTIVITY
3.3.1: Nested if-else statements.
Determine the Inal value of salesBonus given the initial values speciIed below.
if (salesType == 2) {
if (salesBonus < 5) {
salesBonus = 10;
}
else {
salesBonus = salesBonus + 2;
}
}
else {
salesBonus = salesBonus + 1;
}
1) salesType = 1; salesBonus = 0;
0
1
10
2) salesType = 2; salesBonus = 4;
5
6
10
3) salesType = 2; salesBonus = 7;
8
9
10
Feedback?
Sometimes the programmer has multiple if statements in sequence, which looks similar to a multi-branch if-else statement but has a very
different meaning. Each if-statement is independent, and thus more than one branch can execute, in contrast to the multi-branch if-else
arrangement.
#include <iostream>
using namespace std;
int main() {
int userAge;
return 0;
}
Feedback?
PARTICIPATION
ACTIVITY
3.3.2: If statements.
1) numBoxes = 0;
numApples = 9;
2) numBoxes = 0;
numApples = 9;
Feedback?
CHALLENGE
ACTIVITY
3.3.1: Enter the output for the multiple if-else branches.
Start
1
#include <iostream>
using namespace std;
3
int main() {
int numItems;
4
numItems = 4;
if (numItems > 3)
cout << "b" <<
{
endl; b
}
else if (numItems < 8) { m
cout << "f" << endl;
}
else {
cout << "k" << endl;
}
return 0;
}
1 2 3 4
Check Next
Feedback?
CHALLENGE
ACTIVITY
3.3.2: If-else statements.
Start
1
1 #include <iostream>
2 using namespace std;
3
4 int main() {
5 int carYear;
6
7 cin >> carYear;
8
9 /* Your code goes here */
10
11 return 0;
12 }
1 2
Check Next
Feedback?
Activity summary for assignment: Week 3 Participation Activities % (163 of 173 points)
Completion details $