What is the difference between this two codes? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is the difference between this two codes?

https://code.sololearn.com/cLxFgw6q4fW8/?ref=app https://code.sololearn.com/cTdSAw6R5uQG/?ref=app The input is the same. So there is no difference between the while loop and the for loop?

19th Aug 2019, 11:20 PM
Felipe Sucupira
Felipe Sucupira - avatar
3 Answers
+ 2
Of course there will be differences,especially when the situation is not printing a pattern. While loop can be used to ensure that a value meets cetain requirements by continuously working on it and then checking. You don't care about how many times the while loop has run and even the INITIAL value. For loop is commonly used to run something for several times. You don't want it to run for more times. In your case,two loops are interchangable as you provide and know the initial value(0) and final value(10). However,in cases like picking a random character and you don't want 0: While (x==0) { Random num=new Random(); x=num.Next(-3,3); } You don't care the times it takes to get the x's value you want and don't know the first picked number is 0 or not .
20th Aug 2019, 1:13 AM
Ketchup🍅
Ketchup🍅 - avatar
+ 4
Yeah there is not much difference in both.
19th Aug 2019, 11:30 PM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 2
There is no difference in you case. If you want to see differences you can use other type of algorith.
19th Aug 2019, 11:46 PM
Andrés Julián González
Andrés Julián González - avatar