Conditions

Conditions and If Statements

Logical Operators

C# supports the usual logical operators from mathematics, You can use these conditions to perform different actions for different decisions:

  • Less than: a < b

  • Less than or equal to: a <= b

  • Greater than: a > b

  • Greater than or equal to: a >= b

  • Equal to: a == b

  • Not Equal to: a != b

Conditional Statements

C# has the following conditional statements:

  • Use if to specify a block of code to be executed, if a specified condition is true

  • Use else to specify a block of code to be executed, if the same condition is false

  • Use else if to specify a new condition to test, if the first condition is false

  • Use switch to specify many alternative blocks of code to be executed

Syntax

Exercise

Expectation

Read two numbers from user and print the one which is greater

Examples:

Exercise

Expectations:

Print given message based on the age of the user:

Age < 18 → You cannot vote yet!

Age < 25 → Your vote is the future of the country!

Age < 60 → Your vote is important for country!

Otherwise → Your experience is vital for country!

Example:

Last updated