Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8002859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:29:44+00:00 2026-06-04T16:29:44+00:00

Several books show me how to correctly write for, while, and do loops. Lots

  • 0

Several books show me how to correctly write for, while, and do loops.
Lots of online articles compare them to each other.

But I haven’t found any place that tells me what not to do. For example, would it screw things up if I change the value of the counter or condition variable within the loop?

I would like an answer that is not machine dependent.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T16:29:45+00:00Added an answer on June 4, 2026 at 4:29 pm

    Yes you can change the counter within a loop and it can sometimes be very useful. For example in parsing command line arguments where there is an option flag followed by a value. An example of this is shown below:

    Enter the following command:

    program -f filename -o option -p another_option

    Code:

    #include <string.h>
    
    int main(int argc, char** argv)
    {
    
     char *filename, *option, *another_option;
    
     if(argc > 1){
       for(int i=1; i<argc; i++){
         if(strcmp(argv[i],"-f")==0){
            filename = argv[++i];
         } else if(strcmp(argv[i],"-o")==0){
           option = argv[++i];
         } else if(strcmp(argv[i],"-p")==0){
           another_option = argv[++i];
         } else {
           printf("Option \"%s\" not recognized, skipping\n",argv[i]);
           continue;
         }
       }
     } /* end if argc > 1 */
    
     return 0;
    }
    

    The example program automatically increments the counter to access the correct command line string. There are of course ways to incorporate counters etc., but they would only make the code more cumbersome in this case.

    As others have pointed out, this is where many people write bugs and one must be careful when incrementing counters within loops, particularly when the loop is conditional upon the counter value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While studying java with several books, none of them seem to make it clear
I have been reading about dependency properties in several books but all have one
I have studied several articles, some chapters of books about FP and I would
I see code like the following in several books and examples online. The problem
I have been working my way through several books on X-code, the Apple document
I've been programming in Linux/UNIX for several years now, but recently I needed to
I've seen it done in several books and such, so there has to be
I am currently reading several security books(my passion) regarding secure programming, however either the
I've studied several Android books in 2009 and have fiddled with SDK 1.0 with
most of the books about Project Management (if not all of them) describe management

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.