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 PostAn if ... elif ... elif ... sequence is a substitute for the switch or case statements found in other languages.
Cheers and Happy coding
Jump to PostSwitch/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): …
Jump to PostSwitch/case is slow in C or C++? Not in my experience. Do you have evidence to support this claim?
It is hard to compare a Python dictionary lookup with a C switch/case construct.
You have to think a little out of your box, and put yourself into the shoes …
Jump to PostOf course.
I was responding to the specific claim that Python does not have switch statements because they're slower in C than if/else statements. I haven't commented on whether I think switch statements might be useful in Python for other reasons.
This is my original statement:
Switch/case is rather slow …
Jump to PostI think it's easy as making a case and a for loop and testing them.
Cheers and Happy codingOh no, the switch/case will be the winner over multiple if/else. However to get the speed, the C switch/case is very limited and the Python team didn't want to limit …
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.