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

Slides:



Advertisements
Similar presentations
Variables and Functions ROBOTC Software. Variables A variable is a space in your robots memory where data can be stored, including whole numbers, decimal.
Advertisements

08/2012Tanya Mishra1 EASYC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817.
Procedural programming in Java
Programming TBE 540 Farah Fisher. Objectives After viewing this presentation, the learner will be able to… Given a task, create pseudocode Given pseudocode,
Intro Programming By Trevor Decker Team 1743 By Trevor Decker Team 1743.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
How Create a C++ Program. #include using namespace std; void main() { cout
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Copyright © Texas Education Agency, Computer Programming For Loops.
Programming – Touch Sensors Intro to Robotics. The Limit Switch When designing robotic arms there is always the chance the arm will move too far up or.
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
ROBOTC Software Introduction. ROBOTC Software ROBOTC developed specifically for classrooms and competitions Complete programming solution for VEX Cortex.
CIS Computer Programming Logic
Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
CPS120: Introduction to Computer Science Lecture 14 Functions.
CONTENTS Processing structures and commands Control structures – Sequence Sequence – Selection Selection – Iteration Iteration Naming conventions – File.
Review, Pseudocode, Flow Charting, and Storyboarding.
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Identifiers Identifiers in Java are composed of a series of letters and digits where the first character must be a letter. –Identifiers should help to.
Repetition. Loops Allows the same set of instructions to be used over and over again Starts with the keyword loop and ends with end loop. This will create.
Getting Started in RobotC // Comment task main() motor[] {} wait1Msec() ; = Header Code Compile Download Run Take out your notes.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Structured Programming (4 Credits) HNDIT Week 2 – Learning Outcomes Design an algorithmic solution for simple problem such as computation of a factorial,
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
Variables and Functions ROBOTC Software © 2012 Project Lead The Way, Inc.Principles of Engineering.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
RobotC Remote Control. Learning Objectives: Focusing on Virtual World with Physical Examples Understand Real-Time Joystick Mapping Understand how to use.
Variables and Functions ROBOTC Software. Variables A variable is a space in your robots memory where data can be stored, including whole numbers, decimal.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
Arrays and Loops. Learning Objectives By the end of this lecture, you should be able to: – Understand what a loop is – Appreciate the need for loops and.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
Loops. About the Midterm Exam.. Exam on March 12 Monday (tentatively) Review on March 5.
Variables. A variable is a space in your robot’s memory where you can store data, such as whole numbers, decimal numbers, and words. Variable names follow.
Identify the Appropriate Method for Handling Repetition
Bill Tucker Austin Community College COSC 1315
Introduction to Programming in RobotC
Val Manes Department of Math & Computer Science
Introduction To Repetition The for loop
Robotics Programming Using Shaft Encoders
Programming Mehdi Bukhari.
Objectives Identify the built-in data types in C++
Variables A piece of memory set aside to store data
Movement using Shaft Encoders
Using Encoders to go Straight
Programming – Touch Sensors
Getting Started in RobotC
For Loops October 12, 2017.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
LRobot Game.
Basketball Drill Programming Alternatives
Variables ICS2O.
Getting Started in RobotC
Auto Straightening using VEX Shaft Encoders
Loops.
Robotics Programming Using Shaft Encoders
Robotics Programming Using Shaft Encoders
2. Second Step for Learning C++ Programming • Data Type • Char • Float
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
Single-Result Functions & Modularity
Robotics Programming Using Shaft Encoders
CPS125.
Is there an alternative to copy-paste for repeating code?
Presentation transcript:

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

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

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

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

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.

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

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.

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

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

Dry Run: Reading the Program timeToMove timeToMove line Main Body

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.

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

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