csharp_operators_expressions.md - brainchildservices/curriculum GitHub Wiki

SLIDE-1

C# Operator

What are Operators?

  • Operators are symbols that are used to perform operations on operands. Operands may be variables and/or constants. For example, in 2+3, + is an operator that is used to carry out addition operation, while 2 and 3 are operands.

  • Operators are used to perform operations on variables and values.

  • Example Below ->

SLIDE-1(DOWNWARDS)

  • https://dotnetfiddle.net/ksXJJo

                          using System;
    
                          public class Program
                          {
                          	public static void Main()
                          	{
                          		int x;  // Declaring a variable
                          		x=10; // Assigning value to variable x
    
                          		int y=20; // Declaring a variable y of type int and assigning value 20 to it
                          	}
                          }
    

SLIDE-2

What are Arithmetic operators?

Arithmetic operators are used to perform arithmetic operations such as addition, subtraction, multiplication, division, Modulo. Addition : 6 + 3 evaluates to 9 subtraction : 10 - 6 evaluates to 4 multiplication : 4 * 2 evaluates to 8 division : 10 / 5 evaluates to 2 Modulo (Remainder) : 16 % 3 evaluates to 1

SLIDE-2(DOWNWARDS)

Basic Arithmetic Operators

  • https://dotnetfiddle.net/UQEmab

                          using System;
    
                          public class Program
                          {
                          	public static void Main()
                          	{
                          		//Basic Arithematic Operators
    
                          		int number1=20;  
                          		int number2=7;  
    
                          		int sum= number1+number2;
                          		int difference =number1-number2;
                          		int product =number1*number2;
                          		int quotient =number1/number2;
                          		int remainder =number1%number2;   // % is called modulus operator
    
                          		Console.WriteLine("Sum of number1+number2 is:"+sum);
                          		Console.WriteLine("Difference of number1-number2 is:"+difference);
                          		Console.WriteLine("Product of number1*number2 is:"+product);
                          		Console.WriteLine("Quotient of number1/number2 is:"+quotient);
                          		Console.WriteLine("Remainder of number1/number2 is:"+remainder);
                          	}
                          }
    

SLIDE-3

Increment and Decrement operator

  • https://dotnetfiddle.net/czEXLi

                          using System;
    
                          public class Program
                          {
                          	public static void Main()
                          	{
                          		//Increment and Decrement operator
                          		int a=2;
                          		a=a+1;
                          		Console.WriteLine("The value of a after adding 1 is:"+a);
                          		a++;  //Increment Operator
                          		Console.WriteLine("The value of a after increment operator is:"+a);
    
                          		int b=3;
                          		b=b-1;
                          		Console.WriteLine("The value of b after subtracting 1 is:"+b);
                          		b--;  //Decrement Operator
    
                          		Console.WriteLine("The value of b after decrement operator is:"+b);
                          	}
                          }
    

SLIDE-4

Difference between Increment and Decrement Operator

  • https://dotnetfiddle.net/pzQRjz

                          using System;
                          
                          public class Program
                          {
                          	public static void Main()
                          	{
                          		//Difference between Increment and Decrement Operator
                          		int a = 10;
                          		Console.WriteLine("The value of a++ is"+ a++);   	//Post Increment
                          		Console.WriteLine("The value of a is"+ a);  
                          		int b = 10;
                          		Console.WriteLine("The value of ++b is"+ ++b);		//Pre Increment
                          
                          
                          		//i++ means 'tell me the value of i, then increment'
                          		//++i means 'increment i, then tell me the value'
                          
                          		//You won't normally notice a differene in the usage until you use the increment/operator with assignment;
                          		//Post Increment
                          		int x1=20;
                          		x1++;
                          		int x2=x1;
                          		Console.WriteLine("The value of x2 is"+ x2);
                          
                          		int y1=20;
                          		int y2=y1++;
                          		Console.WriteLine("The value of y2 is"+ y2);
                          
                          		//Pre Increment
                          		int p1=20;
                          		++p1;
                          		int p2=p1;
                          		Console.WriteLine("The value of p2 is"+ p2);
                          
                          		int q1=20;
                          		int q2=++q1;
                          		Console.WriteLine("The value of q2 is"+ q2);
                          	}
                          }
    

SLIDE-5

Assignment Operators

  • https://dotnetfiddle.net/Nhops1

      					using System;
    
      					public class Program
      					{
      						public static void Main()
      						{
      							//Assignment Operators
      							int a=10;
      							a+=1;   //a=a+1 or ++a
      							Console.WriteLine("Using Addition assignment operator, Value of a is:"+a);
    
      							int z=10;
      							z+=2;   //z=z+2
      							Console.WriteLine("Using Addition assignment operator, Value of z is:"+z);
    
      							int b=10;
      							b-=2;	//b=b-2;
      							Console.WriteLine("Using Subtraction assignment operator, Value of b is:"+b);
    
      							int c=20;
      							c*=2;	//c=c*2;
      							Console.WriteLine("Using Multiplication assignment operator, Value of c is:"+c);
    
      							int d=20;
      							d/=2;	//d=d/2;
      							Console.WriteLine("Using Division assignment operator, Value of d is:"+d);
    
      							int e=20;
      							e%=3;	//e=e/2;
      							Console.WriteLine("Using Modulus assignment operator, Value of e is:"+e);
      						}
      					}
    

SLIDE-6

Comparison Operators

  • https://dotnetfiddle.net/kISXsB

      					using System;
    
      					public class Program
      					{
      						public static void Main()
      						{
      							// Comparison Operators : The result is a boolean value
      							int p=25;
      							bool result=p>5;
      							Console.WriteLine("The value of result is:"+result);
    
      							int a=7; 
      							Console.Write("Check if a is greated than 3:");
      							Console.WriteLine(a>3);
    
      							int b=30;
      							Console.Write("Check if b is less than 20:");
      							Console.WriteLine(b<20);
    
      							int c=35;
      							Console.Write("Check if c is less than or equal to 35:");
      							Console.WriteLine(c<=35);
    
      							int d=35;
      							Console.Write("Check if d is equal to 50:");
      							Console.WriteLine(d==35);
      						}
      					}
    

SLIDE-7

What are Logical operators?

  • Logical operators are used to perform logical operation such as and, or. Logical operators operates on boolean expressions (true and false) and returns boolean values. Logical operators are used in decision making and loops.

SLIDE-8

  • C# Logical operators

+────────────+────────────+──────────+───────────+
| Operand 1 | Operand 2 | OR (||) | AND (&&) |
+────────────+────────────+──────────+───────────+
| True | True | True | True |
| True | False | True | False |
| False | True | True | False |
| False | False | False | False |
+────────────+────────────+──────────+───────────+

| Operand 1 | Operand 2 | OR (||) | AND (&&) |
|:---------:|:---------:|:-------:|:--------:|
| True | True | True | True |
| True | False | True | False |
| False | True | True | False |
| False | False | False | False |

SLIDE-8(DOWNWARDS)

  • Logical Operators

    • https://dotnetfiddle.net/ANhzwJ

         				using System;
      
         				public class Program
         				{
         					public static void Main()
         					{
         						// Logical Operators
         						// Logical operators are used to determine the logic between variables or values:
      
         						int a=1;
         						int b=10;
         						bool resultAnd=a>0 && b<10;   // Will be true if both conditions are true - Logical AND Operator- &&
         						Console.WriteLine("Checking if a is greater 1 and b is less than 10: "+resultAnd);
      
         						int c=1;
         						int d=10;
         						bool resultOR=c>0 || d<10;   // Will be true if any of the one condition is true - Logical OR Operator-||
         						Console.WriteLine("Checking if c is greater 1 or d is les than 10: "+resultOR);
      
         						bool myValue= true;
         						bool newValue=!myValue;		//The Not Operator will reverse the bool value
         						Console.WriteLine("Revered value of myValue is : "+newValue);
      
         						int p=1;
         						int q=10;
         						Console.Write("Checking if p is greater 1 and q is less than 10:");
         						Console.WriteLine(p>0 && q<10);
         					}
         				}