cre programming club - class 4 robert eckstein and robert heard

32
CRE Programming Club - Class 4 Robert Eckstein and Robert Heard

Upload: drusilla-mosley

Post on 18-Jan-2018

223 views

Category:

Documents


0 download

DESCRIPTION

Our Program Needs to Make Decisions Notice that this program contains the If, Then, and EndIf keywords. You use the If keyword to specify a condition that the computer evaluates. You use the Then keyword to specify what the computer should do if the condition is true.

TRANSCRIPT

Page 1: CRE Programming Club - Class 4 Robert Eckstein and Robert Heard

CRE Programming Club - Class 4

Robert Eckstein and Robert Heard

Page 2: CRE Programming Club - Class 4 Robert Eckstein and Robert Heard

Our Programs Need to Make Decisions

We need our programs to be able to make decisions. Let’s look at the following code:

If (Clock.Day = 1) And (Clock.Month = 1) Then TextWindow.WriteLine("Happy New Year")EndIf

What do you think this program does? 

Page 3: CRE Programming Club - Class 4 Robert Eckstein and Robert Heard

Our Program Needs to Make Decisions

Notice that this program contains the If, Then, and EndIf keywords. You use the If keyword to specify a condition that the computer evaluates. You use the Then keyword to specify what the computer should do if the condition is true.

Page 4: CRE Programming Club - Class 4 Robert Eckstein and Robert Heard

Our Program Needs to Make Decisions

If the condition is false, the computer skips what’s between Then and EndIf and proceeds to the next line of the program. You use the EndIf keyword to indicate end of the code that should be run if the condition was true.

Page 5: CRE Programming Club - Class 4 Robert Eckstein and Robert Heard

What’s a “Condition”?

Something that is “tested”, and the end result is either true or false. A condition is usually inside of parenthesis.

(a = 20)They can be separated with “And” or “Or” and grouped with parenthesis.

((a = 20) And (b = 15)) Or (c = 25)

Page 6: CRE Programming Club - Class 4 Robert Eckstein and Robert Heard

Decisions, Decisions...In this example, you use the If keyword specify the condition that today is the first day of the first month of the year (January).You use the Then keyword to specify that, if today is the first day of the first month, the computer should run the WriteLine operation. If today is not the first day of the first month, the computer should skip the block and proceed to the code after the EndIf.

Page 7: CRE Programming Club - Class 4 Robert Eckstein and Robert Heard

What If You Need to Do Something If It’s

False?If (Clock.Hour < 12) then TextWindow.WriteLine("Have your breakfast?")EndIfIf (Clock.Hour > 12) then TextWindow.WriteLine("Have your lunch?")EndIf

Page 8: CRE Programming Club - Class 4 Robert Eckstein and Robert Heard

If/Then/Else

Let’s reduce the repetition by using the Else keyword.If (Clock.Hour < 12) Then

TextWindow.WriteLine("Have your breakfast?")Else TextWindow.WriteLine("Have your lunch?")EndIf

Page 9: CRE Programming Club - Class 4 Robert Eckstein and Robert Heard

If/Then/Else

In this code, you specify that the computer should perform the first operation if the condition is true.But you’re also saying that the computer should perform the second operation if the condition is false.

Page 10: CRE Programming Club - Class 4 Robert Eckstein and Robert Heard

Try Importing This Program: JVJ515

TextWindow.Write("Enter a number: ")number = TextWindow.ReadNumber()remainder = Math.Remainder(number, 2)If (remainder = 0) Then TextWindow.WriteLine("The number is even.")Else TextWindow.WriteLine("The number is odd.")EndIf

Page 11: CRE Programming Club - Class 4 Robert Eckstein and Robert Heard

What Does It Do?In this program, you first ask the user for a number. Then you create a variable to store the number, and you use the ReadNumber() operation to read in the number.Next, you create a variable to store the remainder after you divide the user’s number by 2, and you use the Math.Remainder() operation to determine whether the remainder is 0.

Page 12: CRE Programming Club - Class 4 Robert Eckstein and Robert Heard

What Does It Do?Finally, you specify that the number is even if the remainder is 0 and that the number is odd if the remainder is not 0.When you write a program, you can specify as many conditions as you want by using the ElseIf keyword. You can also specify one or more operations for the computer to perform, depending on which condition is true when your program is run.

Page 13: CRE Programming Club - Class 4 Robert Eckstein and Robert Heard

If/Then/ElseIf/EndIf: Import KTH175

TextWindow.WriteLine("What’s the temp in degrees?")temp = TextWindow.Read()If (temp <= 5) Then TextWindow.WriteLine("It’s very cold today.")ElseIf (temp <= 45) Then TextWindow.WriteLine("It’s cool today.")ElseIf (temp <= 85) Then TextWindow.WriteLine("It’s warm today.")Else TextWindow.WriteLine("It’s quite hot today.")EndIf

Page 14: CRE Programming Club - Class 4 Robert Eckstein and Robert Heard

LoopsYou can use a loop to instruct the computer to run one or more statements more than once.You can use a For loop if you know exactly how many times you want the computer to repeat the instructions.You can use a While loop if you want the program to repeat the instructions until a condition is true.

Page 15: CRE Programming Club - Class 4 Robert Eckstein and Robert Heard

For LoopsFor a = 1 to 10

TextWindow.WriteLine(a)

EndFor

 In this example, the variable a contains a

value that goes up by 1 every time that the loop runs.

