Trace a for loop or while loop iteration by iteration, showing the loop variable and accumulator after each pass, or predict values first.
You are a programming tutor who has noticed that a student can usually explain what a loop is supposed to do in general terms while still getting its final result wrong, and the gap between those two things almost always closes the moment they trace the loop iteration by iteration instead of reasoning about it as one continuous blur. My loop is: [CODE] If I left [CODE] blank, ask me to paste an actual for loop or while loop before doing anything else instead of inventing one. Work in [MODE:select:trace it for me,let me predict each iteration first] mode. Before tracing anything, identify the loop variable or condition controlling how many times the loop runs, and identify any variable declared outside the loop that the loop body modifies, since that is almost always an accumulator whose changing value is the actual point of the loop. If I chose trace it for me, walk through the loop iteration by iteration. For each pass, state the current value of the loop variable, evaluate the loop's continuation condition and state whether it is still true, then show exactly what the loop body does on this pass and the resulting new value of any accumulator or modified variable. Continue until the condition evaluates false and the loop ends, then state the final value of every variable the loop touched, on its own line, clearly labeled. If I chose let me predict each iteration first, state the loop variable's starting value and ask me to predict what will happen on the very first pass, specifically the value the accumulator or modified variable will have immediately after that pass completes. Wait for my answer before revealing anything. Once I respond, reveal the actual result for that pass, confirm or correct my prediction, and if I was wrong, explain concretely which part of the loop body produced a different result than what I predicted. Then move to the next iteration and repeat the same predict-then-reveal pattern, continuing until the loop naturally ends. If my loop has an off-by-one issue, running one time too many or too few compared to what its structure suggests it should, or if it never terminates because the condition can never become false given how the loop body modifies the controlling variable, point that out explicitly as its own note, separate from the normal trace, since that is a structural problem with the loop itself rather than something the trace alone will resolve.
Use this prompt anywhere
10,000+ expert prompts for ChatGPT, Claude, Gemini, and wherever you use AI.
Get Early AccessA student can usually describe what a loop should do in general terms and still get its final result wrong, and that gap almost always closes the moment the loop gets traced iteration by iteration instead of reasoned about as one blur.
Paste your own [CODE], a for loop or while loop, and choose how to work through it. Trace-it-for-me mode walks every pass, stating the loop variable's current value, whether the continuation condition still holds, what the loop body does on that pass, and the resulting new value of any accumulator, ending with every touched variable's final value clearly labeled. Predict-first mode is the more active version, stating the starting value and asking you to predict the accumulator after the very first pass before revealing anything, then repeating that pattern through every iteration.
Either mode identifies any accumulator variable up front, since a value changing across iterations outside the loop is almost always the actual point of the loop. If your loop has a structural problem, an off-by-one running one pass too many or too few, or a condition that can never become false given how the body modifies it, that gets flagged as its own note, separate from the trace, since that's a structural issue tracing alone won't resolve. Run it in the Dock Editor for a set of traced loops, pair with the recursion explainer and tracer to trace a function calling itself, or the higher-order function practice generator for the map, filter, and reduce alternative.
Drop your for loop or while loop into [CODE]. Leaving it blank prompts a request for a real loop instead of an invented example.
Set [MODE] to trace it for me for a complete walkthrough, or let me predict each iteration first to test your own understanding before checking it.
Any variable declared outside the loop that the loop body modifies gets identified before tracing begins, since that's almost always the actual point of the loop.
In predict mode, state your guess for the accumulator's value after each pass before it's revealed. In trace mode, watch each pass's loop variable, condition check, and result unfold.
If your loop runs one time too many or too few, or can never terminate given how the condition variable changes, that gets flagged explicitly as a structural issue.
Paste a basic for loop from a class assignment and watch every iteration traced, seeing exactly how the loop variable and accumulator change on each pass.
Trace your loop iteration by iteration to find the exact pass where the accumulator's value first diverges from what you expected.
Use predict-first mode to state what you think will happen at each iteration before checking, building genuine tracing skill instead of passively reading a finished walkthrough.
Paste a loop that never seems to end, and get flagged explicitly if the loop's condition can never become false given how the body modifies the controlling variable.
Discover more prompts that could help with your workflow.
Build a small working program that calls a real public API, with the request, response, and API key setup explained step by step.
Explain a core functional programming idea, such as pure functions or immutability, with a broken code example and its fixed version shown side by side.
Explain a built-in math function, square root, power, absolute value, or rounding, covering math and syntax, then generate practice calls to predict before revealing results.
10,000+ expert-curated prompts for ChatGPT, Claude, Gemini, and wherever you use AI. Our extension helps any prompt deliver better results.