Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Infinite for loop 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?

like image 508
Kiran Bhat Avatar asked Jan 03 '12 14:01

Kiran Bhat


3 Answers

Use while loop:

 while True:

      if condition1:
            statements
      elif condition2:
            statements
      ...
      else:
            statements
like image 72
soulcheck Avatar answered Sep 28 '22 08:09

soulcheck


while True:
    # do stuff forever
like image 38
Rob Wouters Avatar answered Sep 28 '22 08:09

Rob Wouters


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
like image 22
Makoto Avatar answered Sep 28 '22 07:09

Makoto