# A nested loop puzzle

• Difficulty Level : Medium
• Last Updated : 02 Jul, 2021

Which of the following two code segments is faster? Assume that compiler makes no optimizations.

Take a step-up from those "Hello World" programs. Learn to implement data structures like Heap, Stacks, Linked List and many more! Check out our Data Structures in C course to start learning today.

## C

 `/* FIRST */``for``(i=0;i<10;i++)``  ``for``(j=0;j<100;j++)``    ``//do something`

## Python3

 `# FIRST``for` `i ``in` `range``(``10``):``    ``for` `j ``in` `range``(``100``):``        ``#do something` `# This code is contributed by shivani`

## C

 `/* SECOND */``for``(i=0;i<100;i++)``  ``for``(j=0;j<10;j++)``    ``//do something`

## Python3

 `# SECOND``for` `i ``in` `range``(``100``):``    ``for` `j ``in` `range``(``10``):``      ` `      ``# Do something` `# This code is contributed by shivani`

Both code segments provide same functionality, and the code inside the two for loops would be executed same number of times in both code segments.
If we take a closer look then we can see that the SECOND does more operations than the FIRST. It executes all three parts (assignment, comparison and increment) of the for loop more times than the corresponding parts of FIRST:

1. The SECOND executes assignment operations ( j = 0 or i = 0) 101 times while FIRST executes only 11 times.
2. The SECOND does 101 + 1100 comparisons (i < 100 or j < 10) while the FIRST does 11 + 1010 comparisons (i < 10 or j < 100).
3. The SECOND executes 1100 increment operations (i++ or j++) while the FIRST executes 1010 increment operation.

Below code counts the number of increment operations executed in FIRST and SECOND, and prints the counts.

## C++

 `// C++ program to count number of increment``// operations in FIRST and SECOND``#include` `using` `namespace` `std;` `int` `main()``{``  ``int` `c1 = 0, c2 = 0;``   ` `  ``/* FIRST */``  ``for``(``int` `i=0;i<10;i++,c1++)``    ``for``(``int` `j=0;j<100;j++, c1++);``     ` `   ` `  ``/* SECOND */``  ``for``(``int` `i=0; i<100; i++, c2++)``      ``for``(``int` `j=0; j<10; j++, c2++);``        `  `  ``cout << ``" Count in FIRST = "` `<

## Java

 `// Java program to count number of increment``// operations in FIRST and SECOND` `import` `java.io.*;` `class` `GFG {``    ``public` `static` `void` `main (String[] args) {``       ``int` `c1 = ``0``, c2 = ``0``;` `        ``for``(``int` `i=``0``; i<``10``;i++, c1++){``          ``for``(``int` `j=``0``;j<``100``;j++, c1++){}``        ``}``        ` `    ` `        ``for``(``int` `i=``0``;i<``100``;i++, c2++){``              ``for``(``int` `j=``0``;j<``10``;j++, c2++){}``        ``}``                ` `        ``System.out.println( ``"Count in First = "``+c1);       ``        ``System.out.println( ``"Count in SECOND = "``+c2);``    ``}``}`

## Python3

 `# Python program to count number of increment``# operations in FIRST and SECOND``c1 ``=` `0``c2 ``=` `0` `# FIRST``for` `i ``in` `range` `(``10``):``    ``for` `j ``in` `range` `(``100``):``        ``c1 ``+``=` `1``    ``c1 ``+``=` `1` `# SECOND``for` `i ``in` `range` `(``100``):``    ``for` `j ``in` `range` `(``10``):``        ``c2 ``+``=` `1``    ``c2 ``+``=` `1``  ` `print``(``"Count in FIRST = "` `,c1)``print``(``"Count in SECOND  = "` `,c2)` `# This code is contributed by shivanisinghss2110`

