English
Summary
Conclusions
Control flow constructs change a program's sequential execution order. if/else and the ternary operator ?: choose an action based on a Boolean condition; the switch statement and expression select by values and patterns: constant, relational, and logical (and, or, not). A switch expression returns a value and must be exhaustive. A while loop checks its condition before an iteration, while do/while checks afterward and runs the body at least once; for is convenient for counters, and foreach for iterating over collection elements. break and continue control loop execution, and most tasks can be solved with common algorithms: accumulating a sum, counting, finding minimum and maximum values, and approximate calculations with a specified accuracy.
Self-check questions
- What is a code block? What is the scope of a variable declared inside it?
- How does an
else ifchain work? Why does condition order matter? - What happens if you place a semicolon after an
ifcondition? - How does the ternary operator
?:differ from anifstatement? - How does a
switchstatement work? Why is there no fall-through between branches in C#? - Which patterns can you use with
isand in aswitchexpression? - What is the purpose of a
whencondition in acasebranch? - How does a
switchexpression differ from aswitchstatement? What does an exhaustive expression mean? - How does
whilediffer fromdo/while? Give examples of their use. - What are the parts of a
forloop header? In what order are they executed? - How many times is the inner loop body executed in nested loops?
- What is
foreachfor? Can you modify its iteration variable? - How do
break,continue, andreturndiffer? - How can you exit multiple nested loops at once?
- How can you use a loop to calculate a value to a specified accuracy?
Useful links
- Selection statements: https://learn.microsoft.com/dotnet/csharp/language-reference/statements/selection-statements
- The
switchexpression: https://learn.microsoft.com/dotnet/csharp/language-reference/operators/switch-expression - Patterns: https://learn.microsoft.com/dotnet/csharp/language-reference/operators/patterns
- Iteration statements: https://learn.microsoft.com/dotnet/csharp/language-reference/statements/iteration-statements
- Jump statements: https://learn.microsoft.com/dotnet/csharp/language-reference/statements/jump-statements
- Stepping through code in Visual Studio: https://learn.microsoft.com/visualstudio/debugger/navigating-through-code-with-the-debugger