·  C# – Syntax – Conditional Statements – for

 

- Info:

  - This tutorial shows how to use for conditional statements.

 

- Example:

  - Create C# Console Application Test.cs.

 

    Test.cs

    using System;

   

    class Test {

   

      static void Main(string[] args) {

   

        int i;

        int j;

   

        //SIMPLE.   Output is "1 2 3 4".

        for (i = 1; i <= 4; i++) {

          Console.Write(i);

        }

   

        //COMPLEX.  Output is "1 2 3".

        for (i = 1, j = 5; i <= 4 && j > 2; i++, j--) {

          Console.Write(i);

        }

   

        //BREAK.    Output is "1 2 3" since we break out of for loop before condition i<4 is reached.

        for (i = 1; i <= 4; i++) {

          Console.Write(i);

          if (i == 3) { break; }

        }

   

        //CONTINUE. Output is "1 2 4" since display of number 3 is skipped.

        for (i = 1; i <= 4; i++) {

          if (i == 3) { continue; };

          Console.Write(i);

        }

       

      }

   

    }