Read the following code: n = 2 while(n < 5): print(n) n = n + 1 What output would be produced for the given values of n? 0 1 2 3 4 1 2 3 4 5 2 3 4 2 3 4 5
im confused
the program declares n to be 2 the while loop sets a condition: while n is less than 5, it runs these two lines: print(n) n = n + 1 the first line prints the value of n, then the next line re-assigns n to be n + 1 so you start with n = 2. the function will print 2, then it will re-assign n to be 2 + 1, or 3. since n is now 3, which is still less than 5, the function runs again. keep running the function, writing what it prints out, while the conditions are met.
so this means its C? right?
good, the function starts at 2 and runs its last loop when n = 4, printing n = 4 and re-assigning n as 5 (which makes the loop fail to run again), so it is just 2 3 4
yeah makes sense, ill go with C
Join our real-time social learning platform and learn together with your friends!