Thread: how to get out of an if loop?

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    6

    how to get out of an if loop?

    good day. what i want to do with my code is to perform dothisthing if the user's input string is equal to "doaction". i am looking for a way to exit condition 2 and return to condition 1 (waiting for user to input a new string) once the user presses the letter 's'. all of your input and ideas will much appreciated

    Code:
    int howlong;
    if (!(strcmp (USERINPUT,doaction)){               //condition 1
       if (howlong < 10 seconds){                        //condition 2
               dothisthing;
       }
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by iwanttolearnc View Post
    good day. what i want to do with my code is to perform dothisthing if the user's input string is equal to "doaction". i am looking for a way to exit condition 2 and return to condition 1 (waiting for user to input a new string) once the user presses the letter 's'. all of your input and ideas will much appreciated

    Code:
    int howlong;
    if (!(strcmp (USERINPUT,doaction)){               //condition 1
       if (howlong < 10 seconds){                        //condition 2
               dothisthing;
       }
    }
    Short answer... You're not going to do that in 2 nested if statements.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    6
    thank you for the quick reply sir. do you have any suggestions on how i can go about doing that?

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by iwanttolearnc View Post
    thank you for the quick reply sir. do you have any suggestions on how i can go about doing that?
    You need to write your code for USERINPUT, doaction, dothisthing and howlong first. Once you have them before you, poducing good results, I'm pretty sure the rest is going to be obvious.

    Generally you break out of a block of code with, well, break; but that may not be appropriate in every case.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    6
    below is my code sir. its for a zilog microcontroller. is break appropriate for this case?
    Code:
            char userinput[4];
    	char led[] = {'l', 'e', 'd', '\0'};
    	
    	int howlong;
    
    	PDHDE = 0X03;	//zilog related
    	PDAF = 0X00;		//zilog related
    	PDDD = 0X00;		//zilog related
    
    
    	init_uart(_UART1 , _DEFFREQ , _DEFBAUD); //zilog related
    	select_port (_UART1); //zilog related
    
    	printf ("type led\n");
    	
    	userinput[0] = getch();
    	userinput[1] = getch();
    	userinput[2] = getch();	
    	userinput[3] = '\0';
    
    	if (!(strcmp(userinput, led))){		//condition 1
    		if(howlong++ < 0xFF){		//condition 2
    				PDOUT = 0X01;
    				delay();
    				PDOUT = 0x00;
    				delay();			
    		}
    
    	}

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    6
    ive tried the break statement but it only does the action with pdout once. any ideas as to why?

    Code:
    char userinput[4];
    char led[] = {'l', 'e', 'd', '\0'};
    
    int howlong;
    
    PDHDE = 0X03;	//zilog related
    PDAF = 0X00;	 //zilog related
    PDDD = 0X00;	 //zilog related
    
    
    init_uart(_UART1 , _DEFFREQ , _DEFBAUD); //zilog related
    select_port (_UART1); //zilog related
    
    printf ("type led\n");
    
    userinput[0] = getch();
    userinput[1] = getch();
    userinput[2] = getch();	
    userinput[3] = '\0';
    
    if (!(strcmp(userinput, led))){	 //condition 1
    if(howlong++ < 0xFF){	 //condition 2
    PDOUT = 0X01;
    delay();
    PDOUT = 0x00;
    delay();	
    
    if (getch() == 's'){
    break;
    }
    }
    
    }
    Last edited by Salem; 09-14-2010 at 02:57 PM. Reason: code tags, but still no indentation

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    6
    ive tried the break statement but it only does the action with pdout once. any ideas as to why?

    Code:
    char userinput[4];
    char led[] = {'l', 'e', 'd', '\0'};
    
    int howlong;
    
    PDHDE = 0X03;	//zilog related
    PDAF = 0X00;	 //zilog related
    PDDD = 0X00;	 //zilog related
    
    
    init_uart(_UART1 , _DEFFREQ , _DEFBAUD); //zilog related
    select_port (_UART1); //zilog related
    
    printf ("type led\n");
    
    userinput[0] = getch();
    userinput[1] = getch();
    userinput[2] = getch();	
    userinput[3] = '\0';
    
    if (!(strcmp(userinput, led))){	 //condition 1
    if(howlong++ < 0xFF){	 //condition 2
    PDOUT = 0X01;
    delay();
    PDOUT = 0x00;
    delay();	
    
    if (getch() == 's'){
    break;
    }
    }
    
    }
    Last edited by Salem; 09-14-2010 at 02:57 PM. Reason: It's CODE not QUOTE!

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Just like the error says, you don't break out of an if statement. break is only for exiting loops, and an if statement is not a loop!

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Put the code in a loop which executes once.
    Code:
    do {
      if ( something ) break;
    } while ( 0 );
    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.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Learn to indent. Furthermore, if you need to stop processing if get a certain input, then why not process if you DON'T get the input? Thus you don't need break, only an if statement.


    Code:
    if (!(strcmp(userinput, led)))
    {     //condition 1
        if(howlong++ < 0xFF)
        {     //condition 2
            PDOUT = 0X01;
            delay();
            PDOUT = 0x00;
            delay();    
    
            if (getch() != 's')
            {
                // Continue processing
            }
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    As has been pointed out "if" is not a loop, it's a one time conditional test.

    Try something like this...

    Code:
    howlong = 0;
    
    if (!(strcmp(userinput, led)))	 //condition 1
      while (howlong++ < 0xFF)	 //condition 2
        { PDOUT = 0X01;
          delay();
          PDOUT = 0x00;
          delay();	
          if (getch() == 's')
            break; }
    There are some excellent C language tutorials available for free download and/or study on the net. Google is your friend...


    [/code]

  12. #12
    Registered User
    Join Date
    Sep 2010
    Posts
    6
    thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Poll event loop
    By rogster001 in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2009, 04:28 AM
  2. need help with a loop
    By Darkw1sh in forum C Programming
    Replies: 19
    Last Post: 09-13-2009, 09:46 PM
  3. funny-looking while loop
    By Aisthesis in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2009, 11:54 PM
  4. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM