CS1 Lecture 3(?)
Tuesday, the 2th of December, 2025
std::cout and std::cin)Say we have our basic hello world program (compiled with gcc main.cpp -lstdc++ -o a.out):
What if we want to print Hello World!:
What’s wrong with this approach?
Lets take a look as a while loop that prints Hello World 5 times:
Notice that this is:
goto statement - these should be used sparinglyContinue / break is not unique to this style of loop, and is applicable to all loops.
Lets do a side-by-side conparison of the while and do-while loop styles:
Both programs produce the same output, and use roughly the same lines of code.
From this SO post:
I’ve been programming for a while now (2 years work + 4.5 years degree + 1 year pre-college), and I’ve never used a do-while loop short of being forced to in the Introduction to Programming course. I have a growing feeling that I’m doing programming wrong if I never run into something so fundamental.
So, when should we use a do-while loop?
Here is our same “Hello World” program using a traditional C++ for loop:
Notice the different components separated via ;: - Init-statement - Condition - Expression - Loop body / statement
You can include multiple / zero expressions within each component. Expressions are separated via ,
(;;) kind of looks like a spider…TODO
TODO