Meaning
-
What would the following conditional (i+n) in a for loop evaluate to? for (i=0; i + n; i--) I saw it once, but I dont understand what was happening. Did I post well? Rate it! Did I post badly? Rate that too!
0 if
n
starts at 0. Otherwise non-zero, thus thefor
loop would never terminate.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
-
0 if
n
starts at 0. Otherwise non-zero, thus thefor
loop would never terminate.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
-
I want to believe you, but I cannot. int k = 0; int i, n=100; for (i=0; i + n; i--) k++; When this is done, k will equal 100. Can you explain? Did I post well? Rate it! Did I post badly? Rate that too!
The for test is: i + n which is the same as: i + n != 0 n = 100 so we can rewrite as: i + 100 != 0 or (subtract 100 from both sides), i != -100 so as i starts at 0 and decrements by 1 on each iteration it will loop 100 times. ...cmk Save the whales - collect the whole set
-
I want to believe you, but I cannot. int k = 0; int i, n=100; for (i=0; i + n; i--) k++; When this is done, k will equal 100. Can you explain? Did I post well? Rate it! Did I post badly? Rate that too!
It helps to show all of the code up front. Since
n
starts out as100
, rather than 0 as I initially presumed,i + n
will eventually evaluate to 0 which will terminate the loop.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
-
What would the following conditional (i+n) in a for loop evaluate to? for (i=0; i + n; i--) I saw it once, but I dont understand what was happening. Did I post well? Rate it! Did I post badly? Rate that too!
in C/C++, booleans can be reprensented the following way :
0 equals to
false
any other values different from 0 equals totrue
so, as
(i + n)
would equal to 100, it is true - until it become equals to 0... you follow me ? :-D
TOXCCT >>> GEII power
[toxcct][VisualCalc] -
0 if
n
starts at 0. Otherwise non-zero, thus thefor
loop would never terminate.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
-
In the code snippet provided by Esmo2000, when does
n
ever equal -1?
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
-
What would the following conditional (i+n) in a for loop evaluate to? for (i=0; i + n; i--) I saw it once, but I dont understand what was happening. Did I post well? Rate it! Did I post badly? Rate that too!
for n < 0 the loop will execute until i wraps around to abs(n) for n == 0 the loop will never execute (i+0) == 0 == false for n > 0 the loop will execute n times
-
In the code snippet provided by Esmo2000, when does
n
ever equal -1?
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown