1 year ago

#384607

test-img

Noah Gluchman

Can I use the loop iterator outside the for statement?

So I have a variable lopvr that I declared inside what I am going to call the "for statement". This is what I am referring to:

for (int lopvr=1;lopvr==1;);

I would like to know if I can use the variable outside the "for statement", like this:

    for (int lopvr=1;lopvr==1;);
    {
      System.out.println("Enter the passcode:");
      Scanner sc= new Scanner(System.in);
    codeEntered= sc.nextInt();
    int passcode = 1409, attempts = 4;
    
    if (passcode == codeEntered) {
      System.out.println("Correct");
    //other code
      lopvr=0;
       }
    else{//other code
    }

For clarification, I am trying to change lopvr, which was declared INSIDE the "for statement", to set the value to 0, OUTSIDE the "for statement", to end the loop when the right conditions are met (You get the passcode right)

java

loops

variable-assignment

0 Answers

Your Answer

Accepted video resources