Activity 3.1.4 While and If-Else Loops

Activity 3.1.4 While and If-Else Loops

While Loops and If-Else Structures ROBOTC Software Principles of Engineering 2012 Project Lead The Way, Inc.

While loops While loop is a structure within ROBOTC Allows a section of code to be repeated as long as a certain condition remains true Three main parts to every while loop 1. The word while 2. The condition

3. Commands to be repeated 1. The word while Every while loop begins with the keyword while 2. The condition Condition controls how long or how many

times a while loop repeats When condition is true, the while loop repeats When condition is false, the while loop ends and the remainder of the program executes Condition is checked once every time the loop repeats just before the commands between curly braces run

3. Commands to be repeated Commands between curly braces will repeat while condition is true Program checks at the beginning of each pass through the loop Programming - Boolean logic

Program decisions are always based on questions Only two possible answers yes or no true or false Statements that can be only true or false are called Boolean statements

Their true-or-false value is called a truth value. Boolean logic Boolean logic Writing a condition: Example

While the bump switch is not pressed: right motor is on at power value 63 AND left motor is on at same value. Timers Loop control Where would the wait statement go if we wanted the loop to repeat for a controlled

amount of time? Nowhere! We need something else. Solution: Timers Internal stopwatches (4 available) Like encoders, timers should be cleared before they are used. Be careful: Dont clear a timer in a timed loop.

Timers Timer T1 is used as the condition for the while loop, which will run for 30 seconds. If statements If statement in the program is evaluated by condition contained in parentheses

If condition is true, commands between braces are run If condition is false, those commands are ignored Very similar to how a while loop works, but does not repeat the code

If-else statements If-else statement is an expansion of if statement If checks condition and runs appropriate commands when it evaluates to true Else allows code to run when condition is false Either if or else branch is always run once

References Carnegie Mellon Robotics Academy. (2011). ROBOTC. Retrieved from http://www.robotc.net

Recently Viewed Presentations

  • Speed, Velocity, and Acceleration - Currituck County Schools

    Speed, Velocity, and Acceleration - Currituck County Schools

    To describe motion accurately and completely, a frame of reference is needed. An object is in motion if it changes position relative to a reference point. Objects that we call stationary—such as a tree, a sign, or a building—make good...
  • CHAPTER 5 Small Business and the Entrepreneur

    CHAPTER 5 Small Business and the Entrepreneur

    They are widely distributed through convenience stores or local grocery stores. Purchasing decisions are based on the convenience of location and brand-name image. Teaching Tips Ask students what convenience items they have with them. They are likely to have coffee,...
  • CIRCUITS AND CURRENT ELECTRICITY - SMU Physics

    CIRCUITS AND CURRENT ELECTRICITY - SMU Physics

    What is a "slide?" Example ELECTRIC CIRUITS VS. PLAYGROUND CIRCUITS Batteries What is a battery? Potential difference Resistors A Battery and a resistor Total energy/potential change(s) "Big" batteries and small resistors (and vice versa) Ohm's law & Ohmic devices Resistors...
  • Diapositive 1 - INSPQ

    Diapositive 1 - INSPQ

    Le reste du recrutement est fait dans les centres de réadaptation, les prisons, les SIDEP, etc. Pour être éligible à l'étude, la personne doit s'être injecté des drogues dans les 6 mois précédents, être âgée de 14 ans ou plus,...
  • Chapter 1: Introduction to Chemistry

    Chapter 1: Introduction to Chemistry

    The responding variable (dependent variable) is the variable that you observe during an experiment. A good experiment only has one manipulated variable. Theory vs. Law. A theory is a well-tested explanation for a broad set of observations.
  • Chapter 14

    Chapter 14

    the point of support, or axis, about which a lever may be made to rotate. First class lever: lever positioned with the applied force and the resistance on opposite sides of the axis of rotation. Second class lever: lever positioned...
  • Drugs/What you should know

    Drugs/What you should know

    Drug use is the term for taking a drug correctly, following instructions for dose, time, etc.. Drug misuse: is using a drug, such as a prescription drug for its unintended purpose.
  • OLD DOMINION UNIVERSITY - odu.edu

    OLD DOMINION UNIVERSITY - odu.edu

    Old Dominion University The Department of Human Resources Presents BENEFITS 101 Old Dominion University Department of Human Resources * * * Safety Belt Benefit: If you die, or suffer dismemberment in an accident life insurance benefit will pay an additional...