SlideShare une entreprise Scribd logo
1  sur  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

Contenu connexe

Tendances

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
 

Tendances (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
 

Similaire à 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
 

Similaire à 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
 

Plus de 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
 

Plus de 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
 

Dernier

Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxMusic 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxleah joy valeriano
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationRosabel UA
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptshraddhaparab530
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 

Dernier (20)

Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxMusic 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translation
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.ppt
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 

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.