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 968453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:28:58+00:00 2026-05-16T02:28:58+00:00

a quick, simple question from me about for-loops. Situation I’m currently writing some high-performance

  • 0

a quick, simple question from me about for-loops.

Situation
I’m currently writing some high-performance code when I suddenly was wondering how the for-loop actually behaves. I know I’ve stumbled across this before, but can’t for the life of me find this info again :/

Still, my main concern was with the limiter. Say we have:

for(int i = 0; i < something.awesome; i++)
{
// Do cool stuff
}

Question
Is something.awesome stored as an internal variable or is the loop constantly retrieving something.awesome to do the logic-check? Why I’m asking is of course because I need to loop through a lot of indexed stuff and I really don’t want the extra function-call overhead for each pass.

However if something.awesome is only called once, then I’m going back under my happy rock! 🙂

  • 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-16T02:28:59+00:00Added an answer on May 16, 2026 at 2:28 am

    You can use a simple sample program to check the behaviour:

    using System;
    
    class Program
    {
        static int GetUpperBound()
        {
            Console.WriteLine("GetUpperBound called.");
            return 5;
        }
    
        static void Main(string[] args)
        {
            for (int i = 0; i < GetUpperBound(); i++)
            {
                Console.WriteLine("Loop iteration {0}.", i);
            }
        }
    }
    

    The output is the following:

    GetUpperBound called. 
    Loop iteration 0. 
    GetUpperBound called. 
    Loop iteration 1. 
    GetUpperBound called. 
    Loop iteration 2. 
    GetUpperBound called. 
    Loop iteration 3. 
    GetUpperBound called. 
    Loop iteration 4. 
    GetUpperBound called.
    

    The details of this behaviour are described in the C# 4.0 Language Specification, section 8.3.3 (You will find the spec inside C:\Program Files\Microsoft Visual Studio 10.0\VC#\Specifications\1033):

    A for statement is executed as
    follows:

    • If a for-initializer is present, the variable initializers or statement
      expressions are executed in the order
      they are written. This step is only
      performed once.

    • If a for-condition is present, it is evaluated.

    • If the for-condition is not present or if the evaluation yields true,
      control is transferred to the embedded
      statement. When and if control reaches
      the end point of the embedded
      statement (possibly from execution of
      a continue statement), the expressions
      of the for-iterator, if any, are
      evaluated in sequence, and then
      another iteration is performed,
      starting with evaluation of the
      for-condition in the step above.

    • If the for-condition is present and the evaluation yields false, control
      is transferred to the end point of the
      for statement.

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

Sidebar

Related Questions

Question simple and quick: I have started to use Netbeans to write some code
I'm writing a simple program to convert text to strings. One quick question, how
I have a simple question, which should hopefully have a quick answer. The code
A quick and simple question:). I have a .jar with allot of classes and
Hopefully this is a quick question. I'm making a simple rock paper scissors game.
Is this a simple process? I'm only writing a quick hacky UI for an
Just a quick and simple question: I am using php and am looking to
This is a quick but simple question. I am having a bad day and
just a quick question. Is there a SIMPLE way to dynamic set the cell
Quick question: What's the best way to go about removing <br> and <br />

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.