Control flow: if, else and while
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
A C# program runs its statements one after another, top to bottom, until a control-flow statement changes the order. Follow a ten-line countdown in Program.cs 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. In C#, every condition must be a bool.
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.
03n is an int. What happens if you write while (n) in C#?
C# is type-safe: if and while need a bool, true or false. Write the question in full, such as while (n > 0). The same rule makes the slip if (n = 1) an error instead of a hidden bug.
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, and every if or while condition must be one.
- 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 5 and 7. Many teams and style guides say to always write braces { } so that adding a second line later cannot slip outside the if.
- In C# a condition must be a bool. There is no conversion from int to bool, so while (n) or if (n) does not compile (error CS0029). Writing if (n = 1) by mistake is also error CS0029, because n = 1 is an int, not a bool.
- With top-level statements there is no Main method to write. When the last statement finishes, the program ends with exit code 0 (success).
- 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-- is short for n = n - 1. C# also has do-while, switch, break and continue.
- The program on the card is real C#. Put the ten lines in Program.cs of a new console project (dotnet new console), run dotnet run and it prints 3, 2, last and Go! on four lines. The card indents by 2 spaces to fit; Visual Studio uses 4.
- Microsoft Learn: Selection statements (if, else, switch)
- Microsoft Learn: Iteration statements (for, foreach, do, while)
- Microsoft Learn: Jump statements (break, continue, return)
- Microsoft Learn: bool (C# reference)
- Microsoft Learn: Comparison operators
- Microsoft Learn: Equality operators (==)
- Microsoft Learn: Compiler error CS0029
- Microsoft Learn: Top-level statements
- Microsoft Learn: Console.WriteLine method
- Microsoft Learn: Branches and loops (C# tutorial)
اختبار يتاح بعد الخطوة الأخيرة.