Explanation of Solution
a.
//Initialize the variables
int n=1; int k=2; int r=n;
//Check the condition
if (k<n)
{
//Assign k to r
r=k
}
Value of each variable after “if” statement:
n=1, k=2, r=1
Explanation:
The value assigned to variable “n” is “1”, the condition inside the “if” statement fails. As the value of “n” is not greater than “k” and so the value of “r” remains unchanged.
Explanation of Solution
b.
//Initialize the variables
int n=1; int k=2; int r;
//Check the condition
if (n<k)
{
//Assign k to r
r=k
}
//Otherwise
else
{
//Add k+n and store it in r
r=k+n;
}
Value of each variable after “if”statement:
n=1, k=2, r=2
Explanation:
The value assigned to variable “n” is “1”, the condition inside the “if” statement is true. As the value of “k” is greater than “n” and so the value of “r” is changed to “2”.
Explanation of Solution
c.
//Initialize the variables
int n=1; int k=2; int r=k;
//Check the condition
if (r<k)
{
//Assign k to r
n=r
}
//Otherwise
else
{
//Assign n to k
k=n;
}
Value of each variable after “if”statement:
n=1, k=1, r=2
Explanation:
The value assigned to variable “n” is “1”, the condition inside the “if” statement fails. As the value of “r” is not greater than “k” and it moves to the “else” part where the value of “k” is changed to value of “n” that is “1”. In the first statement, the value of “r” is initialized to value of “k” that is “2”.
Explanation of Solution
d.
//Initialize the variables
int n=1; int k=2; int r=3;
//Check the condition
if (r<n+k)
{
//Assign k to r
r=2*n
}
//Otherwise
else
{
//Assign n to k
k=2*r;
}
Value of each variable after “if”statement:
n=1, k=6, r=3
Explanation:
The value assigned to variable “n” is “1”, the condition inside the “if” statement fails. As the value of “r” is not greater than “n+k” and it moves to the “else” part where the value of “k” is changed to value of “2*r” that is “6”. In the first statement, the value of “r” is initialized to “3”.
Want to see more full solutions like this?
Chapter 5 Solutions
Big Java, Binder Ready Version: Early Objects
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education