if statement. it is made up of three main components: the keyword itself, an expression that is...

15
if statement

Upload: elvin-lambert

Post on 18-Jan-2018

216 views

Category:

Documents


0 download

DESCRIPTION

 The suite of the  if clause, expr_true_suite, will be executed only if the above conditional expressionresults in a Boolean true value.  Otherwise, execution resumes at the next statement following the suite.

TRANSCRIPT

Page 1: If statement.  It is made up of three main components:  The keyword itself,  an expression that is tested for its truth value,  and a code suite to

if statement

Page 2: If statement.  It is made up of three main components:  The keyword itself,  an expression that is tested for its truth value,  and a code suite to

It is made up of three main components: The keyword itself, an expression that is tested for its truth

value, and a code suite to execute if the

expression evaluates to non-zero or true.

The syntax for an if statement is:

if expression: expr_true_suite

Page 3: If statement.  It is made up of three main components:  The keyword itself,  an expression that is tested for its truth value,  and a code suite to

The suite of the if clause, expr_true_suite, will be

executed only if the above conditional expressionresults in a Boolean true value.

Otherwise, execution resumes at the next statement following the suite.

Page 4: If statement.  It is made up of three main components:  The keyword itself,  an expression that is tested for its truth value,  and a code suite to

Example: if make_hard_copy: send_data_to_printer() Single line statements such as the above

are valid syntax-wise; however, although it may be convenient, it may make your code more difficult to read, so it is recommended that you indent the suite on the next line as below.

if make_hard_copy: send_data_to_printer()

Page 5: If statement.  It is made up of three main components:  The keyword itself,  an expression that is tested for its truth value,  and a code suite to

if test_marks >= 50:print “Passed\n”

Page 6: If statement.  It is made up of three main components:  The keyword itself,  an expression that is tested for its truth value,  and a code suite to

Python features an else statement that can be paired with an if statement.

The else statement identifies a block of code to be executed if the conditional expression of the if statement resolves to a false Boolean value.

The syntax :if expression: expr_true_suiteelse: expr_false_suite

Page 7: If statement.  It is made up of three main components:  The keyword itself,  an expression that is tested for its truth value,  and a code suite to

if x > y : print (“x is greater than y”)else: print (“ x is smaller than y”)

Page 8: If statement.  It is made up of three main components:  The keyword itself,  an expression that is tested for its truth value,  and a code suite to

if test_marks >= 50: print “Passed\n”else: print “Failed\n”

Page 9: If statement.  It is made up of three main components:  The keyword itself,  an expression that is tested for its truth value,  and a code suite to

elif is the Python else-if statement. If the original statement is false and the elif statement is True, then the block of statements after elif will be executed.

Like the else, the elif statement is optional

You may specify as many elif headers as you need. Take note that each elif header also requires a condition.

Page 10: If statement.  It is made up of three main components:  The keyword itself,  an expression that is tested for its truth value,  and a code suite to

if expression1: expr1_true_suiteelif expression2: expr2_true_suite:elif expressionN: exprN_true_suiteelse: none_of_the_above_suite

Page 11: If statement.  It is made up of three main components:  The keyword itself,  an expression that is tested for its truth value,  and a code suite to

test_marks = input(“Enter marks: ”)if test_marks >= 80: grade = “A”elif test_marks >= 70: grade = “B”elif test_marks >= 60: grade = “C”elif test_marks >= 50: grade = “D”else: grade = “F”print “Grade: ”, grade…

Page 12: If statement.  It is made up of three main components:  The keyword itself,  an expression that is tested for its truth value,  and a code suite to

The Boolean operators and, or, and not can be used to provide multiple

conditional expressions or perform negation of expressions in the

same if statement. example: if not warn and (system_load >= 10): print "WARNING: losing resources”

Page 13: If statement.  It is made up of three main components:  The keyword itself,  an expression that is tested for its truth value,  and a code suite to

An employee earns RM 5 per hour for the first 30 hours and RM 10 per hour for each hour over 30. Write a program that able to compute an employee’s weekly wage. Example output:

33 hours wage RM 180

Provide your solution with flowchart. Transform the flowchart to Python code.

Page 14: If statement.  It is made up of three main components:  The keyword itself,  an expression that is tested for its truth value,  and a code suite to

Shirts are on sale for RM 4 each if more than two are purchased and RM 6 each otherwise. Write a program that able to read in an input number of shirts purchased and display the total cost.

Analyze the problem:Identify the input, output and formula.

Design your solution with a pseudocode and flowchart. Transform the design to Python code.

Page 15: If statement.  It is made up of three main components:  The keyword itself,  an expression that is tested for its truth value,  and a code suite to

Write a program that reads an integer and determines and prints whether it is odd or even. (Hint: use the modulus operator. An even number is a multiple of two. Any multiple of two leaves a remainder of zero when divided by 2.

Analyze the problem:Identify the input, output and formula. Design your solution with a pseudocode and flowchart. Transform the design to Python code.