CSC 115 Introduction to Computer Programming Zhen Jiang
























- Slides: 24
CSC 115 Introduction to Computer Programming Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA 19383 zjiang@wcupa. edu
Table of Contents n n n Review of Basic I/O Condition and Decision Making Format/Syntax and Execution Boolean Expression Development n Multiple selection n Nested if n Code alignment and end of block n == and is
Review Output n Number n String n Combination (by using + and str) n Input n Type n Conversion (float and int) n Processing n Feel it in the test n
Condition and Decision Making A deterministic output by a given input n Big-or-small? n Random. randint(1, 6), store and not show n Guess 123 small or 456 big n Won or lost? n
Yes No Condition Action 1 Action 2 Action 3
Yes Boolean Expression Action 1 If controlled No Action 2 else controlled Action 3 9/17/2021 6
Syntax and Execution n If block if condition : action 1 (statements 1) else : action 2 (statements 2) # a blank line to separate action 3 (statement 3)
Boolean Expression n Simple condition Format <Value> <relational operators> <Value> n Number value relational operators ==, !=, <, >, <=, >= !!! Number one error: “a=2” instead of “a==2” n String value relational operators left for later discussion n n Complex condition n n And, or, not Truth table
C 1 T T F F C 2 T F C 1 and C 2 T F F F C 1 or C 2 T T T F not C 1 F F T T n n n Truth table Precedence order http: //www. mathcs. emory. edu/~valerie/courses/fall 1 0/155/resources/op_precedence. html
n n Relational operators. (7 - 1) * 5 6 * 5 30 have lower precedence than math + 3 > 7* 5 + 3 > 35 33 > 35 False Relational operators cannot be chained (unlike math operators) 2 <= x <= 10 error! 9/17/2021 10
Development Process n n n 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!
n n n Multiple selection Nested if (chained condition) Example: letter grade
n Nested if for multiple section problem If then case 1 Else if then case 2 else … end if End if
n 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)
n 0 3 4 2 11 1 Try 0, 4, 11 and see the results! 0 2 4 2 11 3
Indeed x = int(input("enter Int: ")) y=0 if x > 3 : y=1 if x < 10 : y=2 else : y =3 print(y)
n My point n One line in wrong place n n Could cause the program 99. 99% different Location/alignment maters! Blank line is useful Structural procedure n n Ask yourself where (the next program segment line) the computer execution goes Before that, check if you can find the “if” for each “else”
n 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")
n 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")
n Same code, try “a 3” and “a 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")