C# For Loop

HTML
CSS
Bootstrap
JavaScript
C#
SQL
Salesforce Admin
Exercise
Study Material

For and Foreach Loop in C#

For Loop

In C#, a for loop is a control flow statement that allows you to execute a block of code repeatedly based on a specified condition. It’s typically used when you know how many times you want to repeat a block of code.

Basic structure:

for (initialization; condition; increment/decrement)

        {

            // Code block to execute

        }

For loop Statement

using System;

public class Forloopexample

{

    public static void Main(string[] args)

    {

        // Calculate the sum of numbers from 1 to 5

        int sum = 0;

        for (int j = 1; j <= 5; j++)

        {

            sum += j;

        }

        Console.WriteLine(“Sum of numbers from 1 to 5: ” + sum);

    }

}

Output : Sum of numbers from 1 to 5: 15

Foreach Loop

In C#, the foreach loop is used to iterate over elements in a collection or array. It simplifies the process of iterating through each element of a collection without needing to keep track of indices or the length of the collection manually.

Basic structure :

foreach (var item in collection)

        {

            // Statements to be executed for each item in the collection

        }

Foreach Loop Statement

using System;

public class ForEachLoopExample

{

    public static void Main(string[] args)

    {

        int[] numbers = { 14, 8, 21, 5, 37, 2 };

        // Assume the first number in the array as the maximum

        int max = numbers[0];

        // Iterate over each number in the array

        foreach (var number in numbers)

        {

            // Check if the current number is greater than the current maximum

            if (number > max)

            {

                // If yes, update the maximum

                max = number;

            }

        }

        // Print the maximum value found

        Console.WriteLine(“The maximum value in the array is: ” + max);

    }

}

Output: The maximum value in the array is: 37

Course Video

Practices Tasks

1. You need to create a program that prints the multiplication table of a given number using a for loop. The program should display the multiplication table from 1 to 10 for the specified number.

First you have to take input from console

After giving input you have to show like this same as below 

2. Write a program in C# to show all even numbers between 1 to 10 using a for loop. 

Iterate through numbers from 1 to 10 using a for loop and then check in if condition that

 i % 2 == 0 if yes then show in console like same as below 

3. You have a list of products that a customer has added to their cart, So  you have to check collection of items which added in their shopping cart by using foreach loop.

First you have to create a list of items in a string format and then show ouput Same as below using foreach loop

4. Use a foreach loop to iterate over a list of employee objects and calculate their monthly salaries based on hourly rates and hours worked.

First you have to create a list of employee and define in list name rates hours and workly hours then you have to multiply  hourly rates and hourly worked and then ouput should print same as below