Good day!

// Использовать запятые в операторе цикла for для // выявления наименьшего и наибольшего делителя числа, // не считая само число и единицу. int i, j; int smallest, largest; int num; num = 12; smallest = largest = 1; for (i = 2, j = num/2; (i <= num/2) & (j >= 2); i++, j--) { // Вот здесь не могу понять, какая часть условия выполняется первой, // эта (smallest == 1) & (num % i), а затем результат == 0 или по другому? if ((smallest == 1) & (num % i) == 0) { smallest = i; } if ((largest == 1) & (num % j) == 0) { largest = j; } } Console.WriteLine("The largest Divisor: " + largest); Console.WriteLine("The smallest Divisor: " + smallest); 

    2 answers 2

    You're wrong. Your condition should be broken not into parts (smallest == 1) & (num% i) and the result == 0, but differently. It consists of the following parts:

     (smallest == 1) 

    and

     (num % i) == 0 

    There are three operators: ==, & and%, as well as parentheses. First, the condition is smallest == 1, then num% i, then (num% i) == 0, and the most recent operation will be performed &. since it has the lowest priority of all of the above. The order of calculation can be judged by replacing the operands with methods or properties that output data to the console. So to speak:

     public static int j { get { Console.WriteLine("j"); return 10; } } public static int num { get { Console.WriteLine("num"); return 10; } } public static int i { get { Console.WriteLine("i"); return 10; } } public static int foo { get { Console.WriteLine("foo"); return 10; } } static void Main(string[] args) { if (smallest == j & num % i == foo) { } Console.ReadLine(); } 

    You will get the following output:

     smallest j num i foo 

    That says that the expression smallest == j is calculated first, then num% i, then num% i == foo and at the end the & operator

    • DreamChild, thank you very much. All laid out on the shelves. :) - a66ac

    Googling for half a minute: http://sernam.ru/book_csharp.php?id=32

    • The log operator has the "&" priority 7, and the operator "==" 6, then the operator "==" will be executed from the beginning, I understand correctly? - a66ac
    • Ni55an, thank you. - a66ac