Thread: Program Jumps Between Cases Without Reason

  1. #1
    Registered User
    Join Date
    Aug 2008
    Location
    Delaware
    Posts
    47

    Program Jumps Between Cases Without Reason

    This program seems to jump between cases without any reason.


    Code:
    INDEX = 1;
    
    
    While(1) {
    
    switch (INDEX){
    				
    case 1:				//INDEX = 1	
    									
    if (ABC < LOOKUP3(TPS_INDEX, OneUP, 10)){
    						
    INDEX = 1;		
    						
    HIGH(29);						
    						
    LOW1(20);						
    						
    LOW1(23);						
    						
    LOW1(25);						
    								
    					
    
    }		//END IF
    						
    else {
    INDEX = 2;			
    						
    HIGH(29);						
    HIGH1(20);					
    LOW1(23);							
    LOW1(25);						
    }	//END ELSE
    
    break;	//END GEAR INDEX = 1
      												
     case 2:				//INDEX = 2	
      												
    if (ABC >= LOOKUP3(TPS_INDEX, TwoUP, 10)){
      						
    INDEX = 3;
    
    LOW(29);						
    HIGH1(20);						
    LOW1(23);							
    LOW1(25);						
    
    }	//END IF
    								
    else {
     													
     if (ABC < LOOKUP3(TPS_INDEX, TwoDOWN, 10)){
      						
    INDEX = 1;
    										
    HIGH(29);					
    LOW1(20);						
    LOW1(23);						
    LOW1(25);						
    }	//END IF
    }	//END ELSE
    break;	//END INDEX = 2
      						
    case 3:				//INDEX = 3	  				 		
    if (ABC >= LOOKUP3(TPS_INDEX, ThreeUP, 10)){
    						   						
    LOW(29);						//SS1
    LOW1(20);						//SS2
    LOW1(23);						//CCS	
    LOW1(25);						//TCS
    												
    }	//END IF	
     						
    else {
      				
    if (ABC < LOOKUP3(TPS_INDEX, ThreeDOWN, 10)){
      						
    INDEX = 2;
    
    HIGH(29);						
    HIGH1(20);						
    LOW1(23);							
    LOW1(25);	
    					
    }  //END IF
    }	//END ELSE	
    break;	//END INDEX = 3
      					
    case 4:				//INDEX = 4	  						
    if (RPM < LOOKUP3(TPS_INDEX, FourDOWN, 10)){
      						
    INDEX = 3;											
    LOW(29);						
    HIGH1(20);								
    LOW1(23);						
    LOW1(25);					
    }	//END IF
    else	{if (ABC >= 850){
    HIGH1(25);									
    } //END ELSE
    
    else  {if (ABC < 700){
    LOW1(25);
    																						
    }}}				//END If End ELSE End Else
    							// END INDEX = 4:
    						}  //End Switch INDEX 					 
     
    
    					WAIT(100);
    	
    	}					//End While
    Do you need the last break at the end of the case statements?
    Does the program jump to the end of the program and then follow the while loop when a break is encountered, or to the end of the case statements?
    Do I need a Default Statement?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Perhaps if you indented it properly, you might figure something out for yourself.
    Indent style - Wikipedia, the free encyclopedia

    It's AWFUL!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Location
    Delaware
    Posts
    47
    Sorry, I copied it from Notepad++.

    All the indentations got messed up. Notepad++ tracks the brackets.

    I'll try to repost it with indentations.

  4. #4
    Registered User
    Join Date
    Aug 2008
    Location
    Delaware
    Posts
    47
    I hope that this is better!

    Code:
    int INDEX = 1;
    
    
    While(1) {
    
    	switch (INDEX){
    				
    	case 1:				//INDEX = 1	
    									
    		if (ABC < LOOKUP3(TPS_INDEX, OneUP, 10)){
    						
    		INDEX = 1;		
    						
    		HIGH(29);						
    						
    		LOW1(20);						
    						
    		LOW1(23);						
    						
    		LOW1(25);						
    								
    					
    
    	}		//END IF
    						
    	else {
    		INDEX = 2;			
    						
    		HIGH(29);						
    		HIGH1(20);					
    		LOW1(23);							
    		LOW1(25);						
    	}	//END ELSE
    
    break;	//END INDEX = 1
      												
     	case 2:				//INDEX = 2	
      												
    		if (ABC >= LOOKUP3(TPS_INDEX, TwoUP, 10)){
      						
    		INDEX = 3;
    
    		LOW(29);						
    		HIGH1(20);						
    		LOW1(23);							
    		LOW1(25);						
    
    		}	//END IF
    								
    		else {
     													
     			if (ABC < LOOKUP3(TPS_INDEX, TwoDOWN, 10)){
      						
    			INDEX = 1;
    										
    			HIGH(29);					
    			LOW1(20);						
    			LOW1(23);						
    			LOW1(25);						
    			}	//END IF
    		}	//END ELSE
    break;	//END INDEX = 2
      						
    case 3:				//INDEX = 3	  				 		
    		if (ABC >= LOOKUP3(TPS_INDEX, ThreeUP, 10)){
    						   						
    		LOW(29);						//SS1
    		LOW1(20);						//SS2
    		LOW1(23);						//CCS	
    		LOW1(25);						//TCS
    												
    		}	//END IF	
     						
    		else {
      				
    			if (ABC < LOOKUP3(TPS_INDEX, ThreeDOWN, 10)){
      						
    			INDEX = 2;
    
    			HIGH(29);						
    			HIGH1(20);						
    			LOW1(23);							
    			LOW1(25);	
    					
    			}  //END IF
    		}	//END ELSE	
    break;	//END INDEX = 3
      					
    case 4:				//INDEX = 4	  						
    		if (ABC < LOOKUP3(TPS_INDEX, FourDOWN, 10)){
      						
    		INDEX = 3;											
    		LOW(29);						
    		HIGH1(20);								
    		LOW1(23);						
    		LOW1(25);					
    		}	//END IF
    		else	{if (ABC >= 850){
    			HIGH1(25);									
    			} //END IF
    
    		else  {if (ABC < 700){
    			LOW1(25);
    																						
    		}}}				//End IF End ELSE End Else
    							// END INDEX = 4:
    		}  //End Switch INDEX 					 
      					
    
    					WAIT(100);
    	
    	}					//End While
    Last edited by danlee58; 12-31-2014 at 07:42 PM.

  5. #5
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Here is a clean up of your code:
    Code:
    int INDEX = 1;
    
    while(1) {
    
        switch (INDEX){
    
            case 1:             //INDEX = 1
                if (ABC < LOOKUP3(TPS_INDEX, OneUP, 10)) {
                    INDEX = 1;
                    HIGH(29);
                    LOW1(20);
                    LOW1(23);
                    LOW1(25);
                }               //END IF
                else {
                    INDEX = 2;
                    HIGH(29);
                    HIGH1(20);
                    LOW1(23);
                    LOW1(25);
                }               //END ELSE
                break;          //END GEAR INDEX = 1
    
            case 2:             //INDEX = 2
                if (ABC >= LOOKUP3(TPS_INDEX, TwoUP, 10)) {
                    INDEX = 3;
                    LOW(29);
                    HIGH1(20);
                    LOW1(23);
                    LOW1(25);
                }               //END IF
                else {
                    if (ABC < LOOKUP3(TPS_INDEX, TwoDOWN, 10)) {
                        INDEX = 1;
                        HIGH(29);
                        LOW1(20);
                        LOW1(23);
                        LOW1(25);
                    }           //END IF
                }               //END ELSE
                break;          //END INDEX = 2
    
            case 3:             //INDEX = 3
                if (ABC >= LOOKUP3(TPS_INDEX, ThreeUP, 10)) {
                    LOW(29);    //SS1
                    LOW1(20);   //SS2
                    LOW1(23);   //CCS
                    LOW1(25);   //TCS
                }               //END IF
                else {
                    if (ABC < LOOKUP3(TPS_INDEX, ThreeDOWN, 10)) {
                        INDEX = 2;
                        HIGH(29);
                        HIGH1(20);
                        LOW1(23);
                        LOW1(25);
                    }           //END IF
                }               //END ELSE
                break;          //END INDEX = 3
    
            case 4:             //INDEX = 4
                if (RPM < LOOKUP3(TPS_INDEX, FourDOWN, 10)) {
                    INDEX = 3;
                    LOW(29);
                    HIGH1(20);
                    LOW1(23);
                    LOW1(25);
                }               //END IF
                else {
                    if (ABC >= 850) {
                        HIGH1(25);
                    }           //END IF
                    else {
                        if (ABC < 700) {
                            LOW1(25);
                        }       // End IF
                    }           // End ELSE
                }               // End Else
                                // END INDEX = 4
    
        }                       //End Switch INDEX
    
        WAIT(100);
    }                           //End While
    No, you don't need the break statement in the last case and you don't need a default case.
    If the program achieve a break statement in a case, it will jump out of switch.
    In your case, it will fall to the line 'WAIT(100);'.
    At the end of while loop, it jump back to the begin of while loop and execute the switch again.

    I don't know what you are try to do.
    I see only a switch in an infinity loop.
    How you plan to break the while loop?
    Last edited by WoodSTokk; 12-31-2014 at 07:46 PM.
    Other have classes, we are class

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The switch tests the variable INDEX. Particular cases change the value of INDEX. All of this is in a "while (1)" loop so, after changing value of INDEX, the switch statement will be exercised for the new value of INDEX.


    You need to learn how to copy code into forums so it is readable. Otherwise you're relying on people to give a sensible answer when given wrong information.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Aug 2008
    Location
    Delaware
    Posts
    47
    It's an Infinite Loop. It tests TPS_INDEX against different tables, based on INDEX, and changes INDEX based on the results of the test. It's a basic Elevator program, where INDEX is the current floor, and it goes up or down based on TPS_INDEX and the proper table for the floor.

    I have a working version, but as I add IO operations, it jumps around.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    So you've created a forum thread because your code is doing what it is supposed to do, but you don't understand what it is supposed to do.

    People here can't help you. Your code "jumps around" because that's what it is supposed to do. If you don't understand that, the only solution is to work to understand the code, and reason about what happens when you change things.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    Registered User
    Join Date
    Aug 2008
    Location
    Delaware
    Posts
    47
    It's only supposed to jump as TPS_INDEX changes, but it changes when TPS_INDEX is steady.

    I got an answer to my question. I don't need a break or default after the last case. My Switch/Case code is OK.

    My next step is to reduce the IO operations to determine when it becomes stable.
    Last edited by danlee58; 12-31-2014 at 09:52 PM.

  10. #10
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Ah, okay, now the things become a little bit clearer.
    In case 2 (and also in case 3), you have a if-statement in an else-statement. You can join this like:
    Code:
    …
                }               //END IF
                else if (ABC < LOOKUP3(TPS_INDEX, TwoDOWN, 10)) {
                    INDEX = 1;
                    HIGH(29);
                    LOW1(20);
                    LOW1(23);
                    LOW1(25);
                }               //END ELSE IF
    …
    You can shorting the else-statement in case 4.
    It make the code better readable.
    Code:
    …
                else {
                    if (ABC >= 850) HIGH1(25);
                    else if (ABC < 700) LOW1(25);
                }               // End Else
    …
    What i miss is switching to INDEX 4.
    I suspect it goes to the if-statement in case 3.
    Code:
    …
            case 3:             //INDEX = 3
                if (ABC >= LOOKUP3(TPS_INDEX, ThreeUP, 10)) {
                    INDEX = 4;
                    LOW(29);    //SS1
                    LOW1(20);   //SS2
                    LOW1(23);   //CCS
                    LOW1(25);   //TCS
                }               //END IF
    …
    But this is only a guess.
    Last edited by WoodSTokk; 01-01-2015 at 04:00 AM.
    Other have classes, we are class

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program exits for no reason ?
    By noririco in forum C Programming
    Replies: 12
    Last Post: 05-02-2014, 06:32 PM
  2. Strange Behaviour - Program not stopping in some cases.
    By stepp92 in forum C++ Programming
    Replies: 3
    Last Post: 01-18-2014, 01:11 PM
  3. For some reason, my program keeps a newline in an integer
    By Shlomi Ardan in forum C Programming
    Replies: 1
    Last Post: 11-03-2012, 04:31 PM
  4. Replies: 4
    Last Post: 08-08-2005, 03:12 PM
  5. C++ program jumps for noobie
    By bandito9111 in forum C++ Programming
    Replies: 3
    Last Post: 03-01-2003, 01:48 AM