V
V
Vlad Gromov2018-03-14 21:37:19
C++ / C#
Vlad Gromov, 2018-03-14 21:37:19

(C#) How does the for loop work in a specific example?

Help me understand this "simple example" from Schildt

// Простой пример применения параметра. 

using System;
class ChkNum
{

    // Возвратить значение true, если значение 
    // параметра х окажется простым числом. 

    public bool IsPrime(int x)
    {
        if (x <= 1) return false;
        for (int i = 2; i <= x / i; i++)
            if ((x % i) == 0) return false;
        return true;
    }
}
class ParmDemo
{
    static void Main()
    {
        ChkNum ob = new ChkNum();
        for (int i = 2; i < 10; i++)
            if (ob.IsPrime(i)) Console.WriteLine(i + " простое число.");
            else Console.WriteLine(i + " непростое число.");
    }
}

Here is the result of running this program.
2 is a prime number.
3 is a prime number.
4 is not an easy number.
5 is a prime number.
6 is not an easy number.
7 is a prime number.
8 is not an easy number.
9 is not an easy number.
first there is a For loop from the parmdemo class and it passes the initial value 2 to IsPrime which uses it as X
then the check starts, X>1 and return does not work
then the for loop starts (int i=2; i <= x/i; i++)
and I can't figure out how the loop is executed if i <= x/i !!!!
it turns out 2<=2/2 or 2<=1
what kind of nonsense
is the idea under such a condition, the loop should not be executed at all,
go further, the if condition begins
we have x=2, i is also equal to 2, it turns out 2/2 the number is divisible without a remainder, that is, the condition if((x % i) == 0) is true
and if if((x % i) == 0) return false ; then false is passed to
if(ob.IsPrime(i)) Console.WriteLine(i + " prime.");
else Console.WriteLine(i + " not a simple number.");
if(ob.IsPrime(i)) can be interpreted as if(ob.IsPrime(i)==true), but we passed the value false, so the next line is executed else Console.WriteLine(i + " not a simple number.") ;
But that's not true, and 2 is a prime number!
Explain, please!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vladimir Vladimirovich, 2018-03-15
@Vikkiners

Here is a test for prime numbers. A prime number is a natural number greater than 1 that has exactly two natural divisors: 1 and itself. We take 2. We look at the code.

for (int i = 2; i <= x / i; i++)
            if ((x % i) == 0) return false;
        return true;

Yes, you said right, the loop will not be executed, that is, the line with If will not be executed, the program will "jump" immediately to the true return. So yes, 2 is a prime number! Those. ob.IsPrime(i)) will return True.
The important thing is that you answered your own question. The break condition in the for loop will not give any iteration, it will return true! Try to step through the program (in VS, it seems, put a breakpoint and then press f11) and you will see for yourself.

C
cicatrix, 2018-03-14
@cicatrix

For x=2:

if (x <= 1) return false;
for (int i = 2; i <= x / i; i++) //   2 > 1 - следующая строка не выолнится
    if ((x % i) == 0) return false;
return true;

F
freeExec, 2018-03-14
@freeExec

So the loop is not executed, YES is returned immediately after it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question