+ 1
We can write while loop, same as for loop right??? Like while (i=0; i <=10 ;i++).is this correct???
6 Respuestas
+ 5
Rikki
Normally, you would use "for loop" for a known and "while loop" for an unknown number of iteration. But, they can be used interchangeably with a bit trick. Let's say you want to implement a for loop using while keyword. You might end up to something like this
int i = 0;
while ( i <= 10 && ++i )
cout << i - 1 << endl;
Output:
0
1
2
3
4
5
6
7
8
9
10
+ 5
No both have different syntax
For loop
var i;
for (i = 0; i < 10; i++) {
}
While loop
var i;
i = 0;
while (i < 10) {
i++;
}
+ 4
Hi,
they have differences:
For syntax:
for (init; condition; increment/decrement) {
}
While syntax:
while(condition){
}
An important difference is that for should be used when you know how much times the loop will do.
While should be used when you don't know how many loops will do.
Hope it helps you
+ 2
Rikki
It is incorrect.
In parentheses (after while keyword) should be boolean expression - some condition or function (method), which returns boolean value.
+ 1
for example:
while(a<10)
would be the same as:
for(;a<10;)
- 1
It is incorrect in while loop we can add only one condition