Stats: 3,185,969 members, 7,927,895 topics. Date: Thursday, 22 August 2024 at 03:14 AM |
Nairaland Forum / Science/Technology / Programming / Pls Explain This For Me (1915 Views)
Pls Explain This Code For Me / Please,Help Explain This Code In C / Can Someone Explain This Line Of Code To Me (2) (3) (4)
Pls Explain This For Me by manuel4real(m): 8:45am On Jun 07, 2020 |
Good day nairalanders, I started learning Python some weeks ago and I'm kinda having problems in understanding 'loop'. Like what is it used for What situation do we need to use it Pls can someone help me out, it's really drawing me back |
Re: Pls Explain This For Me by Nobody: 8:48am On Jun 07, 2020 |
manuel4real:Loops are used to repeat a particular set of instructions more than once. 2 Likes |
Re: Pls Explain This For Me by iAmTheJoker: 9:30am On Jun 07, 2020 |
Loops are used to repeat or iterate over a set of instructions as long as the condition holds Take the while loop for example (1) x = 1 (2) while i <=5: // this is the condition (3) print(i) (4) x = x + 1 So first the value of x = 1, then in line 2 it checks if x which is 1 is less than or equal to 5, in line 3 it then prints 1, on the 4th line x = x + 1, is the same as x = 1 + 1, which means x is now 2, it goes back to the condition to check if 2 is less than or equal to 5 , then it prints 2, it increments 2 to 3, it repeats this until i meets the condition (i<=5) The statements inside it are repeatedly executed, as long as the condition holds. Loops can be used in game development , you need a game loop that repeats the drawing of board, movement of items, input etc. Hope it helps 1 Like |
Re: Pls Explain This For Me by manuel4real(m): 9:31am On Jun 07, 2020 |
SegFault:thank you very much sir. |
Re: Pls Explain This For Me by Sapeleboy911(m): 9:52am On Jun 07, 2020 |
Loop is like running around circles until a given condition is met. Take for instance i asked you to buy me fish, you went and bought yam, i asked you to return the yam and buy me fish, you returned it and bought beans, I asked you to return the beans and buy me fish , you returned the beans and bought rice. Not until you buy me the fish I sent you. You will continue to repeat the errand that I sent you. Though I can say if you run the errand for certain amonunt of times like 2,3, or any amount of time without buying the fish I sent you. I can ask you to stop. That is what you call break in loops. To gain more understanding on loops read the book. 'Automate the boring stuff with python'. Take your time and read flow control on chapter 2. 1 Like |
Re: Pls Explain This For Me by stanliwise(m): 10:40am On Jun 07, 2020 |
manuel4real:for example if you need to print 1 - 1000. Can you help with any method of doing this without using a loop? If you were asked to print a calendar, is there any way to do it without a loop. If I give an array of numbers with around 250 element and I told you to multiply each by 5. Can you suggest any other way to accomplish this task without loop. Loop is the heart of computing, computer are better than us in speed and repetition and accuracy. This three hallmark is the essence why anyone would learn how to program and use the advantage of a computer. Many apps and program you use today make extensive use of repetition and shockingly your OS(windows/Linux) is a one big Unending loop that run different statement on each iteration. So also a loop is what keeps a system on and running. A loop is what keeps a server running. If you can solve a task with a loop then you understand computing. 3 Likes 1 Share |
Re: Pls Explain This For Me by Coder2Client(m): 11:11am On Jun 07, 2020 |
1-100 I want to read the numbers in that range. I can follow 3 ways: while, do while and for loops. this simply mean I want to read from 1 to 100, reading this involves iteration i.e repeating. so, you are going start from 1 which is the starting point to 100 which is the target point. I am going to use Java as I'm a Java dev. let's go. for(int i=1; i<=100; i++){ The above code will print: Number : 1 to Number : 100 that is for for loop int i =1; this is while loop int i =1; this for do while loop. For the above code snippets you gonna get the same result that is, Number 1 - Number 100 There is another loop called foreach or enhancedfor loop for(int var : refVar){ that's the syntax. 1 Like |
Re: Pls Explain This For Me by stanliwise(m): 11:38am On Jun 07, 2020 |
Coder2Client:Op wasn’t asking for syntax or implementation but justification for a programmer to understand a loop. But anyway I consider your approach a pragmatic one though |
Re: Pls Explain This For Me by Donald3d(m): 1:29pm On Jun 07, 2020 |
manuel4real: Imagine you are trying to fill a bucket with a bowl, taking water from a big drum . You would keep scooping from the drum with the bowl until the bucket is full. So a loop is a repetitive action until the aim is achieved, once the aim is achieved, the action (in this case, filling the bucket) is terminated. This is a layman simplified real-world explanation . In coding, an example where it can be used is : For example you have a system where you want to sum up all the scores of all the courses a student does : You can loop through all the scores and keep adding it up until its done . Although there are better ways of adding this up instead of using a loop, but that's one way you can use it. 2 Likes |
Re: Pls Explain This For Me by stanliwise(m): 1:33pm On Jun 07, 2020 |
Donald3d:I don’t think there is a better way other than loop. Loop is the backbone of working with collections. It can be decorated to what we call iteration and the rest but the inner structure remains a loop. |
Re: Pls Explain This For Me by nwele2017: 1:40pm On Jun 07, 2020 |
manuel4real:loops are use for truncating iterable data structure. a perfect example is the for loop. It allows you to list all elements in an iterable data structure. to illustrate this for you, A list is a perfect example of python data structure. list=[a,b,c,d] Now with the for loop (an example of loop) we can printout all the element(data) in that data structure (list) by doing the following: for i in list: print(i) this print out all the elements inside list like this: a b c d |
Re: Pls Explain This For Me by Donald3d(m): 1:43pm On Jun 07, 2020 |
stanliwise: I know loops are great but as one goes on in programming, you would have to find quicker ways to solve problems . Something as simple as sum() would sum up the scores, depending on the data type or origin. |
Re: Pls Explain This For Me by stanliwise(m): 1:48pm On Jun 07, 2020 |
Donald3d: ![]() ![]() Maybe you would check the internal structure of sum(); Now matter how advance you’re in programming, no one ever evade loop |
Re: Pls Explain This For Me by Tim1212(m): 1:51pm On Jun 07, 2020 |
As a beginner in programming, to understand how loop works. You need to understand your iniquity in algebra. Loop use more of logical operation to make decisions. I will advice you go back to your basic algebra and study inequities. |
Re: Pls Explain This For Me by stanliwise(m): 2:10pm On Jun 07, 2020 |
Tim1212:please stay away from this advise 3 Likes |
Re: Pls Explain This For Me by Donald3d(m): 2:54pm On Jun 07, 2020 |
stanliwise: You are being unnecessarily analytical, in the real world nobody cares about the internal structure of things, your result is what matters. Why re-inventing the "internal structure", when it has already been done for you. If you look under the hood, everything has an internal structure, so should we start re-inventing all of them ? A good programmer keeps things as simple as possible, as long as the result is the same and it doesn't affect performance. Besides, I never said loops are bad, I use them practically everyday. My point is in some cases there are faster ways to do things than using loops directly. |
Re: Pls Explain This For Me by stanliwise(m): 2:56pm On Jun 07, 2020 |
Donald3d:Since you mentioned the word “directly” then it is fine. Loop is the heart of programming language |
Re: Pls Explain This For Me by Tim1212(m): 2:57pm On Jun 07, 2020 |
Re: Pls Explain This For Me by Nobody: 3:31am On Jun 08, 2020 |
stanliwise:You would have to at sometime especially when you understand computer architecture, for example loops are tasking to the CPU cache and CPU pipelining though CPU caches are now larger than life 64kb is a lot trust me. But if you get to work on embedded systems you might face this problem. |
Re: Pls Explain This For Me by manuel4real(m): 12:35pm On Jun 08, 2020 |
Thank you all, I now understand it perfectly |
Re: Pls Explain This For Me by stanliwise(m): 12:39pm On Jun 08, 2020 |
SegFault:without loop there is no need for a computer. Simple and short. The three hallmark for a computer till day is speed, accuracy and repetition. Aside this human excel in all. |
Re: Pls Explain This For Me by Nobody: 5:32pm On Jun 08, 2020 |
stanliwise:I am not saying that loops aren't useful I am saying that you can't get the performance you need all the time using loops. Sometimes you need to cut down on the use of loops. |
Re: Pls Explain This For Me by stanliwise(m): 5:42pm On Jun 08, 2020 |
SegFault:suggest an alternative for loop please? If for example I need to write one to one thousand. I am expecting you to say recursion but you wouldn’t say that. would you? |
Re: Pls Explain This For Me by Nobody: 8:32pm On Jun 08, 2020 |
stanliwise:Please read and understand my mention once again. I simply said sometimes you'd have to use some other method other than loops. Besides recursion is worse than loops, it consumes stack space. |
Re: Pls Explain This For Me by stanliwise(m): 9:08pm On Jun 08, 2020 |
SegFault:And what I am simply telling you is that there is nothing that replaces loop. Loop is among the bedrock of computer. I mean nothing, like simply nothing. |
Re: Pls Explain This For Me by Nobody: 8:49am On Jun 09, 2020 |
stanliwise:Chai you still don't get what I am saying fashi. Funny thing about computers is that there is no provision for a loop instruction except for Intel processors (which no one uses). Back then when caches were small people had to cut down on the way they use loops (read Art of Assembly by Randall Hyde if you think this is a lie) when there was an alternative they chose it over loops. Now it is even better because CPUs are smart enough to know how many times a piece of code is run. If you ever do embedded system programming and you meet a microcontroller with a small cache you will understand finally what I mean. |
Re: Pls Explain This For Me by stanliwise(m): 8:52am On Jun 09, 2020 |
SegFault:ok we would leave it at this places but for your info I do Embedded system programming. Majorly Arduino. Using C++ |
Re: Pls Explain This For Me by Nobody: 9:58am On Jun 09, 2020 |
stanliwise:Okay good. You a hobbyist or a professional. |
Re: Pls Explain This For Me by IceColdVeins(m): 10:11am On Jun 09, 2020 |
see no mind all these people bombarding your thread with syntax. Basically 'loop' is used to repeat a particular set of instructions. For instance, if you want to design a program that asks for a user's passcode to gain access, The 'loop' can be used to repeatedly ask the user to enter the correct passcode until the user gets the passcode right. So basically, if the user enters a wrong passcode, the program will ask for passcode again and if the user still enters a wrong passcode, the program will keep asking for passcode until user gets it right. Danke! manuel4real: 1 Like |
Re: Pls Explain This For Me by stanliwise(m): 10:50am On Jun 09, 2020 |
SegFault:My course of study is computing science actually. Professionally I am a web dev going to app dev but do embedded system as a hobby now sha |
Re: Pls Explain This For Me by Nobody: 5:44pm On Jun 09, 2020 |
stanliwise:Hmm good. Downloaded an embedded systems book sometime ago when I don't even have one ![]() ![]() |
Re: Pls Explain This For Me by stanliwise(m): 10:10pm On Jun 09, 2020 |
SegFault:its cheap and easy to get |
Is PYTHON Better Than PHP In Web Development? / Installing Oracle 10g On Window Xp / Pls Anyone With Knowledge Of Cobol Basics Here
(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. 57 |