Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,631 members, 7,816,604 topics. Date: Friday, 03 May 2024 at 01:49 PM

Pls Explain This Code For Me - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Pls Explain This Code For Me (561 Views)

Pls Explain This For Me / Please,Help Explain This Code In C / Can Someone Help Me Explain This Code Line? (2) (3) (4)

(1) (Reply) (Go Down)

Pls Explain This Code For Me by manuel4real(m): 5:34pm On Jun 08, 2020
i = 1
while i <=5:
print(i)
i = i + 1

What does the line four stands for?

Thanks in anticipation
Re: Pls Explain This Code For Me by phr33man(m): 5:50pm On Jun 08, 2020
manuel4real:
i = 1
while i <=5:
print(i)
i = i + 1

What does the line four stands for?

Thanks in anticipation

To increase the value of i to avoid infinite loop. If not, it will continue printing 1.

But line 4 increases the value of i which is 1 at initiation so the next output will be 2 after first. Then, 3, 4 and 5. Then the program stops
Re: Pls Explain This Code For Me by stanliwise(m): 5:53pm On Jun 08, 2020
manuel4real:
i = 1
while i <=5:
print(i)
i = i + 1

What does the line four stands for?

Thanks in anticipation
TL; DR
it is a loop. It means after line 1 when the program reach line 2 it checks for the condition and if true continue to line 3 and 4(end of loop) and when it get to 4 it return back to 2. This steps continue until line 2 fails.

COMPLETE EXPLANATION
line 2
While i <= 5
This mean keep the loop Line 2-4 running as much as i is still less than or equal to 5. This mean the i <= 5 must be true but when it becomes false the interpreter skip the entire loop and goto line 5

line 4
i = i + 1

This is the line that ensure that the loop (Line 2 -4) does not continue forever. On each time the loop keep running it keep I increasing with +1. line 2 fails when i becomes 6 which means i = 5 is false and i < 5 is also false.
By this time the program skip to line 5
(Or stops if none exist) and keep going.

Without line 4, line 2 will never fail(since i will not increase will always be less than or equal to 5) and the loop will run forever. It will halt your system and the program will be aborted by force.

Note:
i = i + 1
This line means take the former value of i add 1 to it and put in back into i. In simpler words, it it increasing i by one. It runs i + 1 command first then whatever the answer is, it places it in i I.e i = (result of i + 1 )

So that is loop that start at line 2 to 4 continually until line 2 fails the condition and the it proceed.
Re: Pls Explain This Code For Me by SUPREMOTM: 6:06pm On Jun 08, 2020
manuel4real:
i = 1
while i <=5:
print(i)
i = i + 1

What does the line four stands for?

Thanks in anticipation

You have a loop (I'm assuming you know what a loop is/does)

Line 1: You declare a loop control variable, i

Line 2: You define a condition that must be fulfilled for the loop execution to continue

Line 3: If the condition in line 2 is fulfilled, you do something (I'm assuming you know what the print function does)

Line 4: Line four is a condensed/short form of 2 actions.

#1 First, you increment the loop control variable by 1 (Right hand side), i.e you increased the value of i by 1.

#2 Secondly, you re-assign the new value to the variable i (in line 1, you set i = 1, but in line 4, you have changed/increased it to 1+1)

After line 4, the loop execution will return to line 2, so, the next time the loop condition is checked, the value of i will be 2. The loop will continue to run until i has increased to 5 (as stated in the loop condition), then, the condition will no longer be satisfied and execution will move out of the loop.



So, your print(i) function will output the various values of i, that is 1,2,3 and 4, then code execution will move to the next line after your loop.

1 Like

Re: Pls Explain This Code For Me by power2000: 6:56pm On Jun 08, 2020
manuel4real:
i = 1
while i <=5:
print(i)
i = i + 1

What does the line four stands for?

Thanks in anticipation
for an infinite loop, except the op wants it to be finite.
then, he is also supposed to include the other parameters of i = non-negative integer to achieve that
Re: Pls Explain This Code For Me by stanliwise(m): 7:33pm On Jun 08, 2020
power2000:

for an infinite loop, except the op wants it to be finite.
then, he is also supposed to include the other parameters of i = non-negative integer to achieve that
can you explain in simpler terms what you mean?

1 Like

Re: Pls Explain This Code For Me by Barney001: 7:46pm On Jun 08, 2020
manuel4real:
i = 1
while i <=5:
print(i)
i = i + 1

What does the line four stands for?

Thanks in anticipation
Line 4: it means keep adding 1 to the original value of i(1)

You can use i += 1...much sleeker cool

(1) (Reply)

Windows 13 Iso / Wpcontents.net Review - Premium Wordpress Themes And Plugins Platform / Coding With HTML And CSS Using My Phone

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 18
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.