0

Can I change switch loop into else-if loop?

31st May 2021, 8:01 AM
Decoder 👩‍💻
7 ответов
0
switch and if-else aren't loops . Use else if for every case of switch and else for default.
31st May 2021, 8:03 AM
TOLUENE
TOLUENE - avatar
0
There is nothing called switch loop in Java
31st May 2021, 8:03 AM
Abhishek Tandon
Abhishek Tandon - avatar
0
private void startpoint(int myX, int myY){ int nx, ny; var random = new Random(); for(int k = 0; k< 4; k++){ int r = random.nextInt(4); // the random path that this program will take for(int q = 0; q < 4;q++){ // random between 0 and 4 switch (r) { case 0: // moving up nx = myX; ny = myY - 1; if (between(nx, x) && between(ny, y) && maze[nx][ny] == 0) { maze[myX][myY] |=1; maze[nx][ny] |= 2; //start again startpoint(nx, ny); break; } case 1: //moving right nx = myX + 1; ny = myY; if (between(nx, x) && between(ny, y) && maze[nx][ny] == 0) { maze[myX][myY] |= 4; maze [nx][ny] |= 8; startpoint(nx, ny); break; } case 2: //moving down nx = myX; ny = myY + 1; if (between(nx, x) && between(ny, y) && maze[nx][ny] == 0) { maze[myX][myY] |= 2; maze[nx][ny] |= 1; startpoint(nx, ny); break; }
31st May 2021, 8:11 AM
Decoder 👩‍💻
0
case 3: //moving left nx = myX - 1; ny = myY; if (between(nx, x) && between(ny, y) && maze[nx][ny] == 0) { maze[myX][myY] |= 8; maze[nx][ny] |= 4; startpoint(nx, ny); break; } default: } }
31st May 2021, 8:12 AM
Decoder 👩‍💻
0
Can i change the cases into else-if statements ?
31st May 2021, 8:24 AM
Decoder 👩‍💻
0
Decoder 👩‍💻 better indent/format your code: it will be more readable/understandable ^^ next times use code playground to share your code, even if that's only a sample of it, when it is long... anyway, despite your code lacks of closing curly braces, I don't think ypu need to wrap your switch..case inside a loop (hard to say, without your full code): if one case occur, it will be executed without needing to loop over possible values ^^ In fact, actually your code will run 4 times the same case... wich I guess is not expected (and could at worst make your code wrong, at better loose time) ;P (to be continued in next post)
31st May 2021, 4:10 PM
visph
visph - avatar
0
if..else statements can almost ever replace switch conditional statement (not loop)... switch (value) { case value1: /* code to do if value==value1 */ break; // exit switch, else next case code will be executed case value2: /* code to do if value==value2 */ if (condition) { /* code executed if condition is true */ break; // exit switch } // next case will be executed if condition is false case value3: /* code executed if value==value3 OR (value==value2 and condition is false) */ // last case doesn't require to explicitly break, since you doesn't have a 'default' statement, else 'default' doesn't require break } you can do it as: if (value==value1) { /* 1st case */ } else if (value==value2) { /* 2nd case */ } else if (value==value3) { /* 3rd case */ } else { /* default case: both else and default are optional and run for all other cases */ } ... since you doesn't chain cases ^^
31st May 2021, 4:11 PM
visph
visph - avatar