I'm new to Python. Actually I implemented something using Java as shown below.
for(;;){
switch(expression){
case c1: statements
case c2: statements
default: statement
}
}
How do I implement this in Python?
I'm new to Python. Actually I implemented something using Java as shown below.
for(;;){
switch(expression){
case c1: statements
case c2: statements
default: statement
}
}
How do I implement this in Python?
Use while loop:
while True:
if condition1:
statements
elif condition2:
statements
...
else:
statements
Formally, there's no switch
statement in Python; it's a series of nested if-elif-else
statements.
Infinite loops are accomplished by the while True
statement.
All together:
while True:
if condition_1:
condition_1_function
elif condition_2:
condition_2_function
elif condition_3:
condition_3_function
else: # Always executes like "default"
condition_default_function
If you are looking for a way to iterate infinitely in python you can use the itertools.count() function like a for loop. http://docs.python.org/py3k/library/itertools.html#itertools.count
You can use
while True:
if c1:
statements
elif c2:
statements
else:
statements
or
var = 1
while var == 1:
# do stuff