What will be the output of the following C# code?

static void Main(string[] args)
  {
      int i = 1, j;
      do
      {
          for (j = 1; ; j++)
          {
              if (j > 2)
                  break;
              if (i == j)
                  continue;
              Console.WriteLine(i + " " + j);
          }
          i++;
      } while (i < 3);
      Console.ReadLine();
  }

a)1 2 2 1

b)2 1 1 2

c)1 3 2 1

d)1 1 2 1

1 thought on “What will be the output of the following C# code?”

  1. a
    Explanation: for i = 1. When control enters in loop first if condition is checked for where j = 1 and as (j > 2) which is false. Control is now passed to console statement with i = 1 and j = 2. Now, in while condition value of ā€˜iā€™ reflected is 2 i.e i = 2 as i++. Since, (i < 3) control again enters in for loop with i = 2 but j = 1 not j = 2 for j++ and hence, again same condition executes for console statement.
    Output : 1 2
    2 1

Leave a Comment