简体   繁体   中英

How to break out of loop

I'm trying to assign the value of optiontext to just one array value at a time.

Is there a way I can break out of the code, because different values are to be assigned to different numbers.

for (int i = 1; i <=20; i++) 
    {
        option[i]=optionText;
    }

Output:

1. Quit
2. Quit
3. Quit
4. Quit
5. Quit
6. Quit
7. Quit
8. Quit
9. Quit
10. Quit
11. Quit
12. Quit
13. Quit
14. Quit
15. Quit
16. Quit
17. Quit
18. Quit
19. Quit
20. Quit           

I just want quit for number one, not the whole 20 options and should only display the numbers that have values.

what it should look like

     1. Quit
     2. Get information
     3. Display n integers, first descending then ascending
     4. Display n squares with odds descending, evens ascending
     5. Check if one string is the reverse of another

If you want to assign a value to just one array element then there is no need to use the loop. Eg if you want to assign optionText to first element then you can simply do:

option[0] = optionText;

The i in option[i] specifies which array index you want. If you just want quit for the first option in the array, you do not need a for loop.

For example you should use, option[0] = optionText; hence, the first element is 0.

In this situation you does not need the for,, just create an integer pointer to fill the array.. something like this.

private int pointer =0;  // define it as an attribute (global variable)

protected void buton_click(){
    option[pointer]=optionText;
    pointer++;  // to be ready for the next index
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM