Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 if statement

2  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

3  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.

4 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()

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

6  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_suite else: expr_false_suite

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

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

9  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.

10 if expression1: expr1_true_suite elif expression2: expr2_true_suite : elif expressionN: exprN_true_suite else: none_of_the_above_suite

11 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 …

12  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”

13  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.

14  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.

15  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.


Download ppt "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."

Similar presentations


Ads by Google