Daniel Duffy
C++ author, trainer
- Joined
- 10/4/07
- Messages
- 10,440
- Points
- 648
Not true at all.
Can you give a counter-example?Not true at all.
so please help me with this:
so please help me with this:
I need the equivalent structure in python of these C structures:
do{
// my code
}while(exp?);
I want to translate some C code to python, also I need the equivalent python code for this
switch(?){
case i: //code ; break;
....
}
try not to google, no satisfying equivalent code found so far
thanks in advance
xsn
and the python code is ... ?those constructs don't really exist in python but, as usual, they are all syntactic sugar for something else.
By the way, translating literally from one language to another is a very bad idea.
- do ... while can be implemented either with a loop or a tail recursive function
- the switch is implemented using a dictionary and function calls or multiple if...elif...else.
where is the code to translate?and the python code is ... ?
...
while True:
# do something
if condition: break
...
def tailrec(*args, **kwargs):
"""Tail recursive method"""
if cond: return
#do stuff
tailrec(...)
...
switch_dict = { key1: func1, key2: func2, key3: func3 }
switch_dict[value](*args, **kwargs)
...
switch_result = {
cond1: func1,
cond2: func2,
cond3: func3
}[1]
...
if cond1:
...
elif cond2:
...
elif cond3:
...
else:
...
switch(exp){
case i_th: // code ; break;
}
is usually used when several branches are required, (more than 2).
My C code uses switch statement for +10
case i_th: // code; break;
how to code switch() C-instruction in the most simmilar way in python ?
This would typically be a question one sees in Student forum, for example.Thanks for your reply.
what about
do{
//...
}while();
?
it differs from while(){}