Branching

In C#, branching is used to execute different blocks of code depending on conditions. The main branching constructs use the if and else operators.

The if operator

Used to execute a block of code if the condition is true.
Line 1, Char 1

The if-else operator

Allows you to execute one of two blocks of code depending on the condition.
Line 1, Char 1

Using conditional operators

Complex logical expressions can also be passed to conditional operators; the main requirement is that they return the logical type bool.
Line 1, Char 1

Exercises

  1. Write a function that, based on age and whether the person has a driver's license, tells whether they can drive a car. Function signature: bool CanDrive(int age, bool hasDriveLicense).
Line 1, Char 1
  1. Write a function that, depending on a numeric parameter, prints a message. If the number is > 0, the function prints The number is positive; if negative - The number is negative; if 0 - The number is zero. Function signature: void PrintNumberType(int number).
Line 1, Char 1