Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

if-statement's Strange Phenomenon

Status
Not open for further replies.

javaguru007

Programmer
Nov 25, 2003
17
PH
hello to all, i just want to ask if any of you have experienced the following :

if ((Table[index].variable1 == CONSTANT_1) &&
(Table[index].variable2 == CONSTANT_2) &&
(Table[index].variable3 == 0))
{
// Do some things
}

At first, this if-statement was working properly, but after sometime (around a couple of days of continous running), it suddenly does not work anymore. We have checked the log files to determine the values being checked, and all of the 3 Statements are true.

Have any of you guys experienced this ? If ever, would you kindly help us out. Thanks.
 
IMHO it's not possible, that the if-statement stops at once to work.
There are more eventualities, where the problem could be -e.g.:
1. The problem could be in your Table-Data, e.g your Table[idx].variable3 was uninitialized, i.e. not equal zero.
2. The problem could be elsewhere in your program logic, so that under given branch conditions the program runs other as you expected.
...
etc.
To find out what causes the "if-statement's Strange Phenomenon" analyze your data and analyze your program using DEBUGGER.
 
thanks for the reply mikrom, sad to say but based on our log files the values in the Table[index] are all valid that is why we are having a hard time troubleshooting this.

our applications is executed in Linux.....
 
What is index - do you print it? Presumably when you first test it, index varies a log. What is the value when it starts failing. Is it something like -1?
 
1. Look if the program reaches your if-statement everytime as you expected. It could be other branch condion before, so your prgram can over given circumstances jump over your if statement:
Code:
if (other_condition){
...
  if ((Table[index].variable1 == CONSTANT_1) &&
      (Table[index].variable2 == CONSTANT_2) &&
      (Table[index].variable3 == 0))
  {
       // Do some things
  }
...
}

2. What are the datatypes of
Table[index].variable1, Table[index].variable2, Table[index].variable3
and how are
CONSTANT_1, CONSTANT_2
defined ?

3. You can refine your if-statement to analyze it better and you an build in more infos writing in your log.
Code:
if (Table[index].variable1 == CONSTANT_1){
    if (Table[index].variable2 == CONSTANT_2){
       if (Table[index].variable3 == 0){
          // Do some things
       }
       else{
         ..
       }
    }
    else {
      ..
    }
} 
else {
  ..
}
or you can try to invert it
Code:
if ((Table[index].variable1 != CONSTANT_1) ||
    (Table[index].variable2 != CONSTANT_2) ||
    (Table[index].variable3 != 0)){
  // do nothing
}
else{
  // Do some things
}
 
Are any of the variables floats / doubles?
Because 1.2345 and 1.2345 may look the same printed out, but if you arrive at the "same" answer via different routes (one is a calculation, the other a constant), then they may look the same, but compare unequal.



--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Thanks for the advice...
to give you guys a more overview, please refer to the code below :
#define IndexMax 512
#define CONSTANT_1 = 1
#define CONSTANT_2 = 0

boolean bOk = false;
while (bOk == false)
{
// Logging Function to reflect in the Log Files that
// this part is being executed.


// Checks if a certain Table in the Table List
// satisfies a certain condition
// and then exits the while loop
for (int index=0; index < IndexMax; index++)
{
// all members are of type int
if ((Table[index].variable1 == CONSTANT_1) &&
(Table[index].variable2 == CONSTANT_2) &&
(Table[index].variable3 == 0))
{
// Do some things
bOk = true;
}
}
}

Based on our Log Files, the index is correct, the values in the Table[index].member are also correct and the for loop is being executed.
 
Doesn't make sense. You can't have
Code:
#define CONSTANT_1 = 1
That would produce a syntax error since
Code:
 if ((Table[index].variable1 == CONSTANT_1)
would decode to
Code:
 if ((Table[index].variable1 == = 1)
Does index max ever change in value?
 
sorry for the mistype in the #define portion,

#define CONSTANT_1 1
#define CONSTANT_2 0

is the correct one and the indexmax does not change as long as the application is running....
 
javaguru007,

But post the definition of your array struct too.
What are the datatypes of
Table[index].variable1,
Table[index].variable2,
Table[index].variable3
???
 
Look - I simplified your code so that I search only for one condition i.e. CONSTANT==0 in a smaller 5-element array
Code:
#include <stdio.h>

#define IndexMax 5
#define CONSTANT 0
int Table[] = {1, 0, 3, 5, 7};
typedef enum{false, true} boolean;

int main()
{ boolean bOK = false;
  int j=1;
  while (bOK == false){
    int index;
    printf("running while loop %d.time\n", j );
    for (index=0; index < IndexMax; index++){
      printf("Table[%d]=%d, ", index, Table[index]);
      if (Table[index] == CONSTANT) {
        // Do some things
        bOK = true;
        //break;
      }
      printf("bOK=%d\n", bOK);
    }
    j++;
  }
  printf("finally: bOK=%d\n",bOK);  
  return 0;
}

The output is
Code:
running while loop 1.time
Table[0]=1, bOK=0
Table[1]=0, bOK=1
Table[2]=3, bOK=1
Table[3]=5, bOK=1
Table[4]=7, bOK=1
finally: bOK=1

I don't see the sense of your while loop: if there is a element in an array that fullfill the if-condition, then the while-loop run only once.
IMHO your for loop is inefficient: The for-loop runs everytime to the IndexMax. If I use the break in the above if-statement, than it would be more efficient.
Code:
running while loop 1.time
Table[0]=1, bOK=0
Table[1]=0, finally: bOK=1
But if there is not such element, then the while-loop runs to infinity, i.e. if i change the
Code:
int Table[] = {1, 10, 3, 5, 7};
I get
Code:
...
running while loop 41558.time
Table[0]=1, bOK=0
Table[1]=10, bOK=0
Table[2]=3, bOK=0
Table[3]=5, bOK=0
Table[4]=7, bOK=0
running while loop 41559.time
Table[0]=1, bOK=0
Table[1]=10, bOK=0
Table[2]=3, bOK=0
Table[3]=5, bOK=0
Table[4]=7, bOK=0
running while loop 41560.time
Table[0]=1, bOK=0
Table[1]=10, bOK=0
...

I'm not sure if I good understand your task, but to search for one array element, which fullfill a condition I don't see the purpose of a while-loop. I would instead use only this
Code:
#include <stdio.h>

#define IndexMax 5
#define CONSTANT 0
int Table[] = {1, 0, 3, 5, 7};
typedef enum{false, true} boolean;

int main()
{ boolean bOK = false;
  int index;
  for (index=0; index < IndexMax; index++){
    printf("Table[%d]=%d, ", index, Table[index]);
    if (Table[index] == CONSTANT) {
      // Do some things
      bOK = true;
      break;
    }
    printf("bOK=%d\n", bOK);
  }
  printf("finally: bOK=%d\n",bOK);  
  return 0;
}
If there is such an element in an array, then the output is
Code:
Table[0]=1, bOK=0
Table[1]=0, finally: bOK=1
and when it isn't, e.g. when we have
Code:
int Table[] = {1, 10, 3, 5, 7};
then the output is
Code:
Table[0]=1, bOK=0
Table[1]=10, bOK=0
Table[2]=3, bOK=0
Table[3]=5, bOK=0
Table[4]=7, bOK=0
finally: bOK=0
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top