Admin Name
Dashboard
Categorey
Sub Categorey
Artilces
Users
Settings
Logout
Welcome to the Admin Panel
Create Post
Select Categorey
- Select -
coding
Select Sub Categorey
Title Name :
Defination :
Loops in C# are used to execute a block of code repeatedly, either a fixed number of times or until a certain condition is met. C# supports several types of loops: for, while, do-while, and foreach.
Code :
using System; class Program { static void Main() { for (int i = 1; i <= 5; i++) { Console.WriteLine("Iteration: " + i); } } }
Code Part 2 :
using System; class Program { static void Main() { // while loop int i = 1; while (i <= 3) { Console.WriteLine("while loop: " + i); i++; } // do-while loop int j = 1; do { Console.WriteLine("do-while loop: " + j); j++; } while (j <= 3); // foreach loop string[] fruits = { "Apple", "Banana", "Cherry" }; foreach (string fruit in fruits) { Console.WriteLine("foreach loop: " + fruit); } } }
Upload Image 1
Upload Image 2
Summary
In the for loop: The loop starts with int i = 1. The condition i <= 5 is true for values 1 through 5. Each time, i is incremented by 1 (i++). Once i becomes 6, the condition is false and the loop exits. In the while loop: Starts with i = 1. Checks i <= 3. Condition is true ? code executes ? i increments. Repeats until i = 4, where condition becomes false. In the do-while loop: Runs the block at least once, even if the condition is false after the first run. Example: If j = 1, the block executes, then checks j <= 3. In the foreach loop: Used to iterate over collections like arrays. Each element in the fruits array is printed one by one.
YouTube Link
Seo Title
Meta Keywords
Meta Description
Introduction to C#
Edit
Variables and Data Types in C#
Edit
Operators in C#
Edit
Control Statements in C# – if, else, switch, and loops Explained
Edit
Loops in C# – for, while, do while, and foreach
Edit
Jump Statements in C# – break, continue, return Explained with Examples
Edit
Conditional Statements in C# if, else if, else, switch Explained with Examples
Edit
Loops in C# – For, While, and Do While with Examples
Edit
Hrjejd
Edit