1 / 24

CSC115 Introduction to Computer Programming

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

agustinv
Download Presentation

CSC115 Introduction to Computer Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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. Yes No Boolean Expression Action 1 If controlled Action 2 else controlled Action 3

  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 • 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 • http://www.mathcs.emory.edu/~valerie/courses/fall10/155/resources/op_precedence.html

  10. 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. 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. Multiple selection • Nested if (chained condition) • Example: letter grade

  13. Nested if for multiple section problem If then case 1 Else if then case 2 else … end if End if

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

  15. Are they different, how much? x = int(input("enter Int: "))y = 0if x > 3 : y = 1if x < 10 : y = 2else : y =3print(y) x = int(input("enter Int: "))y = 0if x > 3 : y = 1if x < 10 : y = 2else : y =3print(y)

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

  17. Indeed x = int(input("enter Int: "))y = 0if x > 3 : y = 1 if x < 10 : y = 2else : y =3print(y) x = int(input("enter Int: "))y = 0if x > 3 : y = 1if x < 10 : y = 2else : y =3print(y)

  18. My point • One line in wrong place • 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”

  19. 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")

  20. 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")

  21. 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")

More Related