/
 Programming Control  Structures  Programming Control  Structures

Programming Control Structures - PowerPoint Presentation

kittie-lecroy
kittie-lecroy . @kittie-lecroy
Follow
343 views
Uploaded On 2020-04-08

Programming Control Structures - PPT Presentation

MIS 3406 Department of MIS Fox School of Business Temple University The syntax of a while loop 2 The syntax of a dowhile loop 3 Another dowhile loop example 4 DISCUSS what in your own words is this code saying ID: 776442

program number loop user program number loop user write array display month enter totals day time total period factorial

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document " Programming Control Structures " is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Programming Control Structures

MIS 3406Department of MISFox School of BusinessTemple University

Slide2

The syntax of a while loop

2

Slide3

The syntax of a do-while loop

3

Slide4

Another do-while loop example

4

DISCUSS – what, in your own words, is this code saying?

If you wanted to describe these 9 lines of code with a comment, what would the comment be?

Slide5

The syntax of a for loop

5

Slide6

Another example of a for loop

6

Let’s see some loops at work! (see rocket.zip)

Slide7

What is an array?

An array is a data type that can contain one or more items called

elements. Each element stores a value that you can refer to with an index. The length of the array indicates the number of elements that it contains.In short, an array is a structure that allows us to store multiple pieces of similar data.

Slide8

The syntax for creating an array

8

Slide9

How to add values to an array

9

This number, inside the square brackets, is the index to the array. It indicates what piece of the array we are going to add a value to.

QUESTION: What is the index of the

first

value in the above array?

Careful! JavaScript arrays are zero-based and that is often a source of trouble for first-time coders.

Now that the totals array exist, and some values have been assigned to it, I can use totals[2], totals[1] and totals[0] like any other primitive variable.

Slide10

How big is the array?

10

QUESTION: Using the totals array from the last slide, what number would this command display?

alert(

totals.length

);

Slide11

Putting arrays to work

11

Slide12

Putting arrays to work(2)

12

QUESTION: Using the JavaScript covered in this lecture, what one line of code here could you add after “what’s next” that would alert the user and display the average of totals?

Slide13

13

Slide14

GuessANumber.js (cont.)

14

Update GuessANumber.js so that the program loops while the guess does not match the randomly generated number. Each time through the loop the program will tell the user if their latest guess was too high or too low. The program will also keep track of how many total guesses the user made before they guessed the actual number. Once they guess the correct number, the program will display an appropriate message including how many guesses they made.

Slide15

BugCollector.js

A bug collector collects bugs every day for five days. Write a program that keeps a running total of the number of bugs collected during the five days. The loop should ask for the number of bugs collected for each day, and when the loop is finished, the program should display the total number of bugs collected.

Slide16

CaloriesBurned.js

Running on a particular treadmill you burn 4.2 calories per minute. Write a program that uses a loop to display the number of calories burned after 10, 15, 20, 25, and 30 minutes.

Slide17

BudgetAnalysis.js

Write a program that asks the user to enter the amount that he or she has budgeted for a month. A loop should then prompt the user to enter each of his or her expenses for the month and keep a running total. When the loop finishes, the program should display the amount that the user is over or under budget.

Slide18

DistanceTraveled.js

The distance a vehicle travels can be calculated as follows:

distance=

speed×time

For example, if a train travels 40 miles per hour for three hours, the distance traveled is 120 miles. Write a program that asks the user for the speed of a vehicle (in miles per hour) and the number of hours it has traveled. It should then use a loop to display the distance the vehicle has traveled for each hour of that time period.

Slide19

AverageRainfall.js

Write a program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program should first ask for the number of years. The outer loop will iterate once for each year. The inner loop will iterate twelve times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month. After all iterations, the program should display the number of months, the total inches of rainfall, and the average rainfall per month for the entire period.

Slide20

C2FTable.js

Write a program that displays a table of the Celsius temperatures 0 through 20 and their Fahrenheit equivalents. The formula for converting a temperature from Celsius to Fahrenheit is

F=(9/5)C+32

where

F

is the Fahrenheit temperature, and

C

is the Celsius temperature. Your program must use a loop to display the table.

Slide21

PenniesForPay.js

Write a program that calculates the amount of money a person would earn over a period of time if his or her salary is one penny the first day, two pennies the second day, and continues to double each day. The program should ask the user for the number of days. Display a table showing what the salary was for each day, then show the total pay at the end of the period. The output should be displayed in a dollar amount, not the number of pennies.

Slide22

SumOfNumbers.js

Write a program with a loop that asks the user to enter a series of positive numbers. The user should enter a negative number to signal the end of the series. After all the positive numbers have been entered, the program should display their sum.

Slide23

OceanLevels.js

Assuming the ocean’s level is currently rising at about 1.6 millimeters per year, create an application that displays the number of millimeters that the ocean will have risen each year for the next 25 years.

Slide24

TuitionIncrease.js

At one college, the tuition for a full-time student is $8,000 per semester. It has been announced that the tuition will increase by 3 percent each year for the next 5 years. Write a program with a loop that displays the projected semester tuition amount for the next 5 years.

Slide25

WeightLoss.js

If a moderately active person cuts their calorie intake by 500 calories a day, they can typically lose about 4 pounds a month. Write a program that lets the user enter their starting weight, then creates and displays a table showing what their expected weight will be at the end of each month for the next 6 months if they stay on this diet.

Slide26

Factorial.js

In mathematics, the notation

n

! represents the factorial of the nonnegative integer

n

. The factorial of

n

is the product of all the nonnegative integers from 1 to

n

. For example,

7!=1×2×3×4×5×6×7=5,040

and

4!=1×2×3×4=24

Write a program that lets the user enter a nonnegative integer then uses a loop to calculate the factorial of that number. Display the factorial.

Slide27

Population.js

Write a program that predicts the approximate size of a population of organisms. The application should allow the user to enter the starting number of organisms, the average daily population increase (as a percentage), and the number of days the organisms will be left to multiply. For example, assume the user enters the following values:Starting number of organisms: 2Average daily increase: 30%Number of days to multiply: 10

Slide28

Pattern1.js

Write a program that uses nested loops to draw the following pattern:

Slide29

Pattern2.js

Write a program that uses nested loops to draw the following pattern: