← Back
Tracer 1 - While Loops
Instructions
PDF Instructions
For each question, complete the following:
- Create the loop from the prompt or trace the loop provided.
- Show your work tracing the variable(s). Assume the variables are named n, m, etc..
- Answer the following questions:
- How many iterations are performed?
- What is/are the final value of the variable(s) when the loop terminates?
- What is the output, if any?
Example
- Create a while loop that displays the numbers from 5 to 25 in increments of 4.
int n = 5;
while (n <= 25)
{
Console.Write(n + ",");
n = n + 4;
}
- n:
5, 9, 13, 17, 21, 25, 29
- Number of iterations: 6
- Final value of n: 29
- Output: 5,9,13,17,21,25
Questions
- Create a while loop that displays the numbers from 1 to 10.
- Create a while loop that displays the numbers from 27 down to 15.
- Create a while loop that displays the numbers 1 to 30 in increments of 3.
- Create a while loop that displays the numbers from 5 to 50 in increments of 5.
- Trace the following loop:
int n = 10;
while (n <= 45)
{
n = n * 2;
n = n - 5;
Console.WriteLine(n);
}
- Trace the following loop:
int n = 1;
while (n < 100)
{
n = n + 25;
n = n - 10;
if (n % 2 == 0)
{
Console.WriteLine(n);
}
}