Professional Documents
Culture Documents
C# Lecture Three: Instructor: Eng Omar Dhore
C# Lecture Three: Instructor: Eng Omar Dhore
C# Lecture Three
C# operators
C# Control Statement
If statement
Switch Statement
Iteration
C# Operators
using System;
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
int x = 5;
int y = 3;
Console.WriteLine(x + y);
}
}
}
C# Comparison Operators
Example 2
using System;
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
int x = 5;
int y = 3;
Console.WriteLine(x != y); // returns True because 5 is not equal to 3
}
}
}
C# Logical Operators
Example 3
using System;
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
int x = 5;
Console.WriteLine(x > 3 && x < 10); // returns True because 5 is greater than 3 AND 5 is less than 10
}
}
}
C# Control Statement
The C# if-else statement also tests the condition. It executes the if block if
condition is true otherwise else block is executed.
if(condition){
//code if condition is true
}
else{
//code if condition is false
}
IF-else Statement
Example 1
{
int time = 20;
if (time < 18)
{
Console . WriteLine("Good day.");
}
else
{
Console . WriteLine("Good evening.");
}
C# Switch Statements
while loop
do loop
for loop
foreach loop
C# While Loop
In C#, while loop is used to iterate a part of the program several times. If
the number of iteration is not fixed, it is recommended to use while loop
than for loop.
1. while(condition){
2. //code to be executed
3. }
C# While Loop
Example
{
int i=1;
while(i<=10)
{
Console . WriteLine(i);
i++;
}
C# Do-While Loop
{
int i = 1;
do{
Console . WriteLine(i);
i++;
}
while (i <= 10) ;
C# For Loop
The C# for loop is used to iterate a part of the program several times. If the
number of iteration is fixed, it is recommended to use for loop than while or
do-while loops.
The C# for loop is same as C/C++. We can initialize variable, check condition
and increment/decrement value.
Syntax:
for (initialization; condition; incr/decr){
//code to be executed
}
C# For Loop
Example
{
for(int i=1;i<=10;i++){
Console . WriteLine(i);
}
}