Engineering Programming (MATLAB/Python) · Problem type

Trace a while loop

Use this for “What is x after while x <= 20, x = 2*x runs from x = 1? How many passes?”

Explore

MATLAB1x = 1;2n = 0;3while x <= 204    x = 2*x;5    n = n + 1;6endPython1x = 12n = 03while x <= 20:4    x = 2*x5    n = n + 1passxnstart10
The first two lines set x to 1 and the pass count n to 0.

Assumptions

•
The test is read before every pass; the body runs only while it holds.
•
MATLAB ends the loop with end; Python ends it where the indent stops.
•
Each row of the table is the values after one pass of the body.
Topic check
See what you know: 5 questions on this topic
New questions each time. Pass to mark it complete.

Related lessons

© 2026 SME. All rights reserved.