
Explanation of Solution
a.
Given statements:
//declare the variables and assign the values
int i = 0;
int j = 10;
int n = 0;
//check "i" is less than "j"
while(i < j)
{
//increment the value
i++;
//decrement the value
j--;
//increment the value
n++;
}
Trace table for the above loops:
i | j | n |
0 | 10 | 0 |
1 | 9 | 1 |
2 | 8 | 2 |
3 | 7 | 3 |
4 | 6 | 4 |
5 | 5 | 5 |
Explanation:
Here, the “i” value is “0”, “j” value is “10” and “n” value is “0”.
- In first iteration of “while” loop checks “i < j” which means “0 < 10”. The condition becomes true, so it increment the “i” value, i = 1, decrement the “j” value, j = 9 and also increment the “n”, n = 1...
Explanation of Solution
b.
Given statements:
//declare the variables and assign the values
int i = 0;
int j = 0;
int n = 0;
//check "i" is less than "10"
while(i < 10)
{
//increment the value
i++;
//calculate the "n" value
n = n + i + j;
//increment the value
j++;
}
Trace table for the above loops:
i | j | n |
0 | 0 | 1 |
1 | 1 | 4 |
2 | 2 | 9 |
3 | 3 | 16 |
4 | 4 | 25 |
5 | 5 | 36 |
6 | 6 | 49 |
7 | 7 | 64 |
8 | 8 | 81 |
9 | 9 | 100 |
Explanation:
Here, the “i” value is 0, “j” value is 0 and “n” value is 0.
- In first iteration of “while” loop checks “i < 10” which means “0 < 10”. The condition becomes true, so it increment the “i” value, i = 1, calculate “n” value which means (0 + 1 + 0 = 1) and also increment the “j”, j = 1.
- In second iteration of “while” loop checks “i < 10” which means “1 < 10”. The condition becomes true, so it increment the “i” value, i = 2, calculate “n” value which means (1+ 2 + 1 = 4) and also increment the “j”, j = 2.
- In third iteration of “while” loop checks “i < 10” which means “2 < 10”. The condition becomes true, so it increment the “i” value, i = 3, calculate “n” value which means (4+ 3 + 2 = 9) and also increment the “j”, j = 3.
- In fourth iteration of “while” loop checks “i < 10” which means “3 < 10”...
Explanation of Solution
c.
Given statements:
//declare the variables and assign the values
int i = 10;
int j = 0;
int n = 0;
//check "i" is greater than "0"
while(i > 0)
{
//decrement the value
i--;
//increment the value
j++;
//calculate the "n" value
n = n + i - j;
}
Trace table for the above loops:
i | j | n |
10 | 0 | 8 |
9 | 1 | 14 |
8 | 2 | 18 |
7 | 3 | 20 |
6 | 4 | 20 |
5 | 5 | 18 |
4 | 6 | 14 |
3 | 7 | 8 |
2 | 8 | 0 |
1 | 9 | -10 |
Explanation:
Here, the “i” value is 10, “j” value is 0 and “n” value is 0.
- In first iteration of “while” loop checks “i > 0” which means “10 > 0”. The condition becomes true, so it decrement the “i” value, i = 9, increment the “j”, j = 1 and calculate “n” value which means (0 + 9 - 1 = 8).
- In second iteration of “while” loop checks “i > 0” which means “9 > 0”. The condition becomes true, so it decrement the “i” value, i = 8, increment the “j”, j = 2 and calculate “n” value which means (8 + 8 - 2 = 14).
- In third iteration of “while” loop checks “i > 0” which means “8 > 0”. The condition becomes true, so it decrement the “i” value, i = 7, increment the “j”, j = 3 and calculate “n” value which means (14 + 7 - 3 = 18).
- In fourth iteration of “while” loop checks “i > 0” which means “7 > 0”...
Explanation of Solution
d.
Given statements:
//declare the variables and assign the values
int i = 0;
int j = 10;
int n = 0;
//check "i" is greater than "0"
while(i != j)
{
//increment the "i" by 2
i = i + 2;
//decrement the "j" by 2
j = j – 2;
//increment the value
n++;
}
Trace table for the above loops:
i | j | n |
0 | 10 | 0 |
2 | 8 | 1 |
4 | 6 | 2 |
6 | 4 | 3 |
8 | 2 | 4 |
... |

Want to see the full answer?
Check out a sample textbook solution
Chapter 6 Solutions
EBK BIG JAVA: EARLY OBJECTS, INTERACTIV
- 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





