Value of n in Malba Tahan Chess algorithm

Asked

Viewed 23 times

0

I am in doubt about the value of n in the code, in the "for" below 1 is assigned to the value of n, so it would not be for the first iteration the value of n to be 2?

#include <stdio.h>

int main(void){
  float n=0;
  int v=0;


  for(n=1;n>=0 && v!=64;n=n+n){
    v++;
    printf("%i° Quad. tem %.0f grãos\n",v,n );
  }
}
  • 1

    if assigned 1, how could it be 2 in the first interaction? that doesn’t make any sense

  • in the sum n = n + n, n is worth 1 then the value would be 2 would not be?

  • As for "for", the block is only executed after the printf ? If so, it makes sense that n is worth 1 because it was displayed in the first iteration, it would be this?

  • 1

    The for command works as follows: for (valor inicial; condição para continuar no loop; incremento) in case the incremento is executed at the end of the block, as if it were the last command. The condition is evaluated at each beginning of the block and the initial value only once. In fact, the traditional way of making such an increase in C would be: n*=2.

  • Thank you very much, my doubt has been resolved!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.