Suggested Videos
Part 2 - Reading and writing to console
Part 3 - Built-in types
Part 4 - String type
In this video, we will discuss the common operators that are available in c# programming language.
Assignment Operator =
Arithmetic Operators like +,-,*,/,%
Comparison Operators like ==, !=,>, >=, <, <=
Conditional Operators like &&, ||
Ternary Operator ?:
Null Coalescing Operator ??
Examples used in the demo
Part 2 - Reading and writing to console
Part 3 - Built-in types
Part 4 - String type
In this video, we will discuss the common operators that are available in c# programming language.
Assignment Operator =
Arithmetic Operators like +,-,*,/,%
Comparison Operators like ==, !=,>, >=, <, <=
Conditional Operators like &&, ||
Ternary Operator ?:
Null Coalescing Operator ??
Examples used in the demo
using System;
namespace ConsoleApplication1
{
class Program
{
public static void Main()
{
// Assignment Operator example
// Single = is the assignment operator
inti = 10;
boolb = true;
// For dividing 2 numbers we can use either
// % or / operators
intnumerator = 10;
intdenominator = 2;
// Arithmentic operator / returns quotient
intquotient = numerator / denominator;
Console.WriteLine("Quotient = {0}", quotient);
// Arithmentic operator % returns remainder
intremainder = numerator % denominator;
Console.WriteLine("Remainder = {0}", remainder);
// To compare if 2 numbers are
// equal use comparison operator ==
intnumber = 10;
if(number == 10)
{
Console.WriteLine("Number is equal to 10");
}
// To compare if 2 numbers are not
// equal use comparison operator !=
if(number != 5)
{
Console.WriteLine("Number is not equal to 5");
}
// When && operator is used all the conditions must
// be true for the code in the "if" block to be executed
intnumber1 = 10;
intnumber2 = 20;
if(number1 == 10 && number2 == 20)
{
Console.WriteLine("Both conditions are true");
}
// When || operator is used the code in the "if" block
// is excuted if any one of the condition is true
number1 = 10;
number2 = 21;
if (number1 == 10 || number2 == 20)
{
Console.WriteLine("Atleast one of the condition is true");
}
}
}
}
The example below is not using the ternary operator. Look at the amount of code we have to write to check if a number is equal to 10, and then initialise a boolean variable to true or false depending on whether the number is equal to 10 or not.
The example below is not using the ternary operator. Look at the amount of code we have to write to check if a number is equal to 10, and then initialise a boolean variable to true or false depending on whether the number is equal to 10 or not.
using System;
namespace ConsoleApplication1
{
class Program
{
public static void Main()
{
intnumber = 10;
boolisNumber10;
if(number == 10)
{
isNumber10 = true;
}
else
{
isNumber10 = false;
}
Console.WriteLine("Number == 10 is {0}", isNumber10);
}
}
}
Ternary operator example : We have rewritten the above program using ternary operator. Notice the amount of code we have to write is greatly reduced.
using System;
namespace ConsoleApplication1
{
class Program
{
public static void Main()
{
intnumber = 10;
// Ternary operator example
boolisNumber10 = number == 10 ? true : false;
Console.WriteLine("Number == 10 is {0}", isNumber10);
}
}
}