+ 1

We can write while loop, same as for loop right??? Like while (i=0; i <=10 ;i++).is this correct???

22nd Jul 2018, 3:47 PM
Rikshith U Uchil
Rikshith U Uchil - avatar
6 ответов
+ 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
22nd Jul 2018, 5:13 PM
Babak
Babak - avatar
+ 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++; }
22nd Jul 2018, 3:58 PM
ᴋᵘⁿᵃˡ
ᴋᵘⁿᵃˡ - avatar
+ 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
22nd Jul 2018, 4:23 PM
Guillem Padilla
Guillem Padilla - avatar
+ 2
Rikki It is incorrect. In parentheses (after while keyword) should be boolean expression - some condition or function (method), which returns boolean value.
22nd Jul 2018, 4:01 PM
Martin Bulíř
+ 1
for example: while(a<10) would be the same as: for(;a<10;)
22nd Jul 2018, 3:57 PM
Jeremy
Jeremy - avatar
- 1
It is incorrect in while loop we can add only one condition
20th Mar 2020, 4:36 AM
Abdul Rahiman Masood
Abdul Rahiman Masood - avatar