如果有一个外部for循环,一个内循环和一个while循环,我怎么知道控制流在哪里? [英] How do i know where the control flows if there is one outer for loop, an inner loop and one while loop?

查看:113
本文介绍了如果有一个外部for循环,一个内循环和一个while循环,我怎么知道控制流在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

各位大家好,我一直在学习C语言,而且我被困在模式程序中。以下是模式:

Hello everyone, I've been learning C language and I am stuck on a pattern program. Here is the pattern:

        1
      2 3 2
    3 4 5 4 3
  4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5







我是这个网站的新手所以我不知道这个模式是否正确粘贴在这里。假设它确实如此,这是它的代码:






I'm new to this site so I don't know if the pattern got correctly pasted here. Assuming it did, here is it's code:

#include <stdio.h>
int main()
{
   int i,space,rows,k=0,count=0,count1=0;
   printf("Enter the number of rows: ");
   scanf("%d",&rows);
   for(i=1;i<=rows;++i)   
   {
       for(space=1;space<=rows-i;++space)
       {
          printf("  ");
          ++count;
        }
        while(k!=2*i-1)
        {
           if (count<=rows-1) 
           {
             printf("%d ",(i+k));
             ++count;
           }
           else
           {
             ++count1;
              printf("%d ", (i+k-2*count1)); 
           }
           ++k;
        }
        count1=count=k=0;
        printf("\n");
    }
    return 0;
}





我在这段代码中理解的是,如果外部for循环1次,内部循环将执行(行-i)次。那么我不明白的是打印空格后,计数值将如何改变++计数。控制器将如何在此代码中流动,因为还有一个while循环。

我假设首先是内部循环和外部循环将执行。但是,如果它以这种方式进行,那么while循环将如何执行。



我已经完成了基本模式。现在我坚持这个。请帮我。我将非常感激。谢谢:)



我尝试了什么:



我有试图搜索循环的控制流但我没有找到任何有用的东西可以帮助我理解这个问题。



What I understand in this code is if the outer for loop 1 time, the inner loop will execute (rows-i) times. Then what I don't understand is after printing the blank space, how will the value of count change by ++count. And how will the control flow in this code,'cause there is a while loop too.
What I'm assuming is first inner and outer loop will execute. But if it goes that way how will while loop get executed.

I have done the basic patterns. Now I'm stuck on this. Please help me. I will be really grateful. Thank you :)

What I have tried:

I have tried searching the control flow of loops but I didn't find anything helpful which could have helped me in understanding this problem.

推荐答案

好的,让我们撕掉额外的东西只看一下流量控制的东西:

OK, let's rip out the "extras" and just look at the flow control stuff:
for(i=1;i<=rows;++i)
{
    for(space=1;space<=rows-i;++space)
    {
    }
    while(k!=2*i-1)
    {
       if (count<=rows-1)
       {
       }
       else
       {
       }
    }
}

所以你有一个外循环:

So you have an outer loop:

for(i=1;i<=rows;++i)
{
    ...
}

其中包含两个内部循环:

Which contains two inner loops:

for(space=1;space&lt;=rows-i;++space)
{
}
while(k!=2*i-1)
{
   ...
}

一个接一个地执行 - 第一个 for 循环运行,然后 while 循环在 for 循环完成时运行。

在第二个循环中,你有一个条件,这意味着每次循环都将执行一方或另一方。

尝试:使用调试器,并在代码的第一行放置一个断点。当您运行程序时,它会在遇到断点时停止并让您决定下一步该做什么。简单地单步执行每个留置权将显示正在发生的事情!

(我无法告诉您如何设置断点或步行,因为这取决于您使用的是哪个开发IDE - 谷歌的断点和你的IDE的名称,你应该很容易找到它。)

Which are executed one after the other - the first for loop runs, then the while loop runs when the for loop is complete.
Inside the second loop, you have a conditional which means that one side or the other will be executed each time it goes round.
Try it: use the debugger, and put a breakpoint on the first line of the code. When you run your program, it will stop when it hits the breakpoint and let you decide what to do next. Simply stepping each lien through will show you exactly what is happening!
(I can't tell you how to set a breakpoint or step a line, because that will depend on which development IDE you are using - google for "breakpoint" and the name of your IDE and you should find it pretty easily).


你应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到代码的确在做什么。

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbra ins.com/idea/help/debugging-your-first-java-application.html [ ^ ]
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see what the code is really doing.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]


这篇关于如果有一个外部for循环,一个内循环和一个while循环,我怎么知道控制流在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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