0

When to use different loops?

We learnt many loops but not when to use which at when..

12th Nov 2016, 10:45 AM
Namas Thapa
Namas Thapa - avatar
2 Answers
+ 6
for loops: usually use them when you know exactly how many times you need to iterate for instance if you have an array of fixed size and you want to go through it while/do while loops: if unsure how many iterations would require but have a certain end condition that will be met but in general, ine can yse either one for every scenario it just might be more convinient to use one over the other, and that knowledge require some experience
12th Nov 2016, 11:14 AM
Burey
Burey - avatar
+ 4
see.. mainly we have 3 types of loops you should use for loops when linear iteration process. In simple words you already know how many times you need to execute the loop. you should use while loop when you are not sure about iterations but you are sure about condition and you want to set it inside if statement inside loop.. hope you understood and lastly.. do while loop is used when you are sure that this piece of code must be executed once. After that id depends on the condition weather to repeat or not since do while is exit control loop. also, there is this one type of loop called foreach loop in php and js. these are used while dealing with arrays or strings hope you are confident now. follow me to get updates
12th Nov 2016, 11:11 AM
Venus Vavadiya
Venus Vavadiya - avatar