Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

loops and iteration: practical reference

loops and iteration: practical reference: Loops repeat a block until a sequence ends or a condition is met.

Version: C# language and .NET 10 reference runtime

Syntax

// loops

Loops repeat a block until a sequence ends or a condition is met.

Choose foreach for enumeration, for for indexes, and while for a state condition.

Modifying a collection during foreach usually invalidates its enumerator.

A condition that never becomes false creates an infinite loop; break and continue change the path.

The complete program actually uses loops and iteration; its exact expected stdout is 6.

Examples

using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Number = System.Int32;

var sum = 0; foreach (var value in new[] { 1, 2, 3 }) sum += value; Console.WriteLine(sum);

Result

6

The complete program actually uses `loops and iteration`; its exact expected stdout is `6`.

Sources