-
Key Features and
Create a new project in Visual Studio 2022 - Naming Standards & Comments
- Data Types
- Variables and Constant Variable
- Conditional Statement
- Switch Conditional Statement
- Arithmetic, Comparison, Assignment and Logical Operators
- For, While and Do While Loops
- Importance of Break and Continue Keywords
- Strings and Important String Methods
- Single Dimensional Array
- Multi Dimensional Array
- Methods and Method Overloading
- Math Class and Functions
- Casting and Type Conversions
- Class and Objects
- Constructor, Parameter less and Parameterized Constructor
- Single, Multi level and Hierarchical Inheritance
- Compile Time Polymorphism, Method and Operator Overloading
- Run-time Polymorphism and Importance of base keyword
- Abstract Class and Method
- Interface and Multiple Inheritance through Interfaces
Agenda
- C# Loops
- for loop
- Nested for loop
- while loop
- do while loop
C# Loops
There may be a situation when user need to execute a block of code several number of times until some condition is met. In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on.
Loop statements are used for repetitive execution.
There are primary three types of loops in C#:
- for loop
- while loop
- do while loop
C# For Loop
- C# for loop is a control flow statement that iterates a part of the program multiple times.
- for loop repeats a block of statements for a specified number of times.
- If the number of iteration is fixed, it is recommended to use for loop.
Syntax:
for (initialization; condition; increment/decrement iterator ){
Statement(s)
}
Example:
Print 0 to 5 Numbers for(int i=0; i<=5; i++){ Console.WriteLine(i); }
While Loops
A while loop statement in C# programming language repeatedly executes a target statement as long as a given condition is true.
Syntax:
Initialization;
while (Condition){
Statement(s);
increment/decrement;
}
While Loops
- 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 user must have to execute the loop at least once, it is recommended to use do-while loop.
- It executes a block of statements at least once irrespective of the condition.
- First, the statements inside loop execute and then the condition gets evaluated, if the condition returns true then the control gets transferred to the “do” else it jumps to the next statement after do-while.
Syntax:
Initialization;do
{
Statement(s);
increment/decrement;
} while (Condition);
for (int i = 0; i <= 5; i++){
Console.WriteLine(i);
}
{
for (int i = 20; i >= 10; i--)
{
if ((i != 15) && (i != 13))
{
Console.WriteLine(i);
}
}
for (int i=1; i<=4; i++)
{
for (int j=1;j<=4;j++){
Console.WriteLine(i+" "+j);
}
}
int i = 1; ;
while (i <= 5)
{
Console.WriteLine(i);
i++;
}
int i = 1;
while (i <= 5)
{
if (i != 2)
{
Console.WriteLine(i);
}
i++;
}
int i = 8;
do
{
Console.WriteLine(i);
i++;
} while (i <= 12);
int i = 20;
do
{
if ((i != 18) && (i != 16))
{
Console.WriteLine(i);
}
i--;
}
while (i >= 15);