+ 2

how can we use switch case in while loop?

suggest code how to write without infinite loop

2nd May 2025, 3:40 PM
NandIni Wasnik
NandIni Wasnik - avatar
8 odpowiedzi
+ 6
NandIni Wasnik , please update some missing details like: > programming language > if your question is related to a sololearn tutorial: mention the tutorial name, modul name and lesson name. if you already have done a code try, please share it here.
2nd May 2025, 7:09 PM
Lothar
Lothar - avatar
+ 5
in which language? while (True) # do something switch (variable) case 1 case 2 default something like this. I hope this helps
2nd May 2025, 3:52 PM
Mihaly Nyilas
Mihaly Nyilas - avatar
+ 3
NandIni Wasnik I believe you are speaking about c++ ... so one you can set a counter or number of iterations and two while condition is true... both examples are used in the following code https://sololearn.com/compiler-playground/cTbEsqGeW1uA/?ref=app
3rd May 2025, 2:23 PM
BroFar
BroFar - avatar
+ 2
If this is about trying to use `break` to get out of a loop from within a nested `switch` that's just not going to work, because the switch will always eat it. You'll need to use a variable to hold the state for the while loop.
3rd May 2025, 7:33 AM
Tom Shaver
Tom Shaver - avatar
+ 2
NandIni Wasnik , you have not stated the programming language you are going to use. it does not seems to be python. anyway - i used a short python code just for information: > using a break statement in a match ... case... will exit the while loop, since this is the ``innermost enclosing loop``. we can not use ``break`` outside a loop. doing so will raise a syntax error. >>> match... case... is running in python since version 3.10, so it does *NOT* run in the sololearn playground (ver. 3.9) while True: a = int(input() or 2) match a: case 1: print('not quite, the number is too small.') case 2: print('correct') break case 3: print('not quite, the number is too large.') print('\nfinal statement from outside match case and while..')
3rd May 2025, 8:12 AM
Lothar
Lothar - avatar
+ 2
It's as follow in java While (true) { Switch(number) { case 1: Statement break; case 2: Statement default Statement } }
3rd May 2025, 10:35 AM
Safiullah Mohammadi
Safiullah Mohammadi - avatar
+ 2
You can use a switch (or its equivalent, like match-case in Python 3.10+ or a dictionary-based approach) inside a while loop to repeatedly perform actions based on user input or changing variables. This is a common pattern for menu-driven programs or repeated decision-making. General structure (in C-like pseudocode): c while (condition) { switch (expression) { case value1: // code break; case value2: // code break; default: // code break; } // update condition or expression as needed } This means the switch-case is evaluated on each iteration of the while loop, allowing different code to run based on the current value. In Python, you can do similar with match-case or dictionary mapping: python while True: value = input("Enter option: ") match value: case "1": print("Option 1") case "2": print("Option 2") case _: print("Invalid")
4th May 2025, 8:07 AM
Naga sravan Ballala
Naga sravan Ballala - avatar
+ 1
Mihaly Nyilas probably the point of the question is that when meeting the case that will be used you will need to change True to false. But i don’t why it isn’t self-evident that case x will simply include changing the True condition.
2nd May 2025, 5:12 PM
Zvi
Zvi - avatar
OSZAR »