tailieunhanh - Lecture Introduction to programming with Java - Chapter 2: Algorithms and design

This chapter presents the following content: 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. | 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 1 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. print statement 2 input Statement & Variables 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. input statement 3 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: assignment statement 4 if Statement Use an if statement if you need to ask a question | 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 1 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,