locked
Variable outside a for loop RRS feed

  • Question

  • Why can't you use a variable that is initialized and declared inside a for loop, outside that for loop? Thank you :)
    Wednesday, November 13, 2013 5:52 PM

Answers

  • This is called "Variable Scope". Don't worry though! There are many ways to get around this.

    One example is:

    int i;
    
    for (i = 0; i < 100; i++)
    {
       i++;
    }

    This code initializes the variable i outside of the loop, so that after the loop is complete you may be that variable else where.

    Something to remember is that, the code that is created inside the curly brackets " { } " is only accessible in the curly brackets " { } ". Sometimes programmers will call code that isn't encased within the curly brackets " { } " orphan code because the code does not belong to anything.

    Imagine you wanted to make a method that would add two numbers like this:

    public int addTwoNumbers()
    {
    // This is declaring two int variables and 
    // assigning the values 5 and 10 to them.
       int number1 = 5;
       int number2 = 10;
    }
    
    // WRONG!!!!!!!!!!!!!
    int answer = number1 + number2;
    
    
    
    
    Since the two variables are declared and initialized inside the method, those variables therefor belong to the method, and can not be accessed outside the method. 

    • Proposed as answer by Ioana Vasilescu Wednesday, November 13, 2013 7:22 PM
    • Marked as answer by Eason_H Thursday, November 21, 2013 1:51 AM
    Wednesday, November 13, 2013 6:47 PM
  • The solution proposed by ChaseJ993 will work, but keep in mind that most would consider this bad style.  The loop variable is for controlling the loop - if you exit the loop and want to know which index you exited at, it's better to use a separate variable for that:

    int interestingIndex = -1; for (int i = 0; i < 100; i++) { if (someCondition) { interestingIndex = i; break; } }

    //now check 'interestingIndex'



    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter


    • Edited by Dave Doknjas Wednesday, November 13, 2013 9:26 PM
    • Marked as answer by Eason_H Thursday, November 21, 2013 1:50 AM
    Wednesday, November 13, 2013 9:24 PM

All replies

  • Wednesday, November 13, 2013 6:06 PM
  • This is called "Variable Scope". Don't worry though! There are many ways to get around this.

    One example is:

    int i;
    
    for (i = 0; i < 100; i++)
    {
       i++;
    }

    This code initializes the variable i outside of the loop, so that after the loop is complete you may be that variable else where.

    Something to remember is that, the code that is created inside the curly brackets " { } " is only accessible in the curly brackets " { } ". Sometimes programmers will call code that isn't encased within the curly brackets " { } " orphan code because the code does not belong to anything.

    Imagine you wanted to make a method that would add two numbers like this:

    public int addTwoNumbers()
    {
    // This is declaring two int variables and 
    // assigning the values 5 and 10 to them.
       int number1 = 5;
       int number2 = 10;
    }
    
    // WRONG!!!!!!!!!!!!!
    int answer = number1 + number2;
    
    
    
    
    Since the two variables are declared and initialized inside the method, those variables therefor belong to the method, and can not be accessed outside the method. 

    • Proposed as answer by Ioana Vasilescu Wednesday, November 13, 2013 7:22 PM
    • Marked as answer by Eason_H Thursday, November 21, 2013 1:51 AM
    Wednesday, November 13, 2013 6:47 PM
  • The solution proposed by ChaseJ993 will work, but keep in mind that most would consider this bad style.  The loop variable is for controlling the loop - if you exit the loop and want to know which index you exited at, it's better to use a separate variable for that:

    int interestingIndex = -1; for (int i = 0; i < 100; i++) { if (someCondition) { interestingIndex = i; break; } }

    //now check 'interestingIndex'



    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter


    • Edited by Dave Doknjas Wednesday, November 13, 2013 9:26 PM
    • Marked as answer by Eason_H Thursday, November 21, 2013 1:50 AM
    Wednesday, November 13, 2013 9:24 PM