In addition, a while or do-while loop can lead to an infinite loop while a "for" loop cannot as long as you have specified the condition and increment.
syntax:
Example below can lead to an infinite loopCode:for(initializer; condition; increment) while(condition) do {} while(condition)
orCode:int i = 0; while (i < 5) { printf("%d\n", i); continue; ++i; }
while below won'tCode:do { printf("%d\n", i); continue; ++i; } while (i < 5);
except if you leave the increment blank likeCode:for (int i = 0; i < 5; ++i) { continue; ++i; }
then it will lead to an infinite loop.Code:for (int i = 0; i < 5;) { continue; ++i; }
if-else versus a switch statements are the same except that some programming languages won't let you use switch statements on certain data types. "switch" is more readable if you need to perform a lot of checks so use it in those situations.



Reply With Quote
