22
Pseudo Code and Flow Charts Chapter 1 Lesson 2

Pseudo Code and Flow Charts - mrjcfhs.commrjcfhs.com/cp1/ch1/lesson_2.pdf• Each programming language has a way to determine if we have reached the end of the file. We will use eof

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Pseudo Code and Flow Charts - mrjcfhs.commrjcfhs.com/cp1/ch1/lesson_2.pdf• Each programming language has a way to determine if we have reached the end of the file. We will use eof

Pseudo Code and Flow Charts

Chapter 1 Lesson 2

Page 2: Pseudo Code and Flow Charts - mrjcfhs.commrjcfhs.com/cp1/ch1/lesson_2.pdf• Each programming language has a way to determine if we have reached the end of the file. We will use eof

Using Pseudocode Statementsand Flowchart Symbols

• Pseudocode

– English-like representation of the logical steps it takes to solve a problem

• Flowchart

– Pictorial representation of the logical steps it takes to solve a problem

2Programming Logic and Design, Eighth

Edition

Page 3: Pseudo Code and Flow Charts - mrjcfhs.commrjcfhs.com/cp1/ch1/lesson_2.pdf• Each programming language has a way to determine if we have reached the end of the file. We will use eof

Writing Pseudocode

• Pseudocode representation of a number-doubling problemstart

input myNumber

set myAnswer = myNumber * 2

output myAnswer

stop

3Programming Logic and Design, Eighth

Edition

Page 4: Pseudo Code and Flow Charts - mrjcfhs.commrjcfhs.com/cp1/ch1/lesson_2.pdf• Each programming language has a way to determine if we have reached the end of the file. We will use eof

Writing Pseudocode (continued)

4Programming Logic and Design, Eighth

Edition

Page 5: Pseudo Code and Flow Charts - mrjcfhs.commrjcfhs.com/cp1/ch1/lesson_2.pdf• Each programming language has a way to determine if we have reached the end of the file. We will use eof

Drawing Flowcharts

• Create a flowchart– Draw geometric shapes that contain the individual

statements

– Connect shapes with arrows

• Input symbol– Indicates input operation

– Parallelogram

• Processing symbol– Contains processing statements such as arithmetic

– Rectangle

5Programming Logic and Design, Eighth

Edition

Page 6: Pseudo Code and Flow Charts - mrjcfhs.commrjcfhs.com/cp1/ch1/lesson_2.pdf• Each programming language has a way to determine if we have reached the end of the file. We will use eof

Drawing Flowcharts (continued)

• Output symbol– Represents output statements

– Parallelogram

• Flowlines– Arrows that connect steps

• Terminal symbols– Start/stop symbols

– Shaped like a racetrack

– Also called lozenges

6Programming Logic and Design, Eighth

Edition

Page 7: Pseudo Code and Flow Charts - mrjcfhs.commrjcfhs.com/cp1/ch1/lesson_2.pdf• Each programming language has a way to determine if we have reached the end of the file. We will use eof

Drawing Flowcharts (continued)

Figure 1-6 Flowchart and pseudocode of program that doubles a number

7Programming Logic and Design, Eighth

Edition

Page 8: Pseudo Code and Flow Charts - mrjcfhs.commrjcfhs.com/cp1/ch1/lesson_2.pdf• Each programming language has a way to determine if we have reached the end of the file. We will use eof

Repeating Instructions• Program in Figure 1-6 only works for one number• What if I wanted to obtain and double 10,000

numbers instead of just one?– Not feasible to run the program over and over 10,000

times– We could repeat the lines of code that get a number,

double it and output 10,000 times in the program• Not feasible to add these lines of code to the program 10,000

more times! This is a code maintenance nightmare.

• The solution: Create a loop (repetition of a series of steps) instead– Avoid an infinite loop (repeating flow of logic that never

ends)

8Programming Logic and Design, Eighth

Edition

Page 9: Pseudo Code and Flow Charts - mrjcfhs.commrjcfhs.com/cp1/ch1/lesson_2.pdf• Each programming language has a way to determine if we have reached the end of the file. We will use eof

Repeating Instructions (continued)

