The for loop is typically used when you know in advance how many times you want to execute a statement or a block of statements.
for (int i = 0; i < 10; i++)
{
Console.WriteLine($"Iteration {i}");
}
Result
Iteration 0
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5
Iteration 6
Iteration 7
Iteration 8
Iteration 9
2. foreach Loop
The foreach loop is used to iterate over the elements of a collection (like arrays, lists, etc.).
int[] numbers = { 1, 2, 3, 4, 5 };
foreach (int number in numbers)
{
Console.WriteLine(number);
}
3. while Loop
The while loop executes a block of statements as long as a specified condition is true. The condition is evaluated before the execution of the loop's body.
Code
int count = 0;
while (count < 5)
{
Console.WriteLine("Count: " + count);
count++;
}
4. do-while Loop
The do-while loop is similar to the while loop, but the condition is evaluated after the execution of the loop's body. This means the code block will be executed at least once.
int count = 0;
do
{
Console.WriteLine("Count: " + count);
count++;
} while (count < 5);
5. break Statement
The break statement is used to exit from the innermost loop or switch statement in which it appears.
for (int i = 0; i < 10; i++)
{
if (i == 5)
{
break; // Exit the loop
}
Console.WriteLine(i);
}
6. continue Statement
The continue statement skips the current iteration of the loop and proceeds with the next iteration.
for (int i = 0; i < 10; i++)
{
if (i % 2 == 0)
{
continue; // Skip even numbers
}
Console.WriteLine(i);
}
The for loop is typically used when you know in advance how many times you want to execute a statement or a block of statements.
for (int i = 0; i < 10; i++) { Console.WriteLine($"Iteration {i}"); }
Result
Iteration 0 Iteration 1 Iteration 2 Iteration 3 Iteration 4 Iteration 5 Iteration 6 Iteration 7 Iteration 8 Iteration 9
2. foreach Loop
The foreach loop is used to iterate over the elements of a collection (like arrays, lists, etc.).
int[] numbers = { 1, 2, 3, 4, 5 }; foreach (int number in numbers) { Console.WriteLine(number); }
3. while Loop
The while loop executes a block of statements as long as a specified condition is true. The condition is evaluated before the execution of the loop's body.
Code
int count = 0; while (count < 5) { Console.WriteLine("Count: " + count); count++; }
4. do-while Loop
The do-while loop is similar to the while loop, but the condition is evaluated after the execution of the loop's body. This means the code block will be executed at least once.
int count = 0; do { Console.WriteLine("Count: " + count); count++; } while (count < 5);
5. break Statement
The break statement is used to exit from the innermost loop or switch statement in which it appears.
for (int i = 0; i < 10; i++) { if (i == 5) { break; // Exit the loop } Console.WriteLine(i); }
6. continue Statement
The continue statement skips the current iteration of the loop and proceeds with the next iteration.
for (int i = 0; i < 10; i++) { if (i % 2 == 0) { continue; // Skip even numbers } Console.WriteLine(i); }
C# is primarily used on the Windows . NET framework, although it can be applied to an open source platform. This highly versatile programming language is an object-oriented programming language (OOP) and comparably new to the game, yet a reliable crowd pleaser.
The C# language is also easy to learn because by learning a small subset of the language you can immediately start to write useful code. More advanced features can be learnt as you become more proficient, but you are not forced to learn them to get up and running. C# is very good at encapsulating complexity.
The decision to opt for C# or Node. js largely hinges on the specific requirements of your project. If you're developing a CPU-intensive, enterprise-level application where stability and comprehensive tooling are crucial, C# might be your best bet.
C# is part of .NET, a free and open source development platform for building apps that run on Windows, macOS, Linux, iOS, and Android. There's an active community answering questions, producing samples, writing tutorials, authoring books, and more.
Copyrights © 2024 letsupdateskills All rights reserved