Is it possible to run a loop in a Switch default? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Is it possible to run a loop in a Switch default?

I have written the following code and it works "fine" but it's not the way I would like it. I tried writing a do...while loop in the default option of the switch to force the user input to choose a correct option. It didn't work and so my default closes the program. Is there a way to make the option choice repeat using switch or do I need to make a class or method to achieve this? https://code.sololearn.com/c8imNgTp6QK5/?ref=app

21st Apr 2022, 5:14 PM
Ausgrindtube
Ausgrindtube - avatar
11 Answers
+ 2
public static void main(String[] arg){ int n=1; boolean repeat=false; do{ n++; // take input repeat=false; switch(n){ case 8: System.out.println("valid"); break; default : System.out.println("invalid"); repeat=true; } }while(repeat); } // sample. Hope this helps.. // but zemiek answer will be better in your case, I think.
22nd Apr 2022, 4:16 PM
Jayakrishna 🇮🇳
+ 3
Ahhh! Awesome! Thanks guys
21st Apr 2022, 9:17 PM
Ausgrindtube
Ausgrindtube - avatar
+ 3
You can make a module or a library for the Switch Case and make a composite logical construct using if...else...endif that integrates these and returns the end result as a FUNCTION. This can be iterated by passing different subsets of values like [[1,5],[3,8]] - one 1×2 array and each element having 2 values.
23rd Apr 2022, 11:54 AM
Sanjay Kamath
Sanjay Kamath - avatar
+ 2
Put the swich case in do while Take a boolean value which should be set to true in default case so that loop won't stops.. Otherwise stops,.
21st Apr 2022, 5:31 PM
Jayakrishna 🇮🇳
+ 2
or int selectNum; do { selectNum = keyinput.nextInt(); } while (selectNum < 1 || selectNum > 6); switch(selectNum) {
21st Apr 2022, 7:29 PM
zemiak
+ 2
Ausgrindtube The structure of the function is void function f(x,y,z){ returns modIntegrator } void modIntegrator{ //Mod 1 Switch Case(x): Case 1: Case 2: Else End Case //return str(output_a) //Mod 2 Switch Case(y): Case 1: Case 2: Else End Case //return str(output_b) } //Mod 3 Switch Case(z): Case 1: Case 2: Else End Case //return str(output_z) returns str(output_x)+str(output_y)+str(output_z) } You can read x,y and z from an enum or for each or for loop or array like for(x=0;x<3;x++) or an enum like [0,1,2] Hope this helps..feel free to ask
24th Apr 2022, 8:08 AM
Sanjay Kamath
Sanjay Kamath - avatar
+ 1
I'm sorry Jayakrishna🇮🇳, my brain isn't working out the boolean part. Would it be as simple as "boolean x = true;"? And if so, how does that stop the code from working? Can I still include my println after the boolean or should it go before?
22nd Apr 2022, 4:03 PM
Ausgrindtube
Ausgrindtube - avatar
+ 1
I could do Zem's code but it wasn't exactly what I had in mind. Your example is perfect! Thanks so much.
22nd Apr 2022, 5:21 PM
Ausgrindtube
Ausgrindtube - avatar
+ 1
You're welcome..
22nd Apr 2022, 7:38 PM
Jayakrishna 🇮🇳
+ 1
Thanks Sanjay Kamath. I think your solution is way past my ability. Something to look forward to learning!
23rd Apr 2022, 1:20 PM
Ausgrindtube
Ausgrindtube - avatar
+ 1
Sanjay Kamath, you probably answered to another post, void function f (x, y, z) { is not java, and why you create 3 switches when only one is in first code..
24th Apr 2022, 8:38 AM
zemiak