C# Introduction

Life is 10% what happens to us and 90% how we react to it. If you don't build your dream, someone else will hire you to help them build theirs.

C# Introduction

C# Operators – Basic Programming

Operators are used for building expressions in C#. To calculate the value of variable or performs operation in variable you will have to make proper expression. These expressions are made using C# operators.
C# provides wide range of operators as arithmetic operators, assignment operators, unary operators, comparison operator, logical operator etc.

C# Arithmetic Operators

Arithmetic Operators are used for basic mathematical calculation in C# programming. The list of Arithmetic Operators is given below:

Operator Description Examples
+ Add numbers X=num1+num2
Subtract numbers X=num1-num2
* Multiply numbers X=num1*num2
/ Divide numbers X=num1/num2
% Divide two numbers and returns reminder X=22%10 then X will be X=2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Arithmetic_Operators
{
    class Program
    {
        static void Main(string[] args)
        {
            int num1, num2;
            int add, sub, mul;
            float div;
 
            //Accepting Values from users
            Console.Write("Enter first number\t\t");
            num1 = Convert.ToInt32(Console.ReadLine());
 
            Console.Write("\n\nEnter second number\t\t");
            num2 = Convert.ToInt32(Console.ReadLine());
 
            //Processing Values
            //Used + operator for adding values
            add = num1 + num2;
            //Used - operator for subtracting values
            sub = num1 - num2;
            //Used * operator for multiplying values
            mul = num1 * num2;
            //Used / operator for dividing values
            div = (float)num1 / num2;
 
            //Displaying Output
            Console.WriteLine("\n\n=====================\n");
            Console.WriteLine("Addition\t\t{0}", add);
            Console.WriteLine("Subtraction\t\t{0}", sub);
            Console.WriteLine("Multiplication\t\t{0}", mul);
            Console.WriteLine("Division\t\t{0}", div);
            Console.WriteLine("\n=======================\n");
 
            Console.ReadLine();
        }
    }
}
C# Assignment Operators

The C# assignment operator is generally suffix with arithmetic operators. The symbol of c sharp assignment operator is “=” without quotes. The assignment operator widely used with C# programming. Consider a simple example:

result=num1+num2;

In this example, the equal to (=) assignment operator assigns the value of num1 + num2 into result variable.
Various types of C# assignment operators are mentioned below:

Assignment Operators Usage Examples
= (Equal to) result=5 Assign the value 5 for result
+= (Plus Equal to) result+=5 Same as result=result+5
-= (Minus Equal to) result-=5 Same as result=result-5
*= (Multiply Equal to) result*=5 Same as result=result*5
/= (Divide Equal to) result/=5 Same as result=result/5
%= (Modulus Equal to) result%=5 Same as result=result%5
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace assignment_operator
{
    class Program
    {
        static void Main(string[] args)
        {
            int num1, num2;
 
            num1 = 10;
            num2 = 5;
 
            num1 += num2; // same as num1=num1+num2
            Console.WriteLine("Add = {0}", num1);
 
            num1 -= num2; // same as num1=num1-num2
            Console.WriteLine("\n\nSubtraction = {0}", num1);
 
            num1 *= num2; // same as num1=num1*num2
            Console.WriteLine("\n\nMultiplication={0}", num1);
 
            num1 %= num2; // same as num1=num1%num2
            Console.WriteLine("\n\nModulus = {0}", num1);
 
            Console.ReadLine();
        }
    }
}
C# Unary Operators

The C# unary operator is widely used for increment or decrement value by 1. This operator widely used with loop constructs to increment loop by 1. It is very easy to use and understand C# unary operators.
++ Increment Operator:
This operator is pronounced as increment operator. It is used for incrementing value by 1. It is used in C# programming by two types: Pre-increment (++i) and Post-increment (i++). In pre-increment, first it increments by 1 then loop executes whereas in Post-increment, the loop executes then it increments by 1.
— Decrement Operator:
The behavior of decrement operator is just opposite from increment operator. It is used for decrementing the value by one. It has also two types: Pre-Decrement (–i) and Post Decrement (i–). In pre-decrement the value is decremented by one then loop executes whereas in post-decrement the loop executed then the value decrements by one.

C# Comparison Operators

The C# comparison operator is used to compare two operands. It returns true or false based on comparison. The complete list of comparison operators are listed in a table. Consider x is a variable and the value assigned the x=2 then,

Operator Name Examples
< Less than x<5 (returns true)
> Greater than x>5 (returns false)
<= Less than equal to x<=2 (returns true)
>= Greater than equal to x>=2 (returns true)
== Equal equal to x==2 (returns true)
!= Not equal to x!=2 (returns false)
C# Logical Operator

The C# Logical Operator also evaluates the values and returns true or false as output. Based on true-false the program behaves dynamically at run time. This operator is widely used with C# programming.
&& Operator :
It is pronounced as and operator. It returns true if both or all the conditions are true and return false if any of the condition is false.
|| Operator:
It is pronounced as or operator. It also returns true or false based on condition. If any one of the condition matches then it returns true but if both or all the conditions are false then it returns false.
! Operator:
It is pronounced as not operator. It returns true if expression is false. The following demonstration will clear the concept of not operator.
^ Operator:
It is pronounced as xor operator. It returns false if the following condition matches:
(i) if both or all the expression returns true.
(ii) If both or all the expression returns false.

Write a program to check whether input character is vowel or not.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace operator_example_1
{
    class Program
    {
        static void Main(string[] args)
        {
            char ch;
 
            Console.Write("Enter a character: \t");
            ch = Convert.ToChar(Console.ReadLine());
 
            if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U')
            {
                Console.WriteLine("\n\n{0} is vowel", ch);
            }
            else
            {
                Console.WriteLine("\n\n{0} is not vowel", ch);
            }
            Console.ReadLine();
        }
    }
}