c++ if….else statements and flowcharts october 10, 2007

22
C++ If….Else Statements and Flowcharts October 10, 2007

Upload: ethan-holmes

Post on 30-Dec-2015

223 views

Category:

Documents


2 download

TRANSCRIPT

C++ If….Else Statements

andFlowcharts

October 10, 2007

Algorithms:

Is a finite sequence of effective statements that, when applied to the problem, will solve it!

An effective statement is a clear, unambiguous instruction that can be carried out.

Each algorithm you develop should have a specific beginning; at the completion of one step, have the next step uniquely determined; and have an ending that is reached in a reasonable amount of time.

Example: “Rise-and-shine algorithm”

Problem:

A student getting out of bed and going to school.

What is the algorithm?

1. Get out of bed.

2. Take off pajamas.

3. Take a shower.

4. Get dressed.

5. Eat breakfast.

6. Carpool to school/Take a bus.

Another one: Watching Television

1. Sit down on the sofa.2. Pick up the remote control.3. Point the remote control at the television.4. Press the Power button on the remote control.5. Wait a few seconds for the television to warm up.6. Do you like the program? If so, go to step 9.7. You do not like the program – press the Next

Channel button on the remote control.8. Got to step 6.9. Put the remote control on the coffee table.10. Relax.

Pseudocode:

Step-by-step set of instructions that are very similar to computer programming instructions.

Written in every day English. Helps programmers develop algorithms. User friendly. They are the steps on how to solve the problem. They can be converted in computer code.

Lets translate our Television-watching steps into pseudo-code.

Begin task: Watching Television.1. Sit down on the sofa.2. Pick up the remote control.3. Point the remote control at the television.4. Press the Power button on the remote control.5. Wait a few seconds.6. Do you like the program?

a. Yes. Go to step 9.b. No. Go to step 7.

7. Press the Next Channel button on the remote control.8. Go to step 5.9. Put the remote control on the coffee table.10. Relax. End task: Watching Television.

Flowcharts!

Is a graphical representation of an algorithm or of a portion of an algorithm.

Are drawn using certain special-purpose symbols such as rectangles, diamonds, ovals, and small circles.

These symbols are connected by arrows called flowlines.

Are useful for developing and representing algorithms.

Clearly show how control structures operate.

Lets look at the symbols!

Rectangle

•Also called an Action symbol.

•Indicates any type of action. Examples: a calculation or an input/output operation.

Action 1

Action 2

Action 3

E

X

A

M

P

L

E

Flowline Symbol:

The lines connect other symbols, and the arrowheads are mandatory only for right-to-left and bottom-to-top flow.

Oval Symbol:

Represents the beginning, the end, or a point of interruption or delay in a program.

Connector Symbol:

Represents any entry from, or exit to, another part of the flowchart. Also serves as an off-page connector.

Decision Symbol:

Represents a decision that determines which of a number of alternative paths is to be followed.

Let’s turn our Pseudocode for watching television into a flowchart!

Start

1

2

3

4

5

Y N

Go to Step 9 Go to step 7

Stop

7

8

9

10Do you like the program?

Homework:

Develop a pseudocode and flowchart for the following. Pick one! Your choice.

1. On a sheet of paper, draw a square of a specific size.

2. Get a drink of water.

3. Make a phone call.

The if…else Statement

Allows the programmer to specify that a different action is to be performed when the condition is true than when the condition is false.

Syntax:

if (condition)

statement;

else

statement;

Example:

Let’s say:

If student’s grade is greater than or equal to 60

Print “Passed”

Else

Print “Failed”

_______________________________________

How would you write this in C++?

Did you pass or fail?

if (grade >= 60)

cout<<“Passed”;

else

cout<<“Failed”;

___________________________________

Did you write your if statement correctly?

Flowchart for the example if statement:

Grade >= 60

Print Failed

Print Pass

False True

Lets try a simple program:#include <iostream.h>main ( ){float number; //User number value.cout<<“\n”;cout<< “Give me a number from 1 o 10 => “;cin >> number;if (number > 5.0)

cout <<“Your number is greater than 5.\n”;else

cout <<“Your number is less than or equal to 5.\n”;cout <<“The value of your number was “<<number<<“\n”;return 0;}

For today:

Create a flowchart of today’s program. Turn in before you leave! Study for quiz on Friday!

For tomorrow:

Turn in your homework. More if…else statements!