Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CRE Programming Club - Class 4 Robert Eckstein and Robert Heard."— Presentation transcript:

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

2 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?

3 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.

4 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.

5 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)

6 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.

7 What If You Need to Do Something If It’s False? If (Clock.Hour < 12) then TextWindow.WriteLine("Have your breakfast?") EndIf If (Clock.Hour > 12) then TextWindow.WriteLine("Have your lunch?") EndIf

8 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

9 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.

10 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

11 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.

12 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.

13 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

14 Loops You 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.

15 For Loops For 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.

16 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

17 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.

18 String Concatenation You use the WriteLine() operation to display the following elements in this order: 1. The value that is stored in the a variable 2. The multiplication sign 3. The value that is stored in the n variable 4. The equals sign 5. The products of the values of the a and n variables

19 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.

20 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

21 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.

22 While Loops If 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.

23 While Loops - HTJ964 Let’s write the following program to demonstrate the While loop: a = 10 While (a <= 100) TextWindow.WriteLine(a) a = a + 10 EndWhile

24 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.

25 While Loops You 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.

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

27 Goto In 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.

28 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.

29 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).

30 Try This - HWV179 start: 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

31 Warning about Goto’s Warning: 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.

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


Download ppt "CRE Programming Club - Class 4 Robert Eckstein and Robert Heard."

Similar presentations


Ads by Google