3.4 Conditionals

Conditionals statements helps to execute programs with respect to the conditions. If the condition is true than it will execute a particular statement.

Conditionals Statements

If Else and Else If statements

These statements helps to execute on some condition. It's like if it is raining outside than take and umbrella with you or if it isn't raining don't take an umbrella, let's implement this example in code.

But first let's look at the if else if conditionals structure.

If Else

if(condition....) {
    // Some statement to execute
}
else {
    // Some statement to execute
}

If Else If

if(condition....) {
    // Some statement to execute
}
else if(condition....) {
    // Some statement to execute
}
else {
    // Some statement to execute
}

Now, as we have understood the structure. Let's code the example.

using System;
class HelloWorld {
  static void Main() {
      bool isRaining = true;
      
      if(isRaining) {
          Console.WriteLine("It is Raining!");
      }
      else {
          Console.WriteLine("It is not Raining");
      }
  }
}

Let's code another problem.

WAP to take certain numbers and if the number is divisible by 2 then print "divisible by 2" if it is divisible by 3 then print "divisible by 3" or else print "this number is not divisible by 2 or 3".

using System;
class HelloWorld {
  static void Main() {
      int num = 5;
      
      if(num%2==0) {
          Console.WriteLine("Divisible by 2");
      }
      else if(num%3==0) {
          Console.WriteLine("Divisible by 3");
      }
      else {
          Console.WriteLine("Number is not Divisible by 2 or 3");
      }
  }
}

Inline If Statement

An inline if statement is a simpler form of an if statement that is very convenient if you want to assign a value to a variable depending on the result of a condition.

Structure to write an inline if statement.

condition ? value if condition is true : value if condition is
false;

Let's write a inline if statement to assign a value to a variable based on a certain condition. If 4 is greater than 2 then "4 has been assigned" else "2 has been assigned".

string statement = 4>2 ? "4 has been assigned" : "2 has been assigned";

Switch Case Statement

The switch statement is similar to an if statement except that it does not work with a range of values. A switch statement requires each case to be based on a single value. Depending on the value of the variable used for switching, the program will execute the correct block of code.

switch(choice) {
    case value:
        // Do something
        break;
    case value2:
        // Do something
        break;
    default:
        // Do something
        break;
}

Let's write a program of a simple grading system. If grade is A print "Very Good", if grade is B "Good, can do better" and default will be "Wrong Choice".

using System;
class HelloWorld {
  static void Main() {
    char ch = 'B';
    
    switch(ch) {
        case 'A':
            Console.WriteLine("Very Good");
            break;
        case 'B':
            Console.WriteLine("Good, can do better");
            break;
        default:
            Console.WriteLine("Wrong Choice");
            break;
    }
  }
}

Break is a jump statement, it basically breaks the execution of the program, if we don't give it than every statement will be printed in Switch Case.