Figure 1-7 Inefficient pseudocode for program that doubles 10,000 numbers

9Programming Logic and Design, Eighth

Edition

Page 10: Pseudo Code and Flow Charts - mrjcfhs.commrjcfhs.com/cp1/ch1/lesson_2.pdf• Each programming language has a way to determine if we have reached the end of the file. We will use eof

Repeating Instructions (continued)

Figure 1-8 Flowchart of infinite number-doubling program

10Programming Logic and Design, Eighth

Edition

• In the flowchart to the right, we see a loop in action.

• Notice the flowline bending back up to get another number, double it and output the result. The action keeps repeating.

– The memory locations used to store myNumber and myAnswerare reused each time through the loop to process the next number (variables)

• The idea to use a loop is good, but notice this loop never ends. It is called an infinite loop and is usually something we want to avoid.

Page 11: Pseudo Code and Flow Charts - mrjcfhs.commrjcfhs.com/cp1/ch1/lesson_2.pdf• Each programming language has a way to determine if we have reached the end of the file. We will use eof

Using a Sentinel Value to End a Program• So how can we create logic that repeats but eventually stops?

• The key is the ability to make a decision within the program

– We can test a value to determine whether the loop should continue

– Flowchart Decision symbol

• Diamond shape

• There are always two possible outcomes of a decision –yes or no (true or false)

• Use a dummy value to indicate that the loop should end

– Data-entry value that the user will never need as a “real” value

– User enters the value to indicate that the loop should end• To do this the user must know what the value is to type when they are done

processing

– This dummy value is called a Sentinel value• The decision symbol above tests to see if myNumber is equal to the sentinel value

of zero

11Programming Logic and Design, Eighth

Edition

Page 12: Pseudo Code and Flow Charts - mrjcfhs.commrjcfhs.com/cp1/ch1/lesson_2.pdf• Each programming language has a way to determine if we have reached the end of the file. We will use eof

Using a Sentinel Value to End a Program• So the example in the previous slide showed a decision symbol that checks to

see if myNumber is zero (or not)

• The idea here is that the user will enter zero when they are done using the program.

• However, not all programs rely on user data entry from a keyboard.

• Many computer programs read data from an input device, such as a disk.• Our program may be reading data from a file on the C drive, for example.

• When this is the case, we often need to check to see if we have reached the end of the data in the file• When we reach the end of the file, we want to stop the loop

• Many programming languages used the term eof (“end of file”) to indicate that we have reached the end of the file• Each programming language has a way to determine if we have reached the end of

the file. We will use eof to represent this in our logic.

– The eof marker at the end of a file automatically acts as a sentinel

12Programming Logic and Design, Eighth

Edition

Page 13: Pseudo Code and Flow Charts - mrjcfhs.commrjcfhs.com/cp1/ch1/lesson_2.pdf• Each programming language has a way to determine if we have reached the end of the file. We will use eof

Using a Sentinel Value to End a Program• The next slide shows how to represent a loop that checks for

the myNumber value of 0. When the value of 0 is encountered, the loop (and program) ends

• The second flowchart is almost identical but checks for eofand stops the loop (and program) if eof is encountered

• Although the figure from the book indicates “don’t do it”, we are going to accept this solution for now.– The book is indicating that there is a better way to structure the

solution and we will learn how to do it in Chapter 3!

– For now, let’s just get acquainted with the flowcharting symbols and how they can represent our logic.

13Programming Logic and Design, Eighth

Edition

Page 14: Pseudo Code and Flow Charts - mrjcfhs.commrjcfhs.com/cp1/ch1/lesson_2.pdf• Each programming language has a way to determine if we have reached the end of the file. We will use eof

Using a Sentinel Value to Enda Program (continued)

Figure 1-9 Flowchart of number-doubling program with sentinel value of 0

14Programming Logic and Design, Eighth

Edition

Figure 1-10 Flowchart using eof

Page 15: Pseudo Code and Flow Charts - mrjcfhs.commrjcfhs.com/cp1/ch1/lesson_2.pdf• Each programming language has a way to determine if we have reached the end of the file. We will use eof

Understanding Programming Environments• Understanding Programming Environments

