C++ Loops and File Handling: A Deep Dive
Classified in Computers
Written on in
English with a size of 4.31 KB
C++ Loops: While, Do-While, and For
While Loop Format
while (condition)
{
statements(s);
}
How While Loop Works
If the condition is true, the statement(s) are evaluated again. If false, the loop is exited.
While Loop Example
int val = 5;
while (val >= 0)
{
cout << val << " ";
val = val - 1;
}
A while loop is a pretest loop (the condition is evaluated before the loop executes).
- If the condition is initially false, the statement(s) in the body of the loop are never executed.
- If the condition is initially true, the statement(s) in the body will continue to be executed until the condition becomes false.
The loop must contain code to allow the condition to eventually become false so the loop can be exited. Otherwise, you have... Continue reading "C++ Loops and File Handling: A Deep Dive" »