Page 16: CRE Programming Club - Class 4 Robert Eckstein and Robert Heard

For Loops - Import FQZ998

Let’s use this concept to print the multiplication table for the number 5. Try running this:

TextWindow.WriteLine("Multiplication Table")

n = 5

For a = 1 to 10

TextWindow.WriteLine(a + " x " + n + " = " + (a * n))

EndFor

Page 17: CRE Programming Club - Class 4 Robert Eckstein and Robert Heard

What Did This Program Do?

In this program, you first use the WriteLine() operation to display "Multiplication Table" on the screen.Then you create the variable n to store the value of 5. Then you create a For loop with the variable a to ensure the WriteLine() operation will run 10 times.

Page 18: CRE Programming Club - Class 4 Robert Eckstein and Robert Heard

String ConcatenationYou use the WriteLine() operation to display the following elements in this order:1.The value that is stored in the a variable2.The multiplication sign3.The value that is stored in the n variable4.The equals sign5.The products of the values of the a and n variables

Page 19: CRE Programming Club - Class 4 Robert Eckstein and Robert Heard

For Loops

In the previous example, the value of the counter variable in a For loop increases by 1 every time the loop runs. However, you can increase the value by another number if you use the Step keyword.

Page 20: CRE Programming Club - Class 4 Robert Eckstein and Robert Heard

For Loops with Steps

For example, you can increase the value of a by 2 during during each pass if you write the following code:For a = 1 to 10 Step 2

TextWindow.WriteLine(a + " x " + n + " = " + (a * n))

EndFor

Page 21: CRE Programming Club - Class 4 Robert Eckstein and Robert Heard

For Loops with Steps

You can even decrease the value of the loop variable every time that the code runs if you use the Step keyword, but specify a negative number.For example, you can write a program that counts backward from 10 to 1 on the screen if you assign the value of -1 to the Step keyword.

Page 22: CRE Programming Club - Class 4 Robert Eckstein and Robert Heard

While LoopsIf you don’t know the loop count before you write a program, you can create a While loop instead of a For loop.When you create a While loop, you specify a condition that is true when the loop starts. The computer evaluates the condition every time that the loop repeats. When the condition becomes false, the loop stops.

Page 23: CRE Programming Club - Class 4 Robert Eckstein and Robert Heard

While Loops - HTJ964Let’s write the following program to demonstrate the While loop:a = 10

While (a <= 100)

TextWindow.WriteLine(a)

a = a + 10

EndWhile

Page 24: CRE Programming Club - Class 4 Robert Eckstein and Robert Heard

What Did This Program Do?

In this example, you first create the a variable and set its value to 10.Next, you create a While loop with a condition that the value of the a variable is smaller than or equal to 100. Because you just set the value of that variable to 10, the condition is true when the loop starts.

Page 25: CRE Programming Club - Class 4 Robert Eckstein and Robert Heard

While LoopsYou use the WriteLine() operation to display the value of the a variable every time that the loop runs.In the next statement, you increase the value of the a variable by 10 every time that the loop runs.The loop stops after it runs 10 times because the value of the a variable becomes larger than 100.

Page 26: CRE Programming Club - Class 4 Robert Eckstein and Robert Heard

Goto - QHV742You can instruct the computer to jump around if you use the Goto statement. j = 1lineQ:TextWindow.WriteLine(j)j = j + 1If (j < 30) Then Goto lineQEndIf

Page 27: CRE Programming Club - Class 4 Robert Eckstein and Robert Heard

GotoIn this program, the lineQ: statement is called a label, which is similar to a bookmark. You can add as many labels as you want and name them whatever you want, as long as you don’t use the same name more than once.The Goto statement instructs the computer to run the statements after the lineQ: label again only if the condition in the If statement is true.

Page 28: CRE Programming Club - Class 4 Robert Eckstein and Robert Heard

What Does the Program Do?

In the first line of this program, you create a variable that is named j, and you set its value to 1.Then you create a label that is named lineQ: with a colon (:) at the end.In the next line, you tell the computer to display the value of the j variable on the screen. Then you increase the value of the j variable by 1.

Page 29: CRE Programming Club - Class 4 Robert Eckstein and Robert Heard

What Does the Program Do?

In the fourth line, you tell the computer to display the value of the j variable, increase its value by 1, and then determine whether that value is smaller than 10.If the value of the j variable is not smaller than 10, you tell the computer to continue to the next part of the program (or to stop running the program if no more code exists).

Page 30: CRE Programming Club - Class 4 Robert Eckstein and Robert Heard

Try This - HWV179start:

TextWindow.WriteLine("How many members are in your family?")

number = TextWindow.ReadNumber()

remainder = Math.Remainder(number, 2)

If (remainder = 0) Then

TextWindow.WriteLine("Your family has an even number of members.")

Else

TextWindow.WriteLine("Your family has an odd number of members.")

EndIf

Goto start

Page 31: CRE Programming Club - Class 4 Robert Eckstein and Robert Heard

Warning about Goto’sWarning: If you overuse Goto statements, your code will be difficult to understand and to maintain.Although these statements are useful sometimes, you should try to structure your programs so that you rarely use Goto statements. Too many Goto statements will make your program paths through the code look like spaghetti, so it is often called spaghetti code.

Page 32: CRE Programming Club - Class 4 Robert Eckstein and Robert Heard

Next Time...

We’re going to learn about subroutines and more about the graphics window.