Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
Why we need nested loop and what is its purpose
Posted 23-Nov-12 22:42pm
ALIWAZ407
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You use a nested loop for a lot of reasons: Think about it in real life.
When you read a book, you are using a loop:
foreach page in book
   {
   read page
   }
But reading a page involves a loop as well:
foreach line in page
   {
   read line
   }
And to read a line, you need to read each word. and for each word, you need to read each character.
 
Computing is the same: you want to do a task, which needs you to loop through items. Each item needs further processing which requires you to loop though smaller portions.
 
for (i = 0; i < linesCount; i++)
   {
   char* line = lines[i];
   for (j = 0; j < line.Length; j++)
      {
      // Do somethign with each character of the line
      }
   }
  Permalink  
Comments
Nelek at 24-Nov-12 5:40am
   
The power of simplicity. +5
Mohibur Rashid at 24-Nov-12 9:16am
   
brilliant answer, +5
Albert Holguin at 25-Nov-12 1:06am
   
Excellent answer Griff +5
Mohd Imran Saifi at 25-Nov-12 2:09am
   
Nice Example
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

As an example, you might find nested loops useful for creating a table:
  int table[10][10];
  for (i=1; i<=10; i++)
   for (j=1; j<=10; j++)
     table[i][j] = i*j;
The above C code creates the classical school multiplication table.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

There is the lots of thing where we need to use nested loop... try the different sorting algorithm in c, c++ in which u must have to use nested loop.
In matrix multiplication also u have to use nested loop.
in short.. you can implement nested loop as per your need.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Gihan Liyanage 332
1 Sergey Alexandrovich Kryukov 330
2 ClimerChinna 222
3 OriginalGriff 206
4 Richard MacCutchan 170
0 Sergey Alexandrovich Kryukov 8,298
1 OriginalGriff 7,112
2 CPallini 2,598
3 Richard MacCutchan 2,005
4 Abhinav S 1,788


Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 24 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100