Home > Back-end >  The if statement in a for loop a strange question
The if statement in a for loop a strange question

Time:02-28

Here is a piece of code I wrote, aims to integrate the elements in the array nums into an integer, in the process of integration testing is more than an int type of numerical range, once more than it returns 0
 int main (void) 
{
Int x=0;
Int nums [11]=,3,2,1 {0};
Int I=4, j;
Int a=nums [0].

For (j=1; J & lt; i; J++)
{
If (a==0)
{
//if (x * 10 & lt; * 10-2147483648 | | x & gt; 2147483647 | | x * 10 + nums [j] <- 2147483648 | | x * 10 + nums [j] & gt; 2147483647)
If (x * 10 & gt; 2147483647 | | x * 10 + nums [j] & gt; 2147483647)
//return 0;
return 0;
}
//else if (nums [0]==1)//handling of negative
If (a==1)//handling of negative
{
//if (0 - (x * 10) & lt; - 2147483648 | | 0 - (x * 10) & gt; 2147483647 | | 0 - (x * 10 + nums [j]) & lt; - 2147483648 | | 0 - (x * 10 + nums [j]) & gt; 2147483647)
If (0 - (x * 10) & lt; - 2147483648 | | 0 - (x * 10 + nums [j]) & lt; - 2147483648)
return 0;
}
//if (a==0)
//{
//if (x * 10 & gt; 2147483647 l | | x * 10 + nums [j] & gt; 2147483647 l)
//return 0;
//}
X=x * 10 + nums [j];
}

Printf (" % d \ n ", x);
return 0;
}

A weird question is, the program will skip two if statements in the for loop, the last statement in the direct execution cycle, this is how to return a responsibility?
O great god answer

CodePudding user response:

X * 10 & gt; 2147483647 | | x * 10 + nums [j] & gt; 2147483647
Such a statement is true forever, because int never greater than INT32_MAX, if the calculation result is greater than the INT32_MAX, will be back,

CodePudding user response:

It is wrong, should be always false

CodePudding user response:

A=num [0], which is 0, no modified behind a, so under a==1, do not perform this procedure,

CodePudding user response:

This have what strange, like
You made the earth longitude and latitude of their location, and then write a if
Said that if in the east of the east longitude 180 degrees, or on the west side of west longitude 180 degrees or 90 degrees north latitude north, or 90 degrees south latitude to the south, then outputs an error message,
Is this nonsense? The natural is damaged,
As same reason,

CodePudding user response:

Computer mathematics not human, it is just a rough approximation,
This is the current computer architecture can't for the birth of a true artificial intelligence philosophy of reason,

CodePudding user response:

Input, if beyond the scope of int, will rollback, you can use the long long type, if not out of scope, then converted to int

CodePudding user response:

Or in the form of a string of judgment

CodePudding user response:

reference 3 floor truth is right or wrong response:
a=num [0], which is 0, no modified behind a, so under a==1, do not perform this procedure,


I'm curious, doesn't even have a is equal to 0
Prev:c
  • Related