chapter2

chapter2 - Chapter 2 Algorithms and Design 1 print...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Chapter 2 - Algorithms and Design print Statement input Statement and Variables Assignment Statement if Statement Flowcharts Flow of Control Looping with Flowcharts Looping with Pseudocode Tracing Pseudocode Syntax Summary Loop Terminatation Counter loop User query loop Sentinel value loop Nested Loops
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 print Statement Here's a pseudocode algorithm that calculates the area of a rectangle: print "Enter a length: " input  length print "Enter a width: " input  width set  area  to  length   *   width print "The area is " area The print statement causes the specified item(s) to be displayed. For example, the first line in the above algorithm causes this to be  displayed: Enter a length: You can print different types of items. Later we'll talk about printing  numbers, but for now, let's focus on printing characters…. If you want to print a sequence of characters, surround the characters  with quotes. A sequence of characters surrounded by quotes is called a  string . For example, in the first line, "Enter a length: " is a string. 1 2 print statement
Background image of page 2
3 print "Enter a length:" input  length print "Enter a width:" input  width set  area  to  length   *   width print "The area is"  area The input statement: Causes the algorithm to wait for the  user  to enter a value. After the user enters a value, the value is stored in the  specified  variable . A variable is a box/container that can hold a value. The first two print statements are called  prompts   because they prompt the user to enter a value. 1 2 3 4 input statement
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Assignment Statement print "Enter a length:" input  length print "Enter a width:" input  width set  area  to  length   *   width print "The area is"  area The assignment statement: Puts the right-hand-side expression's value into the left-hand-side  variable. Suppose that 2 and 4 were entered as input for the above  algorithm. Here's a picture of what the assignment statement  does: 2 length 4 width * 8 area assignment statement 1 2
Background image of page 4
if Statement Use an if statement if  you need to ask a question in order to  determine what to do next. There are three forms for an if statement: “if” “if,   else” “if,   else   if” Format for the “if” form of the if statement: if  <condition> <statement(s)> Classroom notation: I use angled brackets "<>" to surround a  description of what should be placed at a particular position.  Thus, don't enter the word "condition," the word "statement(s),"  or the <>'s. Instead, enter an actual condition and an actual  statement(s). Don't forget to indent.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 10/22/2010.

Page1 / 34

chapter2 - Chapter 2 Algorithms and Design 1 print...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online