Professional Documents
Culture Documents
Part-5
Assignment Operator =
Arithmetic Operators like +,-,*,/,%
Comparison Operators like ==, !=,>, >=, <, <=
Conditional Operators like &&, ||
Ternary Operator ?: -
Null Coalescing Operator ??
The bool keyword is an alias of System.Boolean. It is used to declare variables to store
the Boolean values, true and false.
In c# language, ternary operator (?) use to check a conduction, so other name of
ternary operator is conditional operator, which work as a if statement. This
operator compares two values. It produces a third value that depends on the result
of the comparison.
Examples used in the demo
using System;
namespace ConsoleApplication1
{
class Program
{
public static void Main()
{
// Assignment Operator example
// Single = is the assignment operator
int i = 10;
bool b = 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.
using System;
namespace ConsoleApplication1
{
class Program
{
public static void Main()
{
int number = 10;
bool isNumber10;
if (number == 10)
{
isNumber10 = true;
}
else
{
isNumber10 = false;
}
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()
{
int number = 10;
PART-6
we will discuss
1. Nullable types in C#
2. Null Coalescing Operator ??
The ? operator converts non-nullable types to nullable types
The ?? operator is also known as the null-coalescing operator. It returns the left side operand if
the operand is not null else it returns the right side operand.
if (TicketsOnSale == null)
{
AvailableTickets = 0;
}
else
{
AvailableTickets = (int)TicketsOnSale;
}
PART_7
Implicit conversion is done by the compiler:
1. When there is no loss of information if the conversion is done
2. If there is no possibility of throwing exceptions during the conversion
Example: Converting an int to a float will not loose any data and no exception will be thrown,
hence an implicit conversion can be done.
Where as when converting a float to an int, we loose the fractional part and also a possibility of
overflow exception. Hence, in this case an explicit conversion is required. For explicit
conversion we can use cast operator or the convert class in c#.
Console.WriteLine(f);
}
}
Console.WriteLine(i);
}
}
PART-8
we will discuss
1. Arrays
2. Advantages and dis-advantages of arrays
using System;
class Program
{
public static void Main()
{
// Initialize and assign values in different lines
int[] EvenNumbers = new int[3];
EvenNumbers[0] = 0;
EvenNumbers[1] = 2;
EvenNumbers[2] = 4;
Disadvantages: Arrays cannot grow in size once initialized. Have to rely on integral indices to
store or retrieve items from the array.
PART-9
Decision Making
Statement Description
The ? : Operator:
Where Exp1, Exp2, and Exp3 are expressions. Notice the use and placement of the
colon.
DIFFERENCE BETWEEN
| -means OR it checks the all condition in if statement
|| -means OR if it find the condition is true then it not check other condition.it faster
than |.
& - if the first condition is false then it checks the all other condition.
&& - if the first condition is false then it not checks the other condition.it is faster
than &.
IF Statement: if break statement is used inside a switch statement, the control will
leave the switch statement.
Goto statement: you can either jump to another switch statement or to a specific
label.
using System;
namespace switch_statement
{
class Program
{
static void Main(string[] args)
{
int userchoice;
int coffeecost = 0;
Start:
Console.WriteLine("Please select the size of coffee 1-Small,2-Medium,3-Large");
userchoice = int.Parse(Console.ReadLine());
switch (userchoice)
{
case 1:
coffeecost += 1;
break;
case 2:
coffeecost += 2;
break;
case 3:
coffeecost += 3;
break;
default:
Console.WriteLine("select the right option");
goto Start;
}
Console.WriteLine("Thank You for shopping with us");
Console.WriteLine("cost of coffee {0}Rs", coffeecost);
}
}
}
}
PART-10
You can use one or more loop inside any another while,
nested loops
for or do..while loop.
While example
using System;
namespace WHILE
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("please enter the number to display even number");
int even = int.Parse(Console.ReadLine());
int i = 0;
while (i <= even)
{
Console.WriteLine(i);
i = i + 2;
}
}
}
}