SlideShare a Scribd company logo
1 of 16
Thinking Like a
 Programmer
    Cate Huston
What Does This Mean?
•   For beginners, writing a (small) program should have
    two main components.

    •   Understanding the problem and breaking it down
        into small steps. Write these down on paper.

    •   Converting those steps into code with the
        correct syntax. These steps should be max 2-3
        lines of code for each step.

•   These can be distinct - this can be helpful when
    you’re learning.
Syntax
•   Don’t worry so much about it!

•   It’s easy to look up later when you convert your
    logic to code.

    •   Google!

    •   Notes.

•   Syntax varies between languages, but the
    process of breaking the problem down is still
    the same.
Photo by Flikr User anikarenina




Think of code constructs as building blocks that you
can use to assemble your program. It’s like lego, you
      have to assemble something bit by bit.
Example 1: Persistence
•   The persistence of a number is how many times you
    have to replace the number by the sum or product of its
    digits until one reaches a single digit.

•   See: http://en.wikipedia.org/wiki/
    Persistence_of_a_number

•   Try and break this down into pieces. In this case, we will
    calculate the product.

•   Clue: we need two loops, one inside the other.

    •   These are called nested loops.
Example 1: Persistence
•   Get in the initial number N

•   P = 0 // Persistence is zero

•   While N > 9

    •   tmp = 1 // 1 is neutral for multiplication

    •   for each digit D in N

        •   tmp = tmp * D

    •   P++ // add one to P

    •   N = tmp // update N with the new number

•   Output P
Converting to Code

• Each of these lines of “pseudocode” require
  at most 2-3 lines of code in any language.
• In many languages, they would require one
  line line of code.
• If you don’t know how to convert anything,
  look it up on Google or in your notes.
Example 2: Guessing
Game
•   Let’s make a guessing game. We’ll break it down
    into phases.

•   Phase 1: The computer makes a random number,
    and asks the user to guess it. It then confirms
    whether or not the user got the number right.

•   Phase 2: We give the user a limited number of
    guesses.

•   Phase 3: We allow the user to play again (if they
    want).
Example 2: Guessing
Game: Phase 1
• Phase 1: Make a random number and ask
  the user to guess it, confirm if they got it
  right or not.
 • Maybe you don’t know how to make a
    random number, but just imagine you do -
    it’s easy to look up if you decide to code
    this.
 • Try it!
Example 2: Guessing
Game: Phase 1
• R = random number 1-50
• N = guess from user
• if (R == N)
 • Output “Well done!”
• else
 • Output “You’re wrong!”
Example 2: Guessing
Game: Phase 2
• Phase 2: Give the user a limited number of
  guesses
• We’ll need a boolean variable to keep track
  if they’ve found it or not
• We’ll need a “for” loop to keep track of how
  many guesses they’ve made and stop when
  they’ve used them all up.
• Try it!
Example 2: Guessing
Game: Phase 2
•   R = random number 1-50
•   correct = false // they haven’t guessed it yet

•   for i = 1 to 10
•   // i is our counter, the user has 10 guesses
    •    N = guess from user
    •    if (R == N)
        •     correct = true
        •     exit loop
•   next i

• if (correct)
  • Output “Well done!”
• else
  • Output “Wrong”
Example 2: Guessing
Game: Phase 3
• Phase 3: Allow the user to play again, if they
  want.
• Use a while loop for this.
• Our code from Phase 2 will stay the same,
  inside this loop.
• Try it!
Example 2: Guessing
Game: Phase 3
•   continue = true // we’ll update this later

•   do

    •    // code from Phase 2 goes here

    •    Output “Play Again? Y/N”

    •    response = input from user

    •    if (response == “N”)

        •   continue = false

•   while (continue)
Photo by Flikr User fd




  Try more examples, as many as you can find. Practice
breaking down the problem down into “code-able” pieces.
    Don’t worry about syntax whilst you’re doing this.
@kittenthebad

http://www.catehuston.com/

catehuston@googlewave.com

More Related Content

What's hot

Full Python in 20 slides
Full Python in 20 slidesFull Python in 20 slides
Full Python in 20 slides
rfojdar
 
The ruby programming language
The ruby programming languageThe ruby programming language
The ruby programming language
praveen0202
 
Douglas Crockford Presentation Goodparts
Douglas Crockford Presentation GoodpartsDouglas Crockford Presentation Goodparts
Douglas Crockford Presentation Goodparts
Ajax Experience 2009
 

What's hot (20)

Of Lambdas and LINQ
Of Lambdas and LINQOf Lambdas and LINQ
Of Lambdas and LINQ
 
Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++
 
An Introduction to Processing
An Introduction to ProcessingAn Introduction to Processing
An Introduction to Processing
 
JavaScript Basics
JavaScript BasicsJavaScript Basics
JavaScript Basics
 
Full Python in 20 slides
Full Python in 20 slidesFull Python in 20 slides
Full Python in 20 slides
 
Python's dynamic nature (rough slides, November 2004)
Python's dynamic nature (rough slides, November 2004)Python's dynamic nature (rough slides, November 2004)
Python's dynamic nature (rough slides, November 2004)
 
