- 3

I am not understanding the loop?What is its function and coding method?

30th Jul 2019, 3:39 AM
Siddharth bore
4 Respostas
+ 1
**My post was too long so I separated into two. This is page 1** The ability to loop is one of the most powerful aspects of a computer. You will almost always need to incorporate if not one, but many loops in your programs. I see you’re studying C++ which I havent learned so I cant give you a code example, but one example is looping through a list (which you will most likely do often). Consider you have a program that requires the user to create a username. You will need to store those usernames in a file or list, now when a new user goes to create an account and they enter their desired username, the program needs to check whether or not that username already exists, how does it do that? it will grab that list of stored usernames and compare the entered username to each element in the list of stored usernames, now in order for it to do that it will need to iterate(loop) through every element in the list of usernames and say “does the entered username match this element in the list?” and if it doesnt it will move to the next element and ask that same question until it comes to the conclusion that either that username exists or it doesnt. Without the loop, that task would be a whole lot more difficult (imagine if there are 100,000 usernames to check!) typing out a condition to check every element in that list would take around 100,000 or more lines of code rather than having a loop that does it in less than 10 lines of code. I’ll give you a pseudocode example: We have a list of 5 taken usernames we’ll assign them the values of [“a”, “b”, “c”, “d”, “e”] now a user goes to enter a desired username, lets say they enter the value “e”. now WITHOUT a loop, checking if that username exists would look something like this:
30th Jul 2019, 4:28 AM
Jake
Jake - avatar
+ 1
if “e” equals the first element in the list “a”, then return username exists if “e” equals the second element in the list “b”, then return username exists if “e” equals the third element in the list “c”, then return username exists if “e” equals the fourth element in the list “d”, then return username exists if “e” equals the last element in the list “e”, then return username exists else return the username does not exist and the user can use that desired username now imagine writing that 100,000 times!! and needing to update it everytime a new user is created here’s how we can utilize a loop to automate that menial task: 1.the current index = 0 (the first element in the list which is “a”) 2.while the value of current index is not the last index in the list of usernames then do steps 3,4,5 3. if the current index in list of username matches the entered username, then return username exists and break out of this loop 4. if we have reached the end of the list (“e”) and havent found the username, then return username does not already exist 5. add one to the value of current index and go back to step 2 Now, with the loop, those same 5 lines of code will also work for a list of 100,000 usernames! and that my friend is the beauty of the loop.
30th Jul 2019, 4:28 AM
Jake
Jake - avatar
0
Thankx.
30th Jul 2019, 10:12 AM
Siddharth bore