error (do-while)loop

This is a discussion on error (do-while)loop within the C Programming forums, part of the General Programming Boards category; 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); } ...

  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
    2,752
    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
    2,752
    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
    2,752
    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
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    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?
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8
    Operating system: Fedora 19 (64-Bit) / Linux 3.11.6-200.fc19 / KDE SC 4.11.2
    Compiler: gcc 4.8.2 with Netbeans IDE 7.4

  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-09-2006, 11:44 PM
  5. error check...loop
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 12-01-2001, 09:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21