This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

else if statement is not evaluated even if the condition is true

Other Parts Discussed in Thread: MSP430FR4133, MSP-EXP430FR4133

I am writing a function having an infinite while loop, with nested if else statements in it.

The 'if else' statements depend on a single variable 'state' which is an unsigned integer.

The value of state is initialized to 1 while declaring it. The if statement when state == 1 is evaluated successfully and changes the value of 'state' to 2, but when value of 'state' becomes 2, the 'else if' statement is not evaluated and the code returns back to main if.

Platform: MSP430fr4133 Launchpad, CCS v6.1.0

The code is as follows (The code is inside a function body with no arguments and no return):

unsigned char receivedChar;

volatile unsigned int state;

state = 1;

while(1)

{

//If queue is not empty

if(!isEmpty(&REC_q))

{

//Get the character from the queue

receivedChar = dequeue(&REC_q);

if(state == 1){ 

 //Do something

state = 2;

}

else if(state == 2){

//Do something

}

}

}

  • Hi Rohan!

    When do you expect the else clause to be executed? In this case here

    if( state == 1 )
    { 
      // Do something
    
      state = 2;
    }
    else if( state == 2 )
    {
      // Do something else
    }

    the else clause will only be executed the next round the program cycles through this if clause and when state is still set to 2. When state was 1 and the program executes the if clause and sets state to 2, then the else clause will not be executed although state is now 2 because the if clause was already executed. If you have

    if( Condition A )
    {
      // Option A
    }
    else if( Condition B )
    {
      // Option B
    }
    else if( Condition C )
    {
      // Option C
    }
    else
    {
      // For all other conditions
    }

    there is always only one clause that will be executed.

    I don't know if this is what you are asking about, so if not, please specify your problem in more detail.

    Dennis

  • In other words: if you want the second if to be executed in the same loop iteration, remove the else.

  • In other words: if you want the second if to be executed in the same loop iteration, remove the else.

    Perhaps you haven't noticed the surrounding while (1) { ... } loop. The 'else if' branch should at least execute in the second cycle of this loop. And the state variable is defined as volatile, so it doesn't look like an optimization issue.

  • In the next loop iteration, another character would have been dequeued.
  • f. m. said:
    Perhaps you haven't noticed the surrounding while (1) { ... } loop.

    Of course we did:

    Dennis Eichmann said:
    executed the next round the program cycles

    Clemens Ladisch said:
    executed in the same loop iteration

    But no one knows what happens between two cycles. This looks more like a very small portion of the code. In addition no one knows what

    receivedChar = dequeue( &REC_q );

    does. Maybe it causes

    if( !isEmpty( &REC_q ) )

    to return FALSE the next cycle.

    Dennis

  • I assume the OP had been debugging through several cycles. He should have noticed if it didn't enter the if(!isEmpty(&REC_q) condition - and tell us.

    In theory, it should be sufficient to check whether the state variable is affected by any other operation (beside the state = 2; assignment).

  • Rohan:
    Try adding "state = 1" into the "else if " - you may actually be getting there, but because you have nothing inside of the braces, the code may look as if it has bypassed...
    Hopefully that helps.
  • Hi Dennis,
    Sorry that I did not clarify this, however, the program does go to else if(state == 2) in the next cycle after the queue is dequeued, but even when state variable value is 2, it is evaluated as false and the code returns to if(!isEmtpy.... ).

    Let me know what you think of this.

    -Rohan
  • Hello,

    I want the condition else if executed only when state becomes 2 in the next cycle of while and if(!isEmpty...).
    I see that the program goes to state == 2, but is not correctly evaluated and returns to while(1) again.

    Let me know.
  • Todd:

    To be more precise, the variable state is not affected by any other thing. The queue is used to get characters from the UART and only when the queue is not empty(i.e. only when the character is received) the control goes inside the if else statements, and there are functions inside the if else statements which depend on value of received character.

    However I don't understand the reason that it is evaluating only the first condition always, it does go to the (state == 2) in the next cycle, but just ignores it and returns back to while(1) to check if the queue is empty or not again.

    Let me know.
    Thanks
  • Dennis, Clemens,

    To summarize in short, the queue is being used to get characters from the UART. When the queue is not empty(i.e. there is received character), the code enters the if else loop. First time state =1, so the (state==1) is evaluated correctly and value of state becomes 2 successfully. The variable state does not change ANYWHERE else.

    Now the second time when there is a received character on the UART, the code enters the if else loop, now it goes to (state==2) condition, and then without evaluating anything it just returns back to while(1) and remains at if(!isEmpty....) till the next character is received.
    Now I tried this multiple times using single stepping, but the condition (state==2) is never evaluated successfully.

    Also, //do something are function calls which depend on the value of character received from the UART and nothing to do with the variable state.
    I hope I am pretty clear now.

    Let me know.

    Thanks!
  • Is there any code inside the else-if?
  • Yes,
    There are functions inside the else if, which send data over the UART.
    However none of those functions change the value of variable 'state'.
    Thanks.
  • I am sending two screenshots as attachment to show what problem I am getting.

    First screenshot - First iteration when the code enters (state == 1) and changes value of state = 2;

    Second screenshot - Second iteration the code enters (state == 2) and returns abruptly to main 'if' loop, without entering the else if loop.

    The variables with their values and locations can be seen in the top right corner. I hope if that helps in answering the question.

    Thanks,

    Rohan L

  • Hi Rohan!

    Are you using the MSP-EXP430FR4133 LaunchPad? If so - would you share your complete project to test it?

    Dennis
  • Hi Dennis,
    Yes, i am using MSP-EXP430fr4133 launchPad. I am a little reluctant to share the whole project due to confidential reasons.

    Even then, let me know if you can help with the information I have provided (Or let me know or any suggestions/ changes that you can suggest in the code to solve this problem).

    Thanks,
    Rohan
  • Hi Rohan!

    Rohan Lele said:
    I am a little reluctant to share the whole project due to confidential reasons.

    If you don't want to share it with everyone in the web, I can offer you the possibility to send it to me as a private message. Although I'm at the Embedded World exhibition at the moment, I do have a MSP-EXP430FR4133 LaunchPad in the car.

    Rohan Lele said:
    Even then, let me know if you can help with the information I have provided

    Unfortunately not, otherwise I would have told you how to solve the problem.

    Dennis

  • All the evaluations of dependent multiple conditional statements are done first, only then according code block executed . So changing variable which is already evaluated will do nothing to execution flow because it is already set. As an illustration here's plain C example printing just "state: 1" but not "state: 3"

    #include <stdio.h>
    
    void main(void) {
      int state = 1;
    
      if (state == 1) {
        printf("state: 1\n");
        state = 3;
      } else if (state == 3) {
        printf("state: 3\n");
      }
    }
    

  • My guess is that there is } at the wrong place, as you are using if and more else if inside the master else if

    Use switch & case and code gets cleaner

    and even allows for instant fall-through if needed by putting break inside the end bracket

    case 4: if (door.openonreturn){  
               door.openonreturn = door.open = 0;                  
               door.state = 2; break;}
                                                                   // instant fall-through if not
    case 5: ...

  • Is the state still equal to 2 when you loop back to top of the infinite loop? Perhaps an Interrupt handler runs, corrupts your state variable (which at 0x27F6) before the test for 2.

    Unlikely but perhaps your code is not getting downloaded to the target. The code that gets run will not match the code in the CCS window.
    Assumes:
    - If all the brackets are all in the rights places
    - All the code prior to the "state==2" body is unchanged.
    - The "state=2" body was added recently.
    Even less likely is that you have worn out your FRAM. and cannot change it anymore.

**Attention** This is a public forum