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

Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++Anil Bapat
 
An Introduction to Processing
An Introduction to ProcessingAn Introduction to Processing
An Introduction to ProcessingCate Huston
 
Full Python in 20 slides
Full Python in 20 slidesFull Python in 20 slides
Full Python in 20 slidesrfojdar
 
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)Kiran Jonnalagadda
 
More Little Wonders of C#/.NET
More Little Wonders of C#/.NETMore Little Wonders of C#/.NET
More Little Wonders of C#/.NETBlackRabbitCoder
 
Web development basics (Part-4)
Web development basics (Part-4)Web development basics (Part-4)
Web development basics (Part-4)Rajat Pratap Singh
 
Functional programming in TypeScript
Functional programming in TypeScriptFunctional programming in TypeScript
Functional programming in TypeScriptbinDebug WorkSpace
 
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 DevelopersRutenis Turcinas
 
Ruby for C# Developers
Ruby for C# DevelopersRuby for C# Developers
Ruby for C# DevelopersCory Foy
 
2CPP04 - Objects and Classes
2CPP04 - Objects and Classes2CPP04 - Objects and Classes
2CPP04 - Objects and ClassesMichael Heron
 
The ruby programming language
The ruby programming languageThe ruby programming language
The ruby programming languagepraveen0202
 
The Go Programing Language 1
The Go Programing Language 1The Go Programing Language 1
The Go Programing Language 1İbrahim Kürce
 
Douglas Crockford Presentation Goodparts
Douglas Crockford Presentation GoodpartsDouglas Crockford Presentation Goodparts
Douglas Crockford Presentation GoodpartsAjax Experience 2009
 
Introduction to Kotlin for Android developers
Introduction to Kotlin for Android developersIntroduction to Kotlin for Android developers
Introduction to Kotlin for Android developersMohamed Wael
 
TypeScript Best Practices
TypeScript Best PracticesTypeScript Best Practices
TypeScript Best Practicesfelixbillon
 

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.pptxAmy Nightingale
 
if, while and for in Python
if, while and for in Pythonif, while and for in Python
if, while and for in PythonPranavSB
 
classVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptxclassVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptxssusere336f4
 
Mastering Python lesson 3a
Mastering Python lesson 3aMastering Python lesson 3a
Mastering Python lesson 3aRuth Marvin
 
Game Development with TouchDevelop
Game Development with TouchDevelopGame Development with TouchDevelop
Game Development with TouchDevelopSage Franch
 
Mastering Python lesson3b_for_loops
Mastering Python lesson3b_for_loopsMastering Python lesson3b_for_loops
Mastering Python lesson3b_for_loopsRuth Marvin
 
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...LorennRuster
 
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 librarieslouisadunne
 
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 ghandourOsama Ghandour Geris
 
Five Cliches of Online Game Development
Five Cliches of Online Game DevelopmentFive Cliches of Online Game Development
Five Cliches of Online Game Developmentiandundore
 
C game programming - SDL
C game programming - SDLC game programming - SDL
C game programming - SDLWingston
 
Case Study of the Unexplained
Case Study of the UnexplainedCase Study of the Unexplained
Case Study of the Unexplainedshannomc
 
Learn python
Learn pythonLearn python
Learn pythonmocninja
 

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

15 Tools to Make University Easier
15 Tools to Make University Easier15 Tools to Make University Easier
15 Tools to Make University EasierCate Huston
 
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 ProgrammingCate Huston
 
Art, Life and Programming
Art, Life and ProgrammingArt, Life and Programming
Art, Life and ProgrammingCate Huston
 
Art, Life and Programming
Art, Life and ProgrammingArt, Life and Programming
Art, Life and ProgrammingCate Huston
 
Microsoft Vista: A Usability Problem
Microsoft Vista: A Usability ProblemMicrosoft Vista: A Usability Problem
Microsoft Vista: A Usability ProblemCate Huston
 

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

How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 

Recently uploaded (20)

How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 

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.