Decision and Operators

We always came with some decision-making process during execution where we have to decide on the basis of some constraints or conditions. In C# programming we manage this situation using if else statements.
We will try some examples to understand this better.

using System;
namespace ifDecision 
{ 
    class Program { 
        static void Main(string[] args) { 
            Console.WriteLine("What is you age?"); 
            int n = Convert.ToInt32(Console.ReadLine()); 
            if (n <= 6) { 
                Console.Write("Grow up kiddo"); 
            } 
            else if (n>=6 && n <= 18) { 
                Console.Write("Well, you are still not an adult.");
            } 
            else { 
                 Console.Write("You are an adult"); 
             } 
Console.ReadLine(); 
        }
    }
}

Output:


Let’s break the code and try to analyze. Here at line 7, we are comparing the value of n (provided by the user) and integer six. Here comes the role of operators. ‘>’ is a relational operator, which checks if the value of left operand is greater than the value of right operand, if yes then returns True. There are six types of operators are there namely:-

  • Arithmetic Operators- ‘+’, ‘-‘, ‘/’, ‘%’, etc.
  • Relational Operators- ‘>’, ‘<‘, ‘==’, etc.
  • Logical Operators- ‘&&’, ‘||’, ‘!’
  • Bitwise Operators- ‘&’, ‘~’, ‘^’, etc.
  • Assignment Operators- ‘=’, ‘+=’, ‘-=’, etc.
  • Misc Operators- ‘sizeof()’, ‘typeof()’, ‘is’, etc.

We use these operators in conditional statements and in computing some mathematical or logical problems.
So in line 7 if this operator returns True the block of code inside that if statement will execute else next condition will be checked.
In line 10 again a condition is checked and at last, if all the previous conditions are false then the code block of last else statement will execute. We can use only one if-else statement or only an if statement but every else must have one if.

Leave a Reply

Your email address will not be published. Required fields are marked *