Thread: error (do-while)loop

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    5

    error (do-while)loop

    Code:
     
    int main()
                               {
                                    int i,sum;
    
    
                                   do
                                    {
                                         printf("%d\n",sum);
                                         sum+=i*i;
                                    }
    
    
                                   while(i>3);
                                   {
                                   i=43;
                                   i-=3;
    
    
                                   if(i==23)
                                   break;
                                   }
    
    
                                    return(0);
                               }
    error  break statement not within loop or switch
    
    
    could someone tell me what is the problem,plesase?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    A little bit of formatting might help you see the error:
    Code:
    int main()
    {
       int i,sum;
    
       do
       {
          printf("%d\n",sum);
          sum+=i*i;
       }while(i>3);
    
       {
          i=43;
          i-=3;
    
    
          if(i==23)
             break;
       }
    
       return(0);
    }
    Is the break statement inside a loop, or switch statement?


    Jim

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    5
    i m sorry, i fully dont understand the meaning of switch statement. Inside the loop.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    It is not the switch statement that is not inside a loop. It is the break statement not inside a loop. A break must be either inside a loop or inside a switch statement. Is your break statement inside a loop? Inside a switch statement?

    Jim

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    5
    Inside a loop.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    The problem is that the break statement is not inside a loop. Look at your code I re-posted, your break statement is not inside any loop.

    Please see this link: Control Structures.

    Jim

  7. #7
    spaghetticode
    Guest
    Is it? Where does your loop start (and how do you know?) and where does it end (and how do you know)? What would your loop look like, if it didn't do anything? Just the empty loop?

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by jimblumberg View Post
    A little bit of formatting might help you see the error:
    Code:
    int main()
    {
       int i,sum;
    
       do
       {
          printf("%d\n",sum);
          sum+=i*i;
       }while(i>3);
    
       {
          i=43;
          i-=3;
    
    
          if(i==23)
             break;
       }
    
       return(0);
    }
    Is the break statement inside a loop, or switch statement?


    Jim

    Ok... since nobody else has done this, so far...

    Code:
    int main()
    {
       int i,sum;
    
       do
       {
          printf("%d\n",sum);                 <---- this is your loop
          sum+=i*i;
       }while(i>3); 
    
    
      {
          i=43;
          i-=3;                                    <---- this is not.
    
          if(i==23)
             break;
       }
    
       return(0);
    }
    Get it now?

    If you want to know what a switch() statement is ... look it up!

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    5
    yes.thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Do While Loop Error
    By mintsmike in forum C Programming
    Replies: 4
    Last Post: 03-24-2009, 02:45 PM
  2. error with my for loop?
    By rs07 in forum C Programming
    Replies: 1
    Last Post: 07-25-2008, 07:12 PM
  3. Loop seg error
    By Zishaan in forum Game Programming
    Replies: 2
    Last Post: 03-28-2007, 01:27 PM
  4. error in a for loop
    By saahmed in forum C Programming
    Replies: 11
    Last Post: 03-10-2006, 12:44 AM
  5. error check...loop
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 12-01-2001, 10:46 PM