Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC115 Introduction to Computer Programming

Similar presentations


Presentation on theme: "CSC115 Introduction to Computer Programming"— Presentation transcript:

1 CSC115 Introduction to Computer Programming
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA 19383

2 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

3 Review Output Number String Combination (by using + and str) Input
Type Conversion (float and int) Processing Feel it in the test

4 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?

5 No Yes Condition Action 1 Action 2 Action 3

6 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

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

8 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

9 Truth table Precedence order
F C2 C1 and C2 C1 or C2 not C1 Truth table Precedence order

10 Relational operators have lower precedence than math operators.
(7 - 1) * > 7* 5 6 * > 35 > 35 33 > 35 False Relational operators cannot be chained (unlike math operators) 2 <= x <= 10 error! 11/9/2018

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

12

13 Multiple selection Nested if (chained condition) Example: letter grade

14 Nested if for multiple section problem
If then case 1 Else if then case 2 else end if End if

15

16 Conditional and alternative execution
if condition : action 1 (statements 1) # no else! action 3 (statement 3)

17

18 Are they different, how much?
x = int(input("enter Int: ")) y = 0 if x > 3 : y = 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)

19 Try 0, 4, 11 and see the results! 3 4 2 11 1 2 4 11 3

20 Indeed x = int(input("enter Int: ")) y = 0 if x > 3 : y = 1
x = int(input("enter Int: ")) y = 0 if x > 3 : y = 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)

21 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”

22 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")

23 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")

24 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")


Download ppt "CSC115 Introduction to Computer Programming"

Similar presentations


Ads by Google