Conditional Statements

By   Tewodros   Date Posted: Oct. 14, 2021  Hits: 726   Category:  ASP.NET C#   Total Comment: 0             A+ A-


side

Before we dive in to conditions, we need to understand some key concepts 

Expressions 

Arithmetic expressions are code that we need to compute and evaluate to reach to an answer

For example (3 * 5) + 9 is an an expression that will evaluate to 24.

Boolean expression always evaluate to true or false

Example: (5 * 9) < 40  is false where as 5 > 2 is true

Operators 

Increment and decrement operators ( i++, i--) increase or decrease the value a variable by a certain value

Example: 

int i = 0;

i+=5;  this means i = i + 5; which increases i by 5 and therefore i = 5

Addition, subtraction, division and multiplication operators are also working in the same way. 

Comparative Operators

  1. Less than (<)
  2. Less than or equal to (< =)
  3. Equal (==)
  4. Not Equal( ! =)
  5. greater (>)
  6. greater or equal to (> =)

Logical (or, and, not) operator 

Logical (or, and, not) operators  takes to Boolean expressions and return Boolean value (true, false)

In C#, or is written as || , and is written as && and not is written as !

Examples:

int i = 2;

int j = 4;

(i <5 ) || ( j > 2)    this will evaluate to true or true => true

(i <5 ) && ( j > 2)    this will evaluate to true and true => true

!(i <5 ) || ( j > 2)    this will evaluate to false or true => true

Assignment operator  ( = )

when we assign a value to a variable we are using the equal sign to do that and that is what we call the assignment operator

int i = 9;

Operation precedence

When evaluate operators we need to take into consideration operator precedence and associativity to determine the order in which the operations in an expression are performed. 

We evaluate the expression inside the bracket followed by division and multiplication and then addition and subtraction

Therefore, ((2*3) + (10/5 -2))/2  evaluates to 3

Conditional Statements

Are used for code flow control while evaluating the different conditions set 

There are four type of conational statement

  1. If
  2. if.. else
  3. if…else if… else
  4. switch

If Statement

If the condition is evaluated to true, performs a function or displays information

Example

int a = 9;

int b = 4;

If(a>b)

   Console.WriteLine(a + “>” + b); // output 9 > 4

 

If Else Statement

If the condition is evaluated to true, performs a function or displays information otherwise perform the else block

Example

int a = 1;

int b = 4;

If(a>b)

   Console.WriteLine(a + “>” + b); 

else

   Console.WriteLine(a + “<” + b); // output 1 < 4

 

Assume we want to check if a given number is odd or even.

We can use the usual if else statement

int a = 1;

 bool IsEven;

If(a%2==0)

    IsEven = true;

else

  IsEven = false;

We can rewrite the above if else statements in a very short hand form as follows

bool IsEven= (a%2==0)? true:false;

This means evaluate the bracket Boolean expression and ask if it is true or false and it it is true assign the true to IsEven variable otherwise assign false to the IsEven variable.

If …Else If… Else Statement

If the condition is evaluated to true, performs a function or displays information otherwise check the second if and it is true , performs a function or displays information, otherwise display what is in the else block.

Example

       int a = 4;

       int b = 4;

       if(a > b)

           Console.WriteLine(a + ">" +b);

       else if(a < b)

           Console.WriteLine(a +"<" +b);

       else

           Console.WriteLine(a + "=" + b); //output 4 = 4

                  

Note: We can have as many else ifs as we want but only one else condition

  if(condition1)      {…}

  else if(condition2)    {…}   

  else if(condition3)    {…}

  else if(condition4)    {…}

  else {…}

We can also have nested if...else statements inside other if else statements which can get really messy if we don't know what we are doing. 

 if(condition1)    {

     if(condition1)      {…}

    else if(condition2)    {…}  

    else {…}

  }

  else if(condition2)    {…}   

  else if(condition3)    {…}

  else if(condition4)    {…}

  else {…}

Switch Statement 

Switch Statement is like many else ifs but it has a different syntax and better performance than else..ifs.

We need to separate each case block with a break keyword and provide a default plan at the end in case none of the conditions met.

          string day = "Monday";

           switch (day)           {

               case "Monday":

                   Console.WriteLine("Happy Monday!");

                   break;

               case "Tuesday":

                   Console.WriteLine("Happy Tuesday!");

                   break;

               case "Wednesday":

                   Console.WriteLine("Happy Wednesday!");

                   break;

               case "Thursday":

                   Console.WriteLine("Happy Thursday!");

                   break;

               case "Friday":

                   Console.WriteLine("Happy Friday!");

                   break;

               default:

                   Console.WriteLine("Happy Weekend!");

                   break;

           }

Output: Happy Monday!

 


Tags



Back to Top



Related Blogs






Please fill all fields that are required and click Add Comment button.

Name:*
Email:*
Comment:*
(Only 2000 char allowed)


Security Code:* ilmpdw

Back to Top