Skip to main content
Contents
Dark Mode Prev Up Next Scratch ActiveCode Profile
\(
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Section 5.14 Assessment: Nested If-Statements
Subgoals for Evaluating Selection Statements.
Diagram which statements go together
For if statement, determine whether expression is true or false
If true β follow true branch, if false β follow else branch or do nothing if no else branch
Exercises Exercises
1.
Q1: Following this code what is the value of
delta?
int alpha = 5, beta = 2, gamma = 3, delta = 0, eta = 0;
if (alpha > beta)
{
if (beta > gamma)
{
delta = 10;
}
else
{
delta = 20;
}
}
else
{
if (gamma > beta)
{
delta = 30;
}
else
{
delta = 40;
}
}
2.
Q2: Following this code, what is the value of
eta?
int alpha = 5, beta = 2, gamma = 3, delta = 0, eta = 0;
if (alpha > beta)
{
if ((beta * gamma) <= alpha)
{
eta = 50;
}
else
{
eta = 60;
}
}
else
{
if (delta > beta)
{
eta = 70;
}
else
{
eta = 80;
}
}
3.
Q3: Following this code, what is the value of
delta?
int alpha = 5, beta = 2, gamma = 3, delta = 0, eta = 0;
if (alpha > gamma)
{
if (gamma > beta)
{
delta += 1;
}
if (gamma > alpha)
{
delta += 2;
}
}
if ((beta + gamma) >= alpha)
{
if ((alpha - beta) > gamma)
{
delta += 3;
}
else
{
delta += 4;
}
}
4.
Q4: Following this code, what is the value of
delta and
eta?
int alpha = 5, beta = 2, gamma = 3, delta = 0, eta = 0;
if ((alpha > beta) && (alpha > gamma))
{
if (alpha > delta)
{
delta += 1;
eta += 2;
}
if (beta > delta)
{
delta += 1;
eta += 2;
}
}
if ((beta < gamma) && (beta <= delta))
{
if (eta > gamma)
{
delta += 2;
eta += 3;
}
else
{
delta += 3;
eta += 4;
}
}
delta = 1, eta = 2
delta = 2, eta = 4
delta = 4, eta = 7
delta = 5, eta = 8
delta = 6, eta = 9
5.
Q5: Variables
a,
b, and
c are all declared as characters. If
a='A', b='B', c='C'; What is printed?
if (a < b) {
if (a < c) {
System.out.println("a is smallest");
}
else {
System.out.println("c is smallest");
}
}
else if (b < c) {
System.out.println("b is smallest");
}
else
System.out.println("clueless");
a is smallest
b is smallest
c is smallest
clueless
6.
Q6: Variables
a,
b, and
c are all declared as characters. If
a='C', b='B', c='A'; What is printed?
if (a < b) {
if (a < c) {
System.out.println("a is smallest");
}
else {
System.out.println("c is smallest");
}
}
else if (b < c) {
System.out.println("b is smallest");
}
else
System.out.println("clueless");
a is smallest
b is smallest
c is smallest
clueless
You have attempted
of
activities on this page.