– Recall that you begin solving a problem with a pseudocode or flowchart solution, however eventually you will need to translate that logic into a programming language

– A programming environment is where you can go to actually create your programming solution and execute (run) it

– Text Editor is used to create simple text files• Examples are Notepad or Notepad++• Doesn’t require a lot of disk space

– Integrated Development Environment (IDE) provides an editor, compiler, and other programming tools• Microsoft Visual Studio IDE – Recall that we are using this for the class!

– Regardless of whether a text editor or an IDE is used, the program itself and its logic is the same.

15

Page 16: Pseudo Code and Flow Charts - mrjcfhs.commrjcfhs.com/cp1/ch1/lesson_2.pdf• Each programming language has a way to determine if we have reached the end of the file. We will use eof

Understanding Programming Environments (continued)

Figure 1-12 A C# number-doubling program in Visual Studio

16Programming Logic and Design, Eighth

Edition

One of the major advantages of using an IDE is that different colors are used by the IDE to make items easier to find. It will also highlight syntax

errors for you, offers code completion, includes debugging tools, and allows you to run your program from within the software.

Page 17: Pseudo Code and Flow Charts - mrjcfhs.commrjcfhs.com/cp1/ch1/lesson_2.pdf• Each programming language has a way to determine if we have reached the end of the file. We will use eof

Understanding User Environments

• Understanding User Environments– A user might execute a program you have written in any

number of environments.

– One example is command line. Command Line is a location on your computer screen at which you type text entries to communicate with the computer’s operating system

– Many programs are not run at the command line, but are run using a graphical user interface.• A graphical user interface, or GUI (pronounced gooey), allows users

to interact with a program in a graphical environment

– Regardless of the type of program, the logical process behind the scenes is the same.

17Programming Logic and Design, Eighth

Edition

Page 18: Pseudo Code and Flow Charts - mrjcfhs.commrjcfhs.com/cp1/ch1/lesson_2.pdf• Each programming language has a way to determine if we have reached the end of the file. We will use eof

Understanding User Environments (continued)

Figure 1-13 Executing a number-doubling program

in a command-line environment

18Programming Logic and Design, Eighth

Edition

Page 19: Pseudo Code and Flow Charts - mrjcfhs.commrjcfhs.com/cp1/ch1/lesson_2.pdf• Each programming language has a way to determine if we have reached the end of the file. We will use eof

Understanding User Environments (continued)

Figure 1-14 Executing a number-doubling program

in a GUI environment

19Programming Logic and Design, Eighth

Edition

Page 20: Pseudo Code and Flow Charts - mrjcfhs.commrjcfhs.com/cp1/ch1/lesson_2.pdf• Each programming language has a way to determine if we have reached the end of the file. We will use eof

Understanding the Evolutionof Programming Models

• People have been writing modern computer programs since the 1940s

• Newer programming languages

– Look much more like natural language

– Are easier to use

– Create self-contained modules or program segments that can be pieced together in a variety of ways

20Programming Logic and Design, Eighth

Edition

Page 21: Pseudo Code and Flow Charts - mrjcfhs.commrjcfhs.com/cp1/ch1/lesson_2.pdf• Each programming language has a way to determine if we have reached the end of the file. We will use eof

Understanding the Evolutionof Programming Models (continued)

• Major models or paradigms used by programmers

– Procedural programming

• Focuses on the procedures that programmers create

– Object-oriented programming

• Focuses on objects, or “things,” and describes their features (or attributes) and their behaviors

21Programming Logic and Design, Eighth

Edition

Page 22: Pseudo Code and Flow Charts - mrjcfhs.commrjcfhs.com/cp1/ch1/lesson_2.pdf• Each programming language has a way to determine if we have reached the end of the file. We will use eof

Summary

• Hardware and software accomplish input, processing, and output

• Logic must be developed correctly• Logical errors are much more difficult to locate

than syntax errors• Use flowcharts, pseudocode, IPO charts, and TOE

charts to plan the logic• Avoid infinite loops by testing for a sentinel value• Use a text editor or an IDE to enter your program

statements

22Programming Logic and Design, Eighth

Edition