If loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Parul Bagadia
    New Member
    • Mar 2008
    • 188

    If loop

    IDE used is Microsoft Visual C++ 6.0
    in the inner loop even when the value of second is 3 it executes that for loop... that should not happen even when we use to conditions for validity of for loop.
    Code:
     
    for(first=0;first<2;first++)
     {
      for(second=1;second<3,column>-1;second++,column--)
      {
       if(first==second)
        second++;   
        store=(matrix[1][first]*matrix[2][second]-matrix[1][second]*matrix[2][first])*(matrix[0][column]);
        if(column==1)
         store=store*-1;
        add=store+add;
      }
     }
    .
    One more thing in Visual C++ 6.0 is -1^2; square of -1 is coming as 3...
    Which function is actually getting evaluated?
    Whats the operator for Exponent?
    I tried with almost all operators.. in all those even % was also unexplainable.
  • Tassos Souris
    New Member
    • Aug 2008
    • 152

    #2
    Check the relational expression in your for loop construct.
    Code:
    second<3,column>-1
    Whether or not the body of the for loop executes depends on the value of column and not on the value of second because you are using the comma operator.
    The comma operator works like this:
    • The expression which is at the left is evaluated first
    • Then the expression on the right is evaluated
    • The value of the expression that was evaluated last (the right one) is returned

    So, what you get in your for loop relational expression is a true or false value whether column is greater than -1 or not, cause that expression is evaluated last. The expression second < 3 is evaluated but the result has no impact on the for relational expression.

    The ^ operator is the bitwise exclusive-or operator; surely not what you want. Take a look at the <math.h> header file.

    Comment

    • Bassem
      Contributor
      • Dec 2008
      • 344

      #3
      Both for loop's conditions have to be True to end it.
      So if column is 5 the loop will be executed for 5 times, because 5 is greater than 3 - the condition of second.
      Now you have 2 probabilities
      1- column < 3 the loop will executed 3 times
      2- column > 3 the loop will executed column times

      In math.h, you'll find the most mathematical operations used; pow, exp, ...etc.

      Comment

      Working...