hello im new with python and i want to you ask a question.
is there any function like switch on C++ ?
pwnmercury 0 Newbie Poster
Recommended Answers
Jump to Post— TrustyTony 888Standard answer îs that for fixed values use dictionary lookup, otherwise use if...elif...else.. Learn to use 'in' check in condions. If you want we can recode some meaningfull switch using C snippet in Python.
Jump to Post— Beat_Slayer 17An if ... elif ... elif ... sequence is a substitute for the switch or case statements found in other languages.
Cheers and Happy coding
Jump to Post— TrustyTony 888You can use break to get out of loops and continue to continue iteration from next value.
If you want to jump out deeper from loop I recommend to move the inner loops to function and do return from there.
Jump to Post— snippsat 661is there fucntion like goto on C++
No,and i hope we never see goto it in python.
http://xkcd.com/292/Edsger W.Dijkstra wrote this in 1968.
Jump to Post— vegaseat 1,735Switch/case is rather slow in C or C++ and therefore was not implemented in Python. You can however use the much faster dictionary look-up to mimic a switch/case type function ...
# a dictionary switch/case like statement to replace # multiple if/elif/else statements in Python def switch_case(case): …
All 18 Replies
TrustyTony 888 ex-Moderator Team Colleague Featured Poster
Beat_Slayer 17 Posting Pro in Training
pwnmercury 0 Newbie Poster
TrustyTony 888 ex-Moderator Team Colleague Featured Poster
pwnmercury 0 Newbie Poster
snippsat 661 Master Poster
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
arkoenig 340 Practically a Master Poster
Beat_Slayer 17 Posting Pro in Training
arkoenig 340 Practically a Master Poster
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
arkoenig 340 Practically a Master Poster
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
arkoenig 340 Practically a Master Poster
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
Beat_Slayer 17 Posting Pro in Training
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.