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

  • Home
  • SEARCH
  • 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 439693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:47:57+00:00 2026-05-12T20:47:57+00:00

I have a for loop of the form: for (int i = from; i

  • 0

I have a for loop of the form:

for (int i = from; i < to; i++) {
  // do some code (I don't know exactly what, it is subject to change)
}

And I want to convert it to a while loop (mostly because I want to play with the value of i inside the loop to go backwards and forwards and my co-worker thinks that doing this in a for loop is prone to problems. I tend to agree with him). So I wrote something like this:

int i = from;
while (i < to) {
  try {
    // do some code (I don't know exactly what, it is subject to change)
  } finally {
    i++;
  }
}

Which prompted some some loud comments. My reasoning is that you don’t know what the code inside the loop does – it may (and does) have multiple continue commands.

As a response he wrote this:

int i = from - 1;
while (++i < to) {
  // do some code (I don't know exactly what, it is subject to change)
}

Granted its less lines, but I still think my code is more elegant – what do you think?

  • 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-05-12T20:47:58+00:00Added an answer on May 12, 2026 at 8:47 pm

    And I want to convert it to a while loop (mostly because I want to play with the value of i inside the loop to go backwards and forwards and my co-worker thinks that doing this in a for loop is prone to problems. I tend to agree with him).

    This is perfectly acceptable in most languages. There is no reason to avoid a for loop.

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

Sidebar

Related Questions

I have loop for: for (int i = 1; i < 2000; i++) How
I have a loop like this: for i=1:no %some calculations fid = fopen('c:\\out.txt','wt'); %write
I have multiple textboxes in my form like txtTask0, txtTask1... txtTask12. so I want
I am retrieving some values from DB in form of ArrayList of 240 size
I have some table cells displaying different time. I want the time to be
I want to remove a particular tabpage from the tabcontrol. For which i have
I have a for loop that I want (for every item in a ListBox
I have a C\C++ code that receives a structure over the network, from this
There's something wrong with my while loop (in my Form class), but basically, it
I have the code below that is supposed to draw lines from the top

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.