give the diff forms of looping statements and their structure/ syntax
The two most efficient loops are the while (or do-while), and for loops. while: while (statement) { do action; } do-while: do { do action; } while (statement); for: for (int i = 0; i < someNumber; i++) { do actions; }
here's another one; the goto loop: label: do_something(); if (something) goto label; or you can forget the if condition and go for an infinite loop: label: do_something(); goto label;
'goto' is not a looping construct in the strictest sense, but it's a primitive control construct.
which may be used to construct loops, along with labels.
People will want to hurt you if you use gotos in the manner I outlined above; goto is considered harmful since it creates spaghetti code and messed up control flow and unstructured programs. I would probably use goto to accomplish deep-nested early exits from formal loops, and that's it. You should do your best to re-structure your code so that you won't have to use a goto.
Also, you've got the \[\small\text{for each}\]construct in modern languages, which traverses items in collections. In python: for item in biglist: iterates through the list biglist, with 'item' sequentially referencing elements in biglist.
Python- while True: ##Do stuff that example will occur forever until you say 'break' x=5 while x > 0: x = x - 1 That will end once 'x' variable reaches 0
Join our real-time social learning platform and learn together with your friends!