+ 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?
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 .
+ 4
Yeah there is not much difference in both.
+ 2
There is no difference in you case. If you want to see differences you can use other type of algorith.