More Little Wonders of C#/.NET
More Little Wonders of C#/.NETMore Little Wonders of C#/.NET
More Little Wonders of C#/.NET
 
Web development basics (Part-4)
Web development basics (Part-4)Web development basics (Part-4)
Web development basics (Part-4)
 
Typescript Basics
Typescript BasicsTypescript Basics
Typescript Basics
 
06 LINQ
06 LINQ06 LINQ
06 LINQ
 
Functional programming in TypeScript
Functional programming in TypeScriptFunctional programming in TypeScript
Functional programming in TypeScript
 
TypeScript - Silver Bullet for the Full-stack Developers
TypeScript - Silver Bullet for the Full-stack DevelopersTypeScript - Silver Bullet for the Full-stack Developers
TypeScript - Silver Bullet for the Full-stack Developers
 
Ruby for C# Developers
Ruby for C# DevelopersRuby for C# Developers
Ruby for C# Developers
 
2CPP04 - Objects and Classes
2CPP04 - Objects and Classes2CPP04 - Objects and Classes
2CPP04 - Objects and Classes
 
The ruby programming language
The ruby programming languageThe ruby programming language
The ruby programming language
 
The Go Programing Language 1
The Go Programing Language 1The Go Programing Language 1
The Go Programing Language 1
 
Douglas Crockford Presentation Goodparts
Douglas Crockford Presentation GoodpartsDouglas Crockford Presentation Goodparts
Douglas Crockford Presentation Goodparts
 
Introduction to Kotlin for Android developers
Introduction to Kotlin for Android developersIntroduction to Kotlin for Android developers
Introduction to Kotlin for Android developers
 
TypeScript Best Practices
TypeScript Best PracticesTypeScript Best Practices
TypeScript Best Practices
 
Constructors and destructors in C++ part 2
Constructors and destructors in C++ part 2Constructors and destructors in C++ part 2
Constructors and destructors in C++ part 2
 

Similar to Thinking Like a Programmer

Going loopy - Introduction to Loops.pptx
Going loopy - Introduction to Loops.pptxGoing loopy - Introduction to Loops.pptx
Going loopy - Introduction to Loops.pptx
Amy Nightingale
 
classVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptxclassVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptx
ssusere336f4
 
Case Study of the Unexplained
Case Study of the UnexplainedCase Study of the Unexplained
Case Study of the Unexplained
shannomc
 
Learn python
Learn pythonLearn python
Learn python
mocninja
 

Similar to Thinking Like a Programmer (20)

Going loopy - Introduction to Loops.pptx
Going loopy - Introduction to Loops.pptxGoing loopy - Introduction to Loops.pptx
Going loopy - Introduction to Loops.pptx
 
if, while and for in Python
if, while and for in Pythonif, while and for in Python
if, while and for in Python
 
ForLoops.pptx
ForLoops.pptxForLoops.pptx
ForLoops.pptx
 
classVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptxclassVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptx
 
Mastering Python lesson 3a
Mastering Python lesson 3aMastering Python lesson 3a
Mastering Python lesson 3a
 
Introduction to C ++.pptx
Introduction to C ++.pptxIntroduction to C ++.pptx
Introduction to C ++.pptx
 
Game Development with TouchDevelop
Game Development with TouchDevelopGame Development with TouchDevelop
Game Development with TouchDevelop
 
Mastering Python lesson3b_for_loops
Mastering Python lesson3b_for_loopsMastering Python lesson3b_for_loops
Mastering Python lesson3b_for_loops
 
Agent tic tac-toe - an interactive game for algorithmic and computational th...
Agent tic tac-toe  - an interactive game for algorithmic and computational th...Agent tic tac-toe  - an interactive game for algorithmic and computational th...
Agent tic tac-toe - an interactive game for algorithmic and computational th...
 
Algorithms overview
Algorithms overviewAlgorithms overview
Algorithms overview
 
Finding concurrency problems in core ruby libraries
Finding concurrency problems in core ruby librariesFinding concurrency problems in core ruby libraries
Finding concurrency problems in core ruby libraries
 
CPP10 - Debugging
CPP10 - DebuggingCPP10 - Debugging
CPP10 - Debugging
 
Python week 2 2019 2020 for g10 by eng.osama ghandour
Python week 2 2019 2020 for g10 by eng.osama ghandourPython week 2 2019 2020 for g10 by eng.osama ghandour
Python week 2 2019 2020 for g10 by eng.osama ghandour
 
python.pdf
python.pdfpython.pdf
python.pdf
 
While loop
While loopWhile loop
While loop
 
Five Cliches of Online Game Development
Five Cliches of Online Game DevelopmentFive Cliches of Online Game Development
Five Cliches of Online Game Development
 
C game programming - SDL
C game programming - SDLC game programming - SDL
C game programming - SDL
 
Case Study of the Unexplained
Case Study of the UnexplainedCase Study of the Unexplained
Case Study of the Unexplained
 
PsudoCode.pptx
PsudoCode.pptxPsudoCode.pptx
PsudoCode.pptx
 
