Lecture 2-3: C++ Basic Constructs
Lecture 2-3: C++ Basic Constructs
• Short-circuit evaluation
– (x >= 0) && (y > 1)
– Be careful with increment operators!
• (x > 1) && (y++)
– do-while
• Least flexible
• Always executes loop body at least once
– for
• Natural "counting" loop
while Loops Syntax
do-while Loop Syntax
while Loop Example
• Consider:
count = 0; // Initialization
while (count < 3) // Loop Condition
{
cout << "Hi "; // Loop Body
count++; // Update expression
}
– Loop body executes how many times?
do-while Loop Example
count = 0; // Initialization
do
{
cout << "Hi "; // Loop Body
count++; // Update expression
} while (count < 3); // Loop Condition