Thread: breaking a for loop

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    43
    So i know normally that to break out of a for loop you would just simply have to put the code "break" in and that will exit the loop, my issue is a little more complex. What i am doing is i have two mechanical encoders that are four bit 16 position encoders. When i turn one i want the for loop to execute, and when i turn the other one i want to break out of the for loop.

    so here is the code, the code for the encoders are long so i will only be posting the first part, and before every jumps on me i know the switch statement needs to end with a default, and yes i have one programed and my switch statement work great i just need ot figure out to break the for loop with one turn of the encoder.

    The code is not the pretiest, there is a ton of repeats and yes i plan to factorize it and make it better but at this point in my project i need to get all the code working and tested

    also i am using a micro controller the PSoC so the group two address and i2c commands are just how i communicate thorugh the PSoC and the chip i am using

    so here is the code
    Code:
    encoder1=(Bit1_1_Data_ADDR >>4) & 0x0F;
    		encoder2=(Bit1_0_Data_ADDR) & 0x0F;
    		
    		switch(encoder1)
    		{
    			case 0x00:
    				LCD_PrCString("        ");
    				LCD_Position(1,0);
    				LCD_PrCString("Case0");
    				Police_Primary_LightBar( );
    			break;
    				case 0x01:
    				LCD_PrCString("        ");
    				LCD_Position(1,0);
    				LCD_PrCString("Case1");
                                   Police_LightBar_Secondary( );
                                   break;
    
    	switch(encoder2)
    		{
    			case 0x00:
    				LCD_PrCString("        ");
    				LCD_Position(1,8);
    				LCD_PrCString("Case0");
    			break;
    				case 0x01:
    				LCD_PrCString("        ");
    				LCD_Position(1,8);
    				LCD_PrCString("Case1");
    			break;
    
    
    void Police_Primary_LightBar(void)
    {
    	int j;
    	//LCD_Position(1,0);
    	//LCD_PrCString("Police Primary");
    	for(j=0; j<4000; j++)
    	{
    		if(j<1000)
    		{
    			if(G_Flag1)
    			{
    				Side_Lights_LightBar( );
    				Test_Function4( );
    				I2Caddr=0x20;
    				charcount=2;
    				command[0]=0x00;
    				command[1]=0xE0;
    	
    				I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    				while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    				I2C_ClrWrStatus( );
    			
    				Side_Lights_LightBar( );
    				Test_Function1( );
    				I2Caddr=0x20;
    				charcount=2;
    				command[0]=0x01;
    				command[1]=0xc0;
    	
    				I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    				while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    				I2C_ClrWrStatus( );
    			}
    			else
    			{
    				Side_Lights_LightBar( );
    				Test_Function4();
    				I2Caddr=0x20;
    				charcount=2;
    				command[0]=0x00;
    				command[1]=0x07;
    			
    		
    				I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    				while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    				I2C_ClrWrStatus( );
    			
    			    Side_Lights_LightBar( );
    				Test_Function1( );
    				I2Caddr=0x20;
    				charcount=2;
    				command[0]=0x01;
    				command[1]=0x03;
    	
    				I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    				while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    				I2C_ClrWrStatus( );
    			}
    		}
    	else if((j>1000) && (j<2000))
    	{
    		if(G_Flag1)
    		{
    			Side_Lights_LightBar( );
    			I2Caddr=0x20;
    			charcount=2;
    			command[0]=0x00;
    			command[1]=0x3C;
    	
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    			
    			I2Caddr=0x20;
    			charcount=2;
    			command[0]=0x01;
    			command[1]=0x81;
    	
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    	
    			
    		}
    		else
    		{
    			Side_Lights_LightBar( );
    			I2Caddr=0x20;
    			charcount=2;
    			command[0]=0x00;
    			command[1]=0xC3;
    			
    		
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    			
    			I2Caddr=0x20;
    			charcount=2;
    			command[0]=0x01;
    			command[1]=0x3c;
    	
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    		}
    		
    	
    	}
    	else if((j>2000) && (j<3000))
    	{
    		if(G_Flag1)
    		{
    			Side_Lights_LightBar( );
    			I2Caddr=0x20;
    			charcount=2;
    			command[0]=0x00;
    			command[1]=0x55;
    	
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    	
    			I2Caddr=0x20;
    			charcount=2;
    			command[0]=0x01;
    			command[1]=0x16;
    	
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    		}
    		else
    		{
    			Side_Lights_LightBar( );
    			I2Caddr=0x20;
    			charcount=2;
    			command[0]=0x00;
    			command[1]=0xAA;
    			
    			
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    			
    			Side_Lights_LightBar( );
    			I2Caddr=0x20;
    			charcount=2;
    			command[0]=0x01;
    			command[1]=0x61;
    	
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    		}
    		
    	
    	}
    	else
    	{
    		if(G_Flag1)
    		{
    			Side_Lights_LightBar( );
    			I2Caddr=0x20;
    			charcount=2;
    			command[0]=0x00;
    			command[1]=0xFF;
    	
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    	
    			I2Caddr=0x20;
    			charcount=2;
    			command[0]=0x01;
    			command[1]=0x00;
    	
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    		}
    		else
    		{
    			Side_Lights_LightBar( );
    			I2Caddr=0x20;
    			charcount=2;
    			command[0]=0x00;
    			command[1]=0x00;
    			
    		
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    			
    			I2Caddr=0x20;
    			charcount=2;
    			command[0]=0x01;
    			command[1]=0xFF;
    	
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    		}
    		
    		
    	}
    
    }
    so in summary when i turn the first encoder(encoder one) the police lightbar function gets called and executes which works, and then when i turn the second encoder(encoder 2) i would like for that to break the for loop

    i am not exactly how to do this or if i even can do this, i was thinking of setting a flag for each if else condition and then when i turn the second encoder have the flag break the loop but now sure how to do or even if it will work

    thanks

    i forgot to post some of the code but the test functions and side_lightbar functions are seperate functions that control two ports of the chip at a higher speed (I am using the MCP23016 i/0 expander)
    the flags are generated insdie an isr interupt that code is
    void Counter16_ISR(void)
    {
    G_Flag1= !G_Flag1;
    num1=rand( );
    }
    void Counter16_1_ISR(void)
    {
    G_Flag2 = !G_Flag2;

    num2=rand( );
    }
    void Counter16_2_ISR(void)
    {
    G_Flag3= !G_Flag3;
    num3=rand( );
    }
    void Counter16_3_ISR(void)
    {
    G_Flag4= !G_Flag4;
    //G_Flag4=0;
    num4=rand( );
    }
    sorry its not in proper"quote" format i don't see how to do that when i am replying to a thread
    Last edited by begginer; 03-22-2011 at 10:33 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'm not sure what you are stuck on. In your main loop, wherever that is, simply check to see if switch 1 is on, and if so, call your function that has the loop. Then simply break from the loop when switch 2 triggers.

    You could of course just keep track of your current states:
    Code:
    while state != quit
        switch( state )
            case switch 1 is on
                do loop body, but don't put a new loop here, just use the main loop
            break;
            case all other states ...
        check for new state
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    43
    My main loop is just a while(1) with both the switch statements

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I see you're still madly copy/pasting the same 8 to 10 lines of code, where previously I showed you a function to reduce it to say 2 or 3 lines.
    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.

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    43
    i know, i not yet gotten to make the code pretty yet, i need it all to work first, which is why i plan on factorizing it like you said and reducing it but i need to be able to break out of the for loop and do the other stuff i need to do before i can worry about reducing it

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well I've already shown you what to do here, and you have massive amounts of repeated code. Your whole program looks like I could fit in easily half the lines you have there.

    Write on paper what you plan to do. Then break that into small steps. Then break those steps into smaller steps. Repeat until you run out of ways to break it into smaller steps, in actual words.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Lemme ask you, do you think that you would actually change the function I posted in another thread?

    If the answer is "no", then you're going to save yourself a shed-load of copy/pasting (along with associated errors).

    If the answer is "yes", then think about ALL the places you would have to make the same change (and all the errors that would result from missing some out, or otherwise screwing up more copy/pasting).
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. breaking out of a loop problem
    By dbzx in forum C Programming
    Replies: 7
    Last Post: 04-03-2009, 03:41 AM
  2. Keyloggers (none rule breaking)
    By cloudy in forum C++ Programming
    Replies: 10
    Last Post: 01-25-2006, 04:26 PM
  3. Breaking a for loops when result reached
    By PaulStat in forum C++ Programming
    Replies: 5
    Last Post: 08-15-2003, 09:00 AM
  4. How to Input without Breaking line in a console?
    By Aidman in forum C++ Programming
    Replies: 11
    Last Post: 03-12-2003, 11:55 AM
  5. Breaking down a string
    By Tina in forum C++ Programming
    Replies: 2
    Last Post: 12-05-2001, 08:11 AM