×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

if-statement's Strange Phenomenon

if-statement's Strange Phenomenon

if-statement's Strange Phenomenon

(OP)
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.

RE: if-statement's Strange Phenomenon

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.
 

RE: if-statement's Strange Phenomenon

(OP)
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.....

RE: if-statement's Strange Phenomenon

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?

RE: if-statement's Strange Phenomenon

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
}
 

RE: if-statement's Strange Phenomenon

Quote:


Presumably when you first test it, index varies a log.
Sorry - I meant varies a lot.

RE: if-statement's Strange Phenomenon

Are any of the variables floats / doubles?
http://c-faq.com/fp/fpequal.html
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.

RE: if-statement's Strange Phenomenon

(OP)
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.

RE: if-statement's Strange Phenomenon

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?

RE: if-statement's Strange Phenomenon

(OP)
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....

RE: if-statement's Strange Phenomenon

javaguru007,

But post the definition of your array struct too.
What are the datatypes of
Table[index].variable1,
Table[index].variable2,
Table[index].variable3
???  

RE: if-statement's Strange Phenomenon

(OP)
the datatypes of the variables are all int....  

RE: if-statement's Strange Phenomenon

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close