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:
Code:
for(initializer; condition; increment)
while(condition)
do {} while(condition)
Example below can lead to an infinite loop
Code:
int i = 0;
while (i < 5)
{
printf("%d\n", i);
continue;
++i;
}
or
Code:
do
{
printf("%d\n", i);
continue;
++i;
} while (i < 5);
while below won't
Code:
for (int i = 0; i < 5; ++i)
{
continue;
++i;
}
except if you leave the increment blank like
Code:
for (int i = 0; i < 5;)
{
continue;
++i;
}
then it will lead to an infinite loop.
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.