Learn python
Learn pythonLearn python
Learn python
 

More from Cate Huston (8)

15 Tools to Make University Easier
15 Tools to Make University Easier15 Tools to Make University Easier
15 Tools to Make University Easier
 
Holiday Science Lecture: Art, Life and Programming
Holiday Science Lecture: Art, Life and ProgrammingHoliday Science Lecture: Art, Life and Programming
Holiday Science Lecture: Art, Life and Programming
 
Art, Life and Programming
Art, Life and ProgrammingArt, Life and Programming
Art, Life and Programming
 
Art, Life and Programming
Art, Life and ProgrammingArt, Life and Programming
Art, Life and Programming
 
Web 2.0
Web 2.0Web 2.0
Web 2.0
 
Processing
ProcessingProcessing
Processing
 
iPhone Commerce
iPhone CommerceiPhone Commerce
iPhone Commerce
 
Microsoft Vista: A Usability Problem
Microsoft Vista: A Usability ProblemMicrosoft Vista: A Usability Problem
Microsoft Vista: A Usability Problem
 

Recently uploaded

Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
中 央社
 

Recently uploaded (20)

Climbers and Creepers used in landscaping
Climbers and Creepers used in landscapingClimbers and Creepers used in landscaping
Climbers and Creepers used in landscaping
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDF
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
 
demyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptxdemyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptx
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio App
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptx
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
 
Supporting Newcomer Multilingual Learners
Supporting Newcomer  Multilingual LearnersSupporting Newcomer  Multilingual Learners
Supporting Newcomer Multilingual Learners
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 

Thinking Like a Programmer

  • 1. Thinking Like a Programmer Cate Huston
  • 2. What Does This Mean? • For beginners, writing a (small) program should have two main components. • Understanding the problem and breaking it down into small steps. Write these down on paper. • Converting those steps into code with the correct syntax. These steps should be max 2-3 lines of code for each step. • These can be distinct - this can be helpful when you’re learning.
  • 3. Syntax • Don’t worry so much about it! • It’s easy to look up later when you convert your logic to code. • Google! • Notes. • Syntax varies between languages, but the process of breaking the problem down is still the same.
  • 4. Photo by Flikr User anikarenina Think of code constructs as building blocks that you can use to assemble your program. It’s like lego, you have to assemble something bit by bit.
  • 5. Example 1: Persistence • The persistence of a number is how many times you have to replace the number by the sum or product of its digits until one reaches a single digit. • See: http://en.wikipedia.org/wiki/ Persistence_of_a_number • Try and break this down into pieces. In this case, we will calculate the product. • Clue: we need two loops, one inside the other. • These are called nested loops.
  • 6. Example 1: Persistence • Get in the initial number N • P = 0 // Persistence is zero • While N > 9 • tmp = 1 // 1 is neutral for multiplication • for each digit D in N • tmp = tmp * D • P++ // add one to P • N = tmp // update N with the new number • Output P
  • 7. Converting to Code • Each of these lines of “pseudocode” require at most 2-3 lines of code in any language. • In many languages, they would require one line line of code. • If you don’t know how to convert anything, look it up on Google or in your notes.
  • 8. Example 2: Guessing Game • Let’s make a guessing game. We’ll break it down into phases. • Phase 1: The computer makes a random number, and asks the user to guess it. It then confirms whether or not the user got the number right. • Phase 2: We give the user a limited number of guesses. • Phase 3: We allow the user to play again (if they want).
  • 9. Example 2: Guessing Game: Phase 1 • Phase 1: Make a random number and ask the user to guess it, confirm if they got it right or not. • Maybe you don’t know how to make a random number, but just imagine you do - it’s easy to look up if you decide to code this. • Try it!
  • 10. Example 2: Guessing Game: Phase 1 • R = random number 1-50 • N = guess from user • if (R == N) • Output “Well done!” • else • Output “You’re wrong!”
  • 11. Example 2: Guessing Game: Phase 2 • Phase 2: Give the user a limited number of guesses • We’ll need a boolean variable to keep track if they’ve found it or not • We’ll need a “for” loop to keep track of how many guesses they’ve made and stop when they’ve used them all up. • Try it!
  • 12. Example 2: Guessing Game: Phase 2 • R = random number 1-50 • correct = false // they haven’t guessed it yet • for i = 1 to 10 • // i is our counter, the user has 10 guesses • N = guess from user • if (R == N) • correct = true • exit loop • next i • if (correct) • Output “Well done!” • else • Output “Wrong”
  • 13. Example 2: Guessing Game: Phase 3 • Phase 3: Allow the user to play again, if they want. • Use a while loop for this. • Our code from Phase 2 will stay the same, inside this loop. • Try it!
  • 14. Example 2: Guessing Game: Phase 3 • continue = true // we’ll update this later • do • // code from Phase 2 goes here • Output “Play Again? Y/N” • response = input from user • if (response == “N”) • continue = false • while (continue)
  • 15. Photo by Flikr User fd Try more examples, as many as you can find. Practice breaking down the problem down into “code-able” pieces. Don’t worry about syntax whilst you’re doing this.