Month End Sale: Get Extra 10% OFF on Job-oriented Training! Offer Ending in
D
H
M
S
Get Now

C# Loops and Jump Statements

Level : Beginner
Mentor: Shailendra Chauhan
Duration : 00:03:00

for loop:

A for loop is used for iterating over a sequence (like a list or range) and executing a block of code for each item.

Example:

for (int i = 1; i <= 5; i++)
{
    Console.WriteLine(i);
}

nested for loop:

A nested for loop is a for loop inside another for loop, commonly used for iterating over 2D data structures.

Example:

for (int i = 0; i < 3; i++)
{
    for (int j = 0; j < 2; j++)
    {
        Console.WriteLine($"{i} {j}");
    }
}

infinite for loop:

An infinite for loop runs indefinitely and is often used with a break statement to exit when a certain condition is met.

Example:

while (true)
{
    Console.WriteLine("This is an infinite loop");
    break; // Exit the loop
}

while loop:

A while loop repeatedly executes a block of code as long as a specified condition is true.

Example:

int count = 0;
while (count < 5)
{
    Console.WriteLine(count);
    count++;
}

do while loop:

Some languages have a do-while loop that ensures the block of code is executed at least once before checking the condition.

Example:

int num = 0;
do
{
    Console.WriteLine(num);
    num++;
} while (num < 5);

break statement:

The break statement is used to exit a loop prematurely when a certain condition is met.

Example:

for (int i = 1; i < 10; i++)
{
    if (i == 5)
    {
        break;
    }
    Console.WriteLine(i);
}

continue statement:

The continue statement is used to skip the current iteration of a loop and proceed to the next iteration.

Example:

for (int i = 1; i <= 5; i++)
{
    if (i == 3)
    {
        continue;
    }
    Console.WriteLine(i);
}

goto statement:

The goto statement is used in some programming languages to transfer control to a labeled section of code.

Example:

int main() {
    int i = 1;
start:
    if (i <= 5) {
        std::cout << i << std::endl;
        i++;
        goto start; // This goes back to the "start" label
    }
    return 0;
}
Self-paced Membership
  • 22+ Video Courses
  • 800+ Hands-On Labs
  • 400+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this