You are on page 1of 24

1

C# Lecture Three

Prepared By: Eng Omar Dhore


INSTRUCTOR: ENG OMAR DHORE
Topics covered

 C# operators
 C# Control Statement
 If statement
 Switch Statement
 Iteration
C# Operators

 An operator is simply a symbol that is used to perform operations. There can


be many types of operations like
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Assignment Operators
• Unary Operators
• Ternary Operators
• Misc Operators
C# Operators
Arithmetic Operators
Example 1

 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

C# provides many decision-making statements that


help the flow of the program execution
• if statement
• if-else statement
• Switch Statement
• Ternary operator
C# IF Statement

1. The C# if statement tests the condition. It is executed if condition is true.


2. if(condition){  
3. //code to be executed  
4. }  
 {
 int x = 20;
 int y = 18;
 if (x > y)
 {
 Console. WriteLine("x is greater than y");
 }
 }
C# IF-else 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

 The C# switch statement executes one statement from multiple conditions.


Switch Statements
Example
C# Ternary operator

 There is also a short-hand if else, which is known as the


ternary operator because it consists of three operands. It
can be used to replace multiple lines of code with a single
line. It is often used to replace simple if else statements:
 variable = (condition ? expressionTrue : expressionFalse;
If-else statement vs Ternary operator
Example
Iteration

Iteration is the repetition of a process in a computer


program.

When designing programs, there may be some


instructions that need repeating. This is known as
iteration.

Loops offer a quick and easy way to do something


repeatedly
Iteration

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

 The C# do-while loop is used to iterate a part of the program several


times. If the number of iteration is not fixed and you must have to execute
the loop at least once, it is recommended to use do-while loop.
 The do-while loop is executed at least once because condition is checked after
loop body.
1. do{  
2. //code to be executed  
3. }while(condition);  
Do-While Loop
Example

{  
        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);    
         }    
     }  

You might also like