Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using variables, for..loop and functions to help organize your code

Similar presentations


Presentation on theme: "Using variables, for..loop and functions to help organize your code"— Presentation transcript:

1 Using variables, for..loop and functions to help organize your code
Using the Basketball Drills program

2 Basketball Drill Programming Alternatives
Using the Basketball Drills Activity to introduce: Variables For loop Functions

3 Variables in RobotC Rules for Variable Names
RobotC Variables int Store integer values (-35, 0 , 15 ) int distance = 100; float Store real values (3.82, 0.00, -27.2) float average = 3.82; string Stores words, strings of characters string greeting = “Hello World”; bool Stores true of false bool answer = true; Rules for Variable Names A variable name can not have spaces in it A variable name can not have symbols in it A variable name can not start with a number A variable name can not be the same as an existing reserved word A variable should describe what it is storing

4 Looking at Potential Solutions to Basketball Drills
Pseudo Code Go forward long enough to cross the first line Come back Go forward long enough to cross the second line Go forward lone enough to cross the third line It looks like the time to the second line is twice the time to the first line. And the time to the third line is three times the time to the first line. With enough guessing and checking, you can get the correct values for the wait1Msec().

5 Using a Variable to help with changes
Wherever timeToLine is in the program, RobotC uses the value that is currently stored in timeToLine. For this example that is 2400. It makes refining a program easier and it makes the program easier to read. If only there was a tool in RobotC that would let the code repeat. Note: 2*timeToLine will do the math first and use that value for the wait1Msec() command.

6 For loop in RobotC When to use it Syntax
When you want to repeat something a set number of times Syntax for(int line = 1; line<=3; line++) { //Code repeated } Declares an integer variable called line and gives it an initial value of 1 If the line variable is less than or equal to 3 when it reaches this, it will do the loop another time. After completing the loop, it will add 1 to the variable line. Note: There is no ‘;’ after the for loop line. If you put a ‘;’ there it will repeat nothing each time through the loop. In this example it will repeat the code inside the {} three times. Once when line = 1 Once when line = 2 And Once when line = 3

7 No loop vs. for loop Because it is defined outside of task main the variable timeToLine is a global variable and exists throughout the entire program. This variable line is a local variable and only exists inside the for loop. This will multiply line and timeToLine and send the result to the wait1Msec command.

8 Since line = 1 the first time through this loop line
Since line = 1 the first time through this loop line*timeToLine is the same as 1*2400 = 2400 the first time through this loop. Then 2*2400 = 4800 the second time and 3*2400 = 7200 the third time. RobotC does the math inside the () before executing the wait1Msec() command For loop example

9 What do you think this program does?
Define the Functions above the main body. Main Body Call statement. This will start the function running.

10 Dry Run: Reading the Program
timeToMove timeToMove line Main Body

11 Function Details //+++++ moveForward
Comments added to make the program easier to read. You can add details, … The function ‘Header’ void – It will not return a value moveForward – The name of this function. You get to pick the name of you function as long as: -Starts with a letter -No spaces or punctuation -Not a reserved Word And it should describe what it is doing. int timeToMove int – Sets an integer variable timeToMove – An integer variable that will store the value sent to the function in the call statement. The code for the function goes between {}. When the function is finished the program will return to the line after the call statement.

12 Review Variables For loop Functions
What types (int, float, bool, string) For loop When to use it Functions Header Code Call Statement

13 Online Time: Movement Challenges
Basketball Drills Sentry Simulation 1 Sumo Bot Labyrinth Challenge When you complete the activities, incorporate variables, loops, and functions


Download ppt "Using variables, for..loop and functions to help organize your code"

Similar presentations


Ads by Google