Programming Control Structures

Slides:



Advertisements
Similar presentations
Divide. Evaluate power – 3 = – 3 EXAMPLE – 3 = 3 2 – – 3 = 6 – 3 Multiply. Evaluate expressions Multiply and divide from.
Advertisements

Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
11.3 Function Prototypes A Function Prototype contains the function’s return type, name and parameter list Writing the function prototype is “declaring”
Midterm Extra Credit 1. This assignment is worth 6 points of your 74 point midterm. Assuming you receive all 6 points, how much will your midterm percentage.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Looping Exercises Deciding Which Loop to Use At this.
Chapter 2 - Algorithms and Design
1 Chapter 2 - Algorithms and Design print Statement input Statement and Variables Assignment Statement if Statement Flowcharts Flow of Control Looping.
Count Controlled Loops (Nested) Ain’t no sunshine when she’s gone …
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
Tutorial 9 Iteration. Reminder Assignment 8 is due Wednesday.
CHAPTER 1 Whole Numbers Slide 2Copyright 2012, 2008, 2004, 2000 Pearson Education, Inc. 1.1Standard Notation 1.2Addition 1.3Subtraction 1.4Multiplication.
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
Precedence Operators Error Types
GCSE COMPUTER SCIENCE Practical Programming using Python
Introduction to Algorithms
REPETITION CONTROL STRUCTURE
Chapter 8 Multidimensional Arrays
Chapter 1 Introduction to Java
Loops BIS1523 – Lecture 10.
Chapter 8 Multidimensional Arrays
Topics Introduction to Repetition Structures
C++ Programming: CS150 For.
CHAPTER 5A Loop Structure
Chapter 5: Control Structure
2-D Lists Taken from notes by Dr. Neil Moore
JavaScript: Functions.
Topics Introduction to Repetition Structures
Lecture 07 More Repetition Richard Gesick.
Lecture 4B More Repetition Richard Gesick
Siti Nurbaya Ismail Senior Lecturer
While Loops BIS1523 – Lecture 12.
Chapter 8 Multidimensional Arrays
Week 8 - Programming II Today – more features: Loop control
Control Structure Senior Lecturer
January 2014 Compass Review.
Chapter 8 Search and Sort
Chapter 8 Multidimensional Arrays
Building Java Programs
Count Controlled Loops (Nested)
Java Programming Arrays
Chapter 8 Multidimensional Arrays
Introduction To Programming Information Technology , 1’st Semester
Chapter 8 Multidimensional Arrays
Chapter 8 Multidimensional Arrays
MSIS 655 Advanced Business Applications Programming
CSC115 Introduction to Computer Programming
Using the Target Variable Inside the Loop
Introduction to Algorithms
Programming Control Structures with JavaScript Part 2
Chapter 8 Multidimensional Arrays
Loops and Arrays in JavaScript
Dr. Sampath Jayarathna Cal Poly Pomona
For loops Taken from notes by Dr. Neil Moore
Reading and Writing Text Files
Business Math Assignment
Topics Introduction to Repetition Structures
Assignment Operator, Mathematical Operators & String Operators (basics) MIS 3406 Department of MIS Fox School of Business Temple University.
Functions MIS 3406 Department of MIS Fox School of Business
Arrays & Loops.
Chapter 7 Multidimensional Arrays
2-D Lists Taken from notes by Dr. Neil Moore
Class code for pythonroom.com cchsp2cs
Chapter 8 Multidimensional Arrays
Logial Operators & Conditional Logic
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
For the given relation: (1, 4), (3, 7) (5, 11), (7, 11)
H446 Computer Science.
Week 7 - Monday CS 121.
Presentation transcript:

Programming Control Structures MIS 3406 Department of MIS Fox School of Business Temple University

The syntax of a while loop

The syntax of a do-while loop

Another do-while loop example 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?

The syntax of a for loop

Another example of a for loop Let’s see some loops at work! (see rocket.zip)

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.

The syntax for creating an array

How to add values to an array 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.

How big is the array? QUESTION: Using the totals array from the last slide, what number would this command display? alert(totals.length);

Putting arrays to work

Putting arrays to work(2) 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?

GuessANumber.js (cont.) 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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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: 2 Average daily increase: 30% Number of days to multiply: 10

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

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