Control flow: if, else and while
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
A program runs its lines one after another, top to bottom, until a control-flow statement changes the order. Follow a twelve-line countdown as an arrow marks the line that runs: a while loop repeats its body while n > 0 is true, an if/else picks one of two lines to print on each pass, and the loop stops when its condition turns false.
5 أسئلة سريعة
اختر إجابة لتعرف السبب. تُحفَظ نتيجتك مع تقدّمك.
01n holds 3. Is the condition n > 0 true or false, and what does a while loop do with the answer?
3 > 0 is true. A while loop tests its condition before every pass and runs the body each time the answer is true.
02In if (n == 1) ... else ..., n holds 2. What runs?
2 == 1 is false, so the line under the if is skipped and the else part runs. An if/else always runs exactly one of its two paths.
03What is the difference between = and == in C++?
n = n - 1; stores a new value in n. n == 1 asks whether n equals 1 and gives a bool, true or false.
04What would happen if the line n = n - 1; were removed from the countdown?
A loop stops only when its condition turns false. Without a line that changes n, 3 > 0 stays true forever: an infinite loop that prints 3 again and again.
05The countdown prints 3, 2, last and Go!. How many times does the while condition n > 0 get tested?
It is tested before each of the three passes (n = 3, 2 and 1, all true) and once more when n is 0. That last test is false, which ends the loop.
المسرد
- Control flow
- The order in which a program's statements run. By default top to bottom; if, else and loops change it.
- Condition
- An expression whose answer is true or false, such as n > 0 or n == 1. if and while use one to decide what runs next.
- bool
- The C++ type with exactly two values, true and false. Comparisons such as > and == give a bool.
- if / else
- A choice: if the condition is true, the first statement runs; otherwise the else statement runs. Exactly one of them runs.
- while loop
- A loop that tests its condition before every pass and runs its body again and again while the condition is true.
- Loop body
- The statements inside a loop's { }, run once on every pass.
- Iteration
- One pass through a loop's body. The countdown's loop makes three iterations.
- Infinite loop
- A loop whose condition never becomes false, so it never stops. Usually a bug, often a forgotten update like n = n - 1.
الملاحظات والمصادر
- An if or else without braces controls only the one statement after it, as on lines 6 and 8. Many style guides suggest always writing braces { } so that adding a second line later cannot slip outside the if.
- = stores a value (assignment); == compares two values and gives true or false. Writing if (n = 1) by mistake stores 1 in n; compilers warn about it with -Wall.
- Reaching the closing } of main without a return statement is the same as return 0;, so this program ends with exit status 0 (success). This rule applies only to main.
- The same countdown can be written as a for loop, which keeps the start, the test and the step on one line: for (int n = 3; n > 0; n = n - 1) { ... }. C++ also has do-while, switch, break and continue.
- The program on the card is real, standard C++. Build it with g++ -std=c++20 -Wall -Wextra countdown.cpp -o countdown (or clang++), run ./countdown and it prints 3, 2, last and Go! on four lines.
- cppreference.com: if statement
- cppreference.com: while loop
- cppreference.com: for loop
- cppreference.com: Comparison operators
- cppreference.com: Fundamental types (bool)
- cppreference.com: Statements
- cppreference.com: Main function (return 0 at the end of main)
- Standard C++ Foundation (isocpp.org): Get Started!
- Standard C++ Foundation (isocpp.org): FAQ for people new to C++
اختبار يتاح بعد الخطوة الأخيرة.