logo

Boucle C# For

Le C# pour la boucle est utilisé pour parcourir une partie du programme plusieurs fois. Si le nombre d'itérations est fixe, il est recommandé d'utiliser la boucle for plutôt que les boucles while ou do-while.

La boucle for C# est la même que C/C++. Nous pouvons initialiser la variable, vérifier la condition et incrémenter/décrémenter la valeur.

Syntaxe:

 for(initialization; condition; incr/decr){ //code to be executed } 

Organigramme :

C# pour l'organigramme de la boucle

Exemple de boucle C# For

 using System; public class ForExample { public static void Main(string[] args) { for(int i=1;i<=10;i++){ console.writeline(i); } < pre> <p>Output:</p> <pre> 1 2 3 4 5 6 7 8 9 10 </pre> <h2>C# Nested For Loop</h2> <p>In C#, we can use for loop inside another for loop, it is known as nested for loop. The inner loop is executed fully when outer loop is executed one time. So if outer loop and inner loop are executed 3 times, inner loop will be executed 3 times for each outer loop i.e. total 9 times.</p> <p>Let&apos;s see a simple example of nested for loop in C#.</p> <pre> using System; public class ForExample { public static void Main(string[] args) { for(int i=1;i<=3;i++){ for(int j="1;j&lt;=3;j++){" console.writeline(i+' '+j); } < pre> <p>Output:</p> <pre> 1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3 </pre> <h2>C# Infinite For Loop</h2> <p>If we use double semicolon in for loop, it will be executed infinite times. Let&apos;s see a simple example of infinite for loop in C#.</p> <pre> using System; public class ForExample { public static void Main(string[] args) { for (; ;) { Console.WriteLine(&apos;Infinitive For Loop&apos;); } } } </pre> <p>Output:</p> <pre> Infinitive For Loop Infinitive For Loop Infinitive For Loop Infinitive For Loop Infinitive For Loop ctrl+c </pre></=3;i++){></pre></=10;i++){>

Boucle For imbriquée C#

En C#, nous pouvons utiliser une boucle for dans une autre boucle for, elle est connue sous le nom de boucle for imbriquée. La boucle interne est exécutée entièrement lorsque la boucle externe est exécutée une fois. Ainsi, si la boucle externe et la boucle interne sont exécutées 3 fois, la boucle interne sera exécutée 3 fois pour chaque boucle externe, soit un total de 9 fois.

Voyons un exemple simple de boucle for imbriquée en C#.

 using System; public class ForExample { public static void Main(string[] args) { for(int i=1;i<=3;i++){ for(int j="1;j&lt;=3;j++){" console.writeline(i+\' \'+j); } < pre> <p>Output:</p> <pre> 1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3 </pre> <h2>C# Infinite For Loop</h2> <p>If we use double semicolon in for loop, it will be executed infinite times. Let&apos;s see a simple example of infinite for loop in C#.</p> <pre> using System; public class ForExample { public static void Main(string[] args) { for (; ;) { Console.WriteLine(&apos;Infinitive For Loop&apos;); } } } </pre> <p>Output:</p> <pre> Infinitive For Loop Infinitive For Loop Infinitive For Loop Infinitive For Loop Infinitive For Loop ctrl+c </pre></=3;i++){>

Boucle For C# Infinie

Si nous utilisons un double point-virgule dans la boucle for, il sera exécuté une infinité de fois. Voyons un exemple simple de boucle for infinie en C#.

 using System; public class ForExample { public static void Main(string[] args) { for (; ;) { Console.WriteLine(&apos;Infinitive For Loop&apos;); } } } 

Sortir:

 Infinitive For Loop Infinitive For Loop Infinitive For Loop Infinitive For Loop Infinitive For Loop ctrl+c