## C#

 `// C# program to count number of increment``// operations in FIRST and SECOND``using` `System;` `class` `GFG{``    ` `public` `static` `void` `Main (String[] args)``{``   ``int` `c1 = 0, c2 = 0;` `    ``for``(``int` `i = 0; i < 10; i++, c1++)``    ``{``        ``for``(``int` `j = 0; j < 100;j++, c1++){}``    ``}``    ` `    ``for``(``int` `i = 0; i < 100; i++, c2++)``    ``{``        ``for``(``int` `j = 0; j < 10; j++, c2++){}``    ``}``            ` `    ``Console.WriteLine(``"Count in First = "` `+ c1);       ``    ``Console.WriteLine(``"Count in SECOND = "` `+ c2);``}``}` `// This code is contributed by shivanisinghss2110`

## Javascript

 ``
Output
``` Count in FIRST = 1010
Count in SECOND  = 1100```

Below code counts the number of comparison operations executed by FIRST and SECOND

## C++

 `//program to count the number of comparison``//operations executed by FIRST and SECOND */``#include` `using` `namespace` `std;` `int` `main()``{``   ``int` `c1 = 0, c2 = 0;``    ` `   ``/* FIRST */``   ``for``(``int` `i=0; ++c1&&i<10; i++)``      ``for``(``int` `j=0; ++c1&&j<100;j++);``     `  `   ``/* SECOND */``   ``for``(``int` `i=0; ++c2&&i<100; i++)``      ``for``(``int` `j=0; ++c2&&j<10; j++);``      `  `   ``cout << ``" Count fot FIRST  "` `<

## Java

 `/*package whatever //do not write package name here */` `import` `java.io.*;` `class` `GFG {``    ``public` `static` `void` `main (String[] args) {``         ``int` `c1 = ``0``, c2 = ``0``;` `        ``for``(``int` `i=``0``; i<++c1 && i<``10``;i++) {``              ``for``(``int` `j=``0``;j<++c1 &&j<``100``;j++) {                 ``              ``}``        ``}` `        ``for``(``int` `i=``0``;i<++c2 && i<``100``;i++) {``              ``for``(``int` `j=``0``;j<++c2 &&j<``10``;j++) {        ``              ``}``        ``}``      ` `        ``System.out.println( ``"Count in FIRST = "``+c1);       ``        ``System.out.println( ``"Count in SECOND  = "``+c2);   ` `    ``}``}`

## Python3

 `#program to count the number of comparison``#operations executed by FIRST and SECOND */` `# FIRST``c1 ``=` `1``c2 ``=` `1``i ``=` `0``while` `(i < c1 ``and` `i < ``10``):``    ``j ``=` `-``1``    ``c1 ``+``=` `1``    ``while` `(j < c1 ``and` `j < ``100``):``        ``c1 ``+``=` `1``        ``j ``+``=` `1``    ``i ``+``=` `1` `# SECOND``i ``=` `0``while` `(i < c2 ``and` `i < ``100``):``    ``j ``=` `-``1``    ``c2 ``+``=` `1``    ``while` `(j < c2 ``and` `j < ``10``):``        ``c2 ``+``=` `1``        ``j ``+``=` `1``    ``i ``+``=` `1``      ` `print``(``" Count fot FIRST  "` `, c1)``print``(``" Count fot SECOND  "` `, c2)` `# This code is contributed by shivanisinghss2110``  `

## C#

 `/*package whatever //do not write package name here */` `using` `System;` `class` `GFG {``    ``public` `static` `void` `Main (String[] args) {``         ``int` `c1 = 0, c2 = 0;` `        ``for``(``int` `i = 0; i < ++c1 && i < 10; i++)``        ``{``              ``for``(``int` `j = 0; j < ++c1 && j < 100; j++)``              ``{                 ``              ``}``        ``}` `        ``for``(``int` `i = 0; i < ++c2 && i < 100; i++) {``              ``for``(``int` `j = 0; j < ++c2 && j < 10; j++) {        ``              ``}``        ``}``      ` `        ``Console.WriteLine( ``"Count in FIRST = "``+c1);       ``        ``Console.WriteLine( ``"Count in SECOND  = "``+c2);   ` `    ``}``}` `// This code is contributed by shivanisinghss2110`

## Javascript

 ``
Output
``` Count fot FIRST  1021
Count fot SECOND  1201```

Thanks to Dheeraj for suggesting the solution.