What are the semantic differences between a for loop and a while loop? Can you convert a while loop to an equivalent for loop and vice versa? If so, how?
Java
/*If you any query do comment in the comment section else like the solution*/
For Loop:
for(initialization; condition; increment/decrement) {
//statments
}
eg:
for(i=0;i<10;i++) {
//TODO
}
While Loop:
initialization
while(condition) {
//TODO
increment/decrement
}
i = 0;
while(i < 10) {
//TODO
i++;
}
Also you can see that I've written same loop repeating from i = 0 to 10 using semantics of both for and while so it is possible to convert a while loop to a for loop and vice versa
Get Answers For Free
Most questions answered within 1 hours.