CSC115 Introduction to Computer Programming Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA 19383 zjiang@wcupa.edu
Table of Contents Review of Basic I/O Condition and Decision Making Format/Syntax and Execution Boolean Expression Development Multiple selection Nested if Code alignment and end of block == and is
Review Output Number String Combination (by using + and str) Input Type Conversion (float and int) Processing Feel it in the test
Condition and Decision Making A deterministic output by a given input Big-or-small? Random.randint(1,6), store and not show Guess 123 small or 456 big Won or lost?
No Yes Condition Action 1 Action 2 Action 3
Boolean Expression Action 1 If controlled Action 2 else controlled Yes No Boolean Expression Action 1 If controlled Action 2 else controlled Action 3 11/9/2018
Syntax and Execution If block if condition : action 1 (statements 1) else : action 2 (statements 2) # a blank line to separate action 3 (statement 3)
Boolean Expression Simple condition Complex condition Format <Value> <relational operators> <Value> Number value relational operators ==, !=, <, >, <=, >= !!! Number one error: “a=2” instead of “a==2” String value relational operators left for later discussion Complex condition And, or, not Truth table
Truth table Precedence order F C2 C1 and C2 C1 or C2 not C1 Truth table Precedence order http://www.mathcs.emory.edu/~valerie/courses/fall10/155/resources/op_precedence.html
Relational operators have lower precedence than math operators. (7 - 1) * 5 + 3 > 7* 5 6 * 5 + 3 > 35 30 + 3 > 35 33 > 35 False Relational operators cannot be chained (unlike math operators) 2 <= x <= 10 error! 11/9/2018
Development Process Identify two exclusive options Implement each handling in different action parts Identify the situation (values) for option selection Make a condition so that all the situation value for option part 1 will lead to this condition true. Verify all the situation value for option part 2 will lead to this condition false, otherwise, revise the above condition!
Multiple selection Nested if (chained condition) Example: letter grade
Nested if for multiple section problem If then case 1 Else if then case 2 else … end if End if
Conditional and alternative execution if condition : action 1 (statements 1) # no else! action 3 (statement 3)
Are they different, how much? x = int(input("enter Int: ")) y = 0 if x > 3 : y = 1 if x < 10 : y = 2 else : y =3 print(y) x = int(input("enter Int: ")) y = 0 if x > 3 : y = 1 if x < 10 : y = 2 else : y =3 print(y)
Try 0, 4, 11 and see the results! 3 4 2 11 1 2 4 11 3
Indeed x = int(input("enter Int: ")) y = 0 if x > 3 : y = 1 x = int(input("enter Int: ")) y = 0 if x > 3 : y = 1 if x < 10 : y = 2 else : y =3 print(y) x = int(input("enter Int: ")) y = 0 if x > 3 : y = 1 if x < 10 : y = 2 else : y =3 print(y)
My point One line in wrong place Structural procedure Could cause the program 99.99% different Location/alignment maters! Blank line is useful Structural procedure Ask yourself where (the next program segment line) the computer execution goes Before that, check if you can find the “if” for each “else”
Try enter “3” and “3”: a = int(input("first string: ")) b = int(input ("second string: ")) if a == b : print("same content") else: print("not the same content") if a is b : print ("same string") else: print ("not the same string")
Try this program and enter “3” and “3”. a = input("first string: ") b = input("second string: ") if a == b : print("same content") else: print("not the same content") if a is b : print ("same string") else: print ("not the same string")
Same code, try “a3” and “a3”. a = input("first string: ") b = input("second string: ") if a == b : print("same content") else: print("not the same content") if a is b : print ("same string") else: print ("not the same string")