Coding Design Tools Rachel Gauci. Task: Counting On Create a program that will print out a sequence...

9
Coding Design Tools Rachel Gauci

Transcript of Coding Design Tools Rachel Gauci. Task: Counting On Create a program that will print out a sequence...

Page 1: Coding Design Tools Rachel Gauci. Task: Counting On Create a program that will print out a sequence of numbers from "1" to a "number entered”. Decision’s.

Coding Design Tools

Rachel Gauci

Page 2: Coding Design Tools Rachel Gauci. Task: Counting On Create a program that will print out a sequence of numbers from "1" to a "number entered”. Decision’s.

Task: Counting OnCreate a program that will print out a sequence of

numbers from "1" to a "number entered”.

Decision’s need to be made before moving onto designing…1. What does the task want?2. What type of script would best fit the requirements (e.g.

IF ELSE statement, FOR/WHILE LOOPS etc.)

Page 3: Coding Design Tools Rachel Gauci. Task: Counting On Create a program that will print out a sequence of numbers from "1" to a "number entered”. Decision’s.

LOOPSFOR LOOPS- the loop is repeated a "specific" number of times, determined by the program or the user.  The loop "counts" the number of times the body will be executed.  This loop is a good choice when the number of repetitions is known, or can be supplied by the user. (set initial variable; condition; action on the variable)for (var i : int = 0 ; i < numEnemies ; i++)

While Loops:  The loop must repeat until a certain "condition" is met.  If the "condition" is FALSE at the beginning of the loop, the loop is never executed.  The "condition" may be determined by the user at the keyboard.  The "condition" may be a numeric or an alphanumeric entry.  This is a good, solid looping process with applications to numerous situations.

while (condition/expression) { Statement/s to be executed if expression is true }

Page 4: Coding Design Tools Rachel Gauci. Task: Counting On Create a program that will print out a sequence of numbers from "1" to a "number entered”. Decision’s.

The for loop is used to repeat a section of code known number of times. Sometimes it is the computer that knows how many times, not you, but it is still known. Some examples:

Unknown number of times:• "Ask the User to Guess a pre-determined number between 1 and 100".

You have no way of knowing how many guesses it will take.• "Randomly look in an array for a given value." You have no way of

knowing how many tries it will take to find the actual value.Note: this is a made-up example, because you would never randomly look into an array to find a value. You would always start at the front of the array and look one element at a time until you found the item or got to the end of the array.

Known number of times:• Compute the average grade of the class. While you (the programmer)

might not know how many grades exist in the class, the computer will know. Usually this is accomplished by using the "length" function on an array.

• Print the odd numbers from 1 to 1001.• Search a list (array) of numbers for the biggest grade. Again, the

computer "knows" how many grades there are, so a for loop is appropriate.

Page 5: Coding Design Tools Rachel Gauci. Task: Counting On Create a program that will print out a sequence of numbers from "1" to a "number entered”. Decision’s.

Design solution1. Create an IPO chart 

2. Create a Pseudocode

INPUT PROCESS OUTPUT

Number Using input (variable) loop over the input until the condition is satisfied (<=)

Loop “Count” input

BEGIN Declare input

FOR start variable = 1, variable <= input, Add 1

PRINT "Count:” input + 1 END

Page 6: Coding Design Tools Rachel Gauci. Task: Counting On Create a program that will print out a sequence of numbers from "1" to a "number entered”. Decision’s.

Code solution

#pragma strict

var x= 12;

function Start () {

for (var i = 1; i <= x; i++) { Debug.Log ("Count:" + i); }

}

function Update () {

}

Page 7: Coding Design Tools Rachel Gauci. Task: Counting On Create a program that will print out a sequence of numbers from "1" to a "number entered”. Decision’s.
Page 8: Coding Design Tools Rachel Gauci. Task: Counting On Create a program that will print out a sequence of numbers from "1" to a "number entered”. Decision’s.

How can we test the user’s input?

In Unity, when you create a variable in your script (outside the LOOP statement) you give it an initial value. When you look at your script in the inspector view, it will give

you the opportunity to edit your variable and press play to see the result.

Page 9: Coding Design Tools Rachel Gauci. Task: Counting On Create a program that will print out a sequence of numbers from "1" to a "number entered”. Decision’s.

If, Else if

if statement - executes some code only if a specified condition is true

if...else statement - executes some code if a condition is true and another

code if the condition is false

if...elseif....else statement - selects one of several blocks of code to be

executed

If (condition) { statement }

Else if (other condition) {

statement

}