用于循环破坏构造 [英] For loop breaking constructs

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

问题描述

嗨专家



我以下面的方式看过循环这么多次,但我不明白这背后使用的逻辑。



Hi Experts

I had seen for loop in the below manner so many time but I am not understand the logic behind this to use in this way.

for(int i=2;i<=num/2;i++)





为什么我们将num / 2除以循环中断?



在下面的程序中都给出相同的输出,但为什么在第一种方法中我们将使用num / 2?





Why we divide num/2 as loop break?

In below program both giving same output but why in first approach we will use num/2?

//First Approach 
public static void IsPrimeNumber1(int num)
        {
            int Count=0;
            for(int i=2;i<=num/2;i++)
            {
                if(num%2==0)
                {
                   Count++;
                    break;
                }
            }
            if(Count==0 && num!=1)
            {
                Console.WriteLine("Prime Number");
            }
            else
            {
                 Console.WriteLine("Not Prime Number");
            }
        }
//Second Approach
 public static  void IsPrimeNumber2(int num)
        {
            int i=2;
            for( i=2;i<=num-1;i++)
            {
                if(i%num==0)
                {
                    Console.WriteLine("Not Prime Number");
                    break;
                }
            }
            if (num == i)
            {
                Console.WriteLine("Prime Number");
            }
            else
            {
                Console.WriteLine("Not Prime Number");
            }
        }









谢谢

Dinesh Sharma



我的尝试:



我试图找到除以2之后的逻辑作为中断循环。





Thanks
Dinesh Sharma

What I have tried:

I tried to find the logic behind divide by 2 as loop for break.

推荐答案

因为开发人员不知道他应该从<$开始c $ c> 3 ,逐步 2 ,并停在 sqrt(num) ;典型的菜鸟错误。
Because the developer doesn't know that he should start at 3, step by 2, and stop at sqrt(num) ; typical rookie mistake.


Quote:




for(int i=2;i<=num/2;i++)



为什么我们将num / 2除以循环中断?


Why we divide num/2 as loop break?

我们没有。

此代码是一个循环,当 i 克服 num / 2 时会中断。



想想你如何检查一个数字是否为素数,你做什么,你什么时候停止?

拿一张纸,一支铅笔写下来你要做什么检查31.



你的程序非常错误,使用调试器查看代码正在做什么。



调试器允许你逐行跟踪执行,检查变量,你会看到有一点它停止了你所期望的。

Debugger - 维基百科,免费百科全书ia [ ^ ]

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



[更新]

顺便说一句,不是尝试一下你已经给出但不起作用的代码, />
尝试写自己的,不能再糟糕了。

学习C的好讲座
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf [ ^ ]

http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf [ ^ ]

We don't.
This code is a loop that breaks when i get over num/2.

Think about how you check if a number is prime, what do you do, when do you stop ?
Take a sheet of paper, a pencil and write down what you do to check 31.

Your program is very buggy, use the debugger to see what the code is doing.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

[Update]
By the way, rather than trying a code that you have been given and don't work,
try to write your own, it can't be worse.
A great lecture to learn C
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]


我相信你的意思是写:

I believe you meant to write:
if(num%i==0)



而不是


Instead of

if(num%2==0)



对于第一种方法。

for循环确实在(num / 2)处中断,因为没有素数可以除以任何一个更大的数字。

对于解决方案#3所说的复杂性来说,这肯定是很好的。循环越早结束,额外检查这段代码越复杂,处理大数字时效率越高。


for the first approach.
The for loop does break at (num/2) because, there is no prime number that can be divided by any number that is greater it's half.
That is surely great for the complexity as Solution #3 stated. the more the loop ends early with out extra checking the less complex this piece of code is and the more efficient it becomes when dealing with large numbers.


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

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