SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Was ist angesagt?

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
 

Was ist angesagt? (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
 

Ähnlich wie 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
 

Ähnlich wie 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
 

Mehr von 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
 

Mehr von 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
 

Kürzlich hochgeladen

Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
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
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 

Kürzlich hochgeladen (20)

Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........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
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 

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.