3 Replies - 886 Views - Last Post: 27 April 2012 - 04:50 AM Rate Topic: -----

#1 crazy123   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 138
  • Joined: 04-August 11

If and Elif

Posted 26 April 2012 - 07:16 PM

I need else in python?
I can do this:
if x == 10:
...
elif x == 5:
...
endif



?
Is This A Good Question/Topic? 0
  • +

Replies To: If and Elif

#2 Simown   User is offline

  • Blue Sprat
  • member icon

Reputation: 323
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: If and Elif

Posted 26 April 2012 - 08:42 PM

Certainly. But you don't need the "endif" at the end:

if x == 10:
    print("x is 10")
elif x == 5:
    print("x is 5")



Is perfectly valid syntax. You can stack as many elifs as you want without including an else. Think of "else" as a default value when no other statement holds. If you leave it out, the program is not always guaranteed to execute a statement.

Hope that helps :)

This post has been edited by Simown: 26 April 2012 - 08:42 PM

Was This Post Helpful? 1
  • +
  • -

#3 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2770
  • View blog
  • Posts: 4,429
  • Joined: 21-June 11

Re: If and Elif

Posted 27 April 2012 - 01:52 AM

Please stop asking the same question on multiple sites. That just leads to duplicated answers and wasted effort on the answerers' part.
Was This Post Helpful? 0
  • +
  • -

#4 crazy123   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 138
  • Joined: 04-August 11

Re: If and Elif

Posted 27 April 2012 - 04:50 AM

View PostSimown, on 26 April 2012 - 08:42 PM, said:

Certainly. But you don't need the "endif" at the end:

if x == 10:
    print("x is 10")
elif x == 5:
    print("x is 5")



Is perfectly valid syntax. You can stack as many elifs as you want without including an else. Think of "else" as a default value when no other statement holds. If you leave it out, the program is not always guaranteed to execute a statement.

Hope that helps :)


OK. Thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1