关于嵌套循环的问题 [英] Question about nested loops

查看:71
本文介绍了关于嵌套循环的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我们需要嵌套循环及其目的

Why we need nested loop and what is its purpose

推荐答案

您使用嵌套循环有很多原因:在现实生活中考虑它。 />
当你读一本书时,你正在使用一个循环:

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
      }
   }


例如,您可能会发现嵌套循环对创建表非常有用:

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;



上面的 C 代码创建了经典的学校乘法表。


The above C code creates the classical school multiplication table.


我们需要使用嵌套循环的东西很多...尝试c,c ++中的不同排序算法,你必须使用嵌套循环。

矩阵乘法你也必须使用嵌套循环。

简而言之..你可以根据需要实现嵌套循环。
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.


这篇关于关于嵌套循环的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