Moonic
Moonic

Reputation: 11

How to access For loop varible from outside the loop

Basically Im trying if r1 = r2 give error and if r1 is not equal to r2 give me the last i number. But how can I print the last i number outside of for loop? I wanna put i into r1p. Thank you

    for (int i = r1; i < r1 + 100; i++) 
    { 
        if (i == r2 && i == p1) 
        {
            Console.WriteLine("they are equal"); 
        }
        else 
        {
            r1p = i;
        }
    }

Upvotes: 0

Views: 41

Answers (1)

pm100
pm100

Reputation: 50120

Change the scope of i

 int i = 0;
 for (int i = r1;i < r1+100 ; i++) 
 { 
    if (i==r2 && i == p1) {
        Console.WriteLine("they are equal" ); 

     }
     else {
          r1p = i;
              
     }
  }
  Console.WriteLine($"i = {i}");

Upvotes: 1

Related Questions