What are if statements? what are while loops? - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript What are if statements? what are while loops? - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Monday, December 10, 2018

What are if statements? what are while loops?

What are if statements? what are while loops?



What are if statements? What are While loops?

If statements allow you to examine the current state of a program and respond appropriately to that state. you can write a simple if statement that checks one condition you're looking for.

while loops run as long as certain conditon remain true. You can use while loops to let your programs runs as long as users want them to

If statements

Several kinds of if statements exist. Your choice of which to use depends on the number of conditions you need to test. You can have as many elif blocks as you need, and the else block is always optional.

#Simple if statement
age = 19

if age >= 18:
 print("you're old enough to vote!")
#If-else statements
age = 17

if age >= 18:
 print("you're old enough to vote!")
else:
 print("you cant't vote yet.")
#The if-elif-else chain
age = 12

if age < 4:
 price = 0
elif age < 18:
 price = 5
else:
 price = 10

print("your cost is $" + str(price) + ".")

Screenshot 

python if else

 


Conditional tests with lists

You can easily test whether a certain value is in a list. You can also test whether a list is empty befors trying to loop through the list.

#Testing if a value is in a list
>>> players +['al', 'bea', 'cyn', 'dale']
>>> 'al' in players
True
>>> 'eric' in players
False

No comments:

Post a Comment

Post Top Ad