Q&A

Q1:How do you choose between if...else and switch?
A1: If there are more than just one or two else clauses, and all are testing the same value, consider using a switch statement.
Q2:How do you choose between while and do...while?
A2: If the body of the loop should always execute at least once, consider a do...while loop; otherwise try to use the while loop.
Q3:How do you choose between while and for?
A3: If you are initializing a counting variable, testing that variable and incrementing it each time through the loop, consider the for loop. If your variable is already initialized and is not incremented on each loop, a while loop might be the better choice.
Q4:Is it better to use while(1) or for(;;)?
A4: There is no significant difference.

Get Sams Teach Yourself C++ in 24 Hours, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.