COSC 235: Programming and Problem Solving Ch. 2: Your first programs!!! Instructor: Dr. X.

15
COSC 235: Programming and Problem Solving Ch. 2: Your first programs!!! Instructor: Dr. X

Transcript of COSC 235: Programming and Problem Solving Ch. 2: Your first programs!!! Instructor: Dr. X.

Page 1: COSC 235: Programming and Problem Solving Ch. 2: Your first programs!!! Instructor: Dr. X.

COSC 235: Programming and Problem Solving

Ch. 2: Your first programs!!!Instructor: Dr. X

Page 2: COSC 235: Programming and Problem Solving Ch. 2: Your first programs!!! Instructor: Dr. X.

Goals

• Understand Input Process Output (IPO)

• Use variables• Understand expressions• Use and understand iteration

2

Page 3: COSC 235: Programming and Problem Solving Ch. 2: Your first programs!!! Instructor: Dr. X.

Software Development Process

• Analyze Problem – what?• Determine Specs - IO• Create Design – how?• Implement – make it• Test/Debug – break it • Maintain - …

Page 4: COSC 235: Programming and Problem Solving Ch. 2: Your first programs!!! Instructor: Dr. X.

Software Development Process

https://opensw.files.wordpress.com/2009/07/software_development1.jpg

Page 5: COSC 235: Programming and Problem Solving Ch. 2: Your first programs!!! Instructor: Dr. X.

Program Elements

• Names• Expressions• Input/Output

Page 6: COSC 235: Programming and Problem Solving Ch. 2: Your first programs!!! Instructor: Dr. X.

Names

• Make them meaningful (descriptive)• Camel case• Use _ to connect two words• Beware!– Symbols– Keywords– Case sensitive

Page 7: COSC 235: Programming and Problem Solving Ch. 2: Your first programs!!! Instructor: Dr. X.

Expressions

• Calculate new values<expr>

• Evaluate right hand side• Assign to left!• Spaces: are they important?

Page 8: COSC 235: Programming and Problem Solving Ch. 2: Your first programs!!! Instructor: Dr. X.

Output first… because it is easier

• Syntaxprint(<expr>, <expr>, …, <expr>)print()

• Semantics: display information (data)

Page 9: COSC 235: Programming and Problem Solving Ch. 2: Your first programs!!! Instructor: Dr. X.

Input is a little tougher…

• Syntax<variable> = input(<prompt>)<variable> = eval(input(<prompt>))

• Semantics?

Page 10: COSC 235: Programming and Problem Solving Ch. 2: Your first programs!!! Instructor: Dr. X.

Assignment

• Syntax<var> = <expr>

• Semantics?• Have we seen this before?

Page 11: COSC 235: Programming and Problem Solving Ch. 2: Your first programs!!! Instructor: Dr. X.

Simultaneous assignment

• Syntax<var>, <var>, …, <var> = <expr>, <expr>, …, <expr>

• Exercise: Write a sequence of assignment statements that exchange the values of thing1 & thing2

http://www.cliparthut.com/thing-1-and-2-dr-seuss-clipart-UnZOY3.html

Page 12: COSC 235: Programming and Problem Solving Ch. 2: Your first programs!!! Instructor: Dr. X.

Exercise

• Print “Hello world” 10 times, then print it 100 times…

• Print the integer numbers from 0..10, 0..100

Page 13: COSC 235: Programming and Problem Solving Ch. 2: Your first programs!!! Instructor: Dr. X.

Loops

https://upload.wikimedia.org/wikipedia/commons/thumb/f/f4/Shockwave_coaster_sfot.jpg/220px-Shockwave_coaster_sfot.jpg

http://3.bp.blogspot.com/-eYwYLrlLWZs/UNlpG3goYgI/AAAAAAAACaI/MdQ2-CPcJYY/s1600/01.gif

http://img1.wikia.nocookie.net/__cb20110112235951/logopedia/images/0/0a/Froot_Loops_logo.jpg

Page 14: COSC 235: Programming and Problem Solving Ch. 2: Your first programs!!! Instructor: Dr. X.

Loops

• Syntaxfor <var> in <sequence>:

<body>

• Semantics: avoid doing repetitive things using copy paste =) OR execute a definite sequence of steps

Page 15: COSC 235: Programming and Problem Solving Ch. 2: Your first programs!!! Instructor: Dr. X.

References

• “Python Programming: An Introduction to Computer Science”, 2nd ed., John M. Zelle