While and If-Else Loops - Socorro Independent School District

While and If-Else Loops - Socorro Independent School District

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 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: wait until its dark, then turn on light; wait until its light, then turn off light While loop: More flexible than an until statement In this code a motor runs until an object is within 50 cm. The program cant respond to an emergency shutoff switch.

The program cant control other outputs in response to other inputs. Program waits here until an object is near While loop: More flexible than an until statement A while loop can do the same thing as the until statement. Example code using until statement:

Program waits here until an object is near While loop can do the same thing: Program loops here until an object is near While loop: More flexible than an until statement Other conditions can be added to the while

condition, e.g., an emergency shutoff. Other code can be executed in the while loop. Can expand the condition Can control other outputs inside this bracket While loop: More flexible than an until statement Example equivalent to until:

Can expand the condition Can control other outputs inside this bracket Example using this flexibility: && means AND range from 0 to 10

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 Multiple if-else statements Be careful when using two separate if-else statements, particularly if both are used to control the same mechanism One branch of each if-else statement is always run so that you may create a scenario where the two statements fight

one another Multiple if-else statements In this example, if one of the touch sensors is pressed, the rightMotor will be turned on in one if-else statement and immediately turned off in the

other. Multiple if-else statements This can be corrected by embedding the second if-else within the else branch of the first if-else. The second condition is only checked if the

first condition is false. Nested if-else statements: Else if An else {if else} statement can also be represented as an else if - else Using a range of values in a condition Two strategies will work: Boolean logic Nested if-else statements

Example: Task: Control motor with potentiometer Potentiometer Value 0-500 501-1000 1001-4095 Motor Speed 0 63

127 Using a range of values in a condition Strategy #1: Boolean logic Boolean operator ROBOTC symbol AND

&& OR || Potentiometer Value Motor Speed

0-500 0 501-1000 63 1001-4095

127 True only if the sensor value is more than 500 AND less than 1000 . Using a range of values in a condition Strategy #1: Boolean logic In this example, this strategy wastes time and processor power. The next strategy is

better. Four comparisons waste time here each loop Using a range of values in a condition Potentiometer Motor Speed Strategy #2: Nested if-else Value 0-500 0

preferable in this example. 501-1000 63 In this case the false value 1001-4095 127 of the first condition can be used again by nesting a 2nd if statement inside the first else. References Carnegie Mellon Robotics Academy. (2011). ROBOTC. Retrieved from http://www.robotc.net

Recently Viewed Presentations

  • Comparative education research - CESON

    Comparative education research - CESON

    Mahesh Parajuli 2015. This presentation. Introduction on comparative perspective and comparative education research. Arguments: need for bringing localized perspective in comparative education/development studies for theorizing based on local processes
  • Continental Industrial Water Hose Training

    Continental Industrial Water Hose Training

    Green Hornet™ XF features a medium-duty, lightweight construction that retains flexibility in cold weather. A slightly corrugated outer helix promotes abrasion resistance, low coefficient of friction and ease of coupling. Common applications can be found in the waste management, construction...
  • Modeling Continuous Longitudinal Data Introduction to continuous longitudinal

    Modeling Continuous Longitudinal Data Introduction to continuous longitudinal

    Assumptions of the model ALL strategies assume normally distributed outcome and homogeneity of variances But all strategies are robust against this assumption, especially if data set is >30 **Univariate repeated-measures ANOVA assumes sphericity, or compound symmetry 3.
  • Thermodynamics and Statistical Mechanics

    Thermodynamics and Statistical Mechanics

    Thermodynamics and Statistical Mechanics First Law of Thermodynamics Review of van der Waals Critical Values van der Waals Results van der Waals Results Configuration Work đW = PdV Gas, Liquid, Solid: Kinds of Processes Work done by a gas Work...
  • 3.1.1.6  Energy systems Learning objectives To describe how

    3.1.1.6 Energy systems Learning objectives To describe how

    Oxygen debt is the amount of oxygen needed to return the body to a resting state. Oxygen debt results from EPOC. ... This first stage of EPOC recovery is also known as the alactacid component. The increased rate of respiration...
  • THE HARLEM RENAISSANCE - Weebly

    THE HARLEM RENAISSANCE - Weebly

    the harlem renaissance duke ellington louis armstrong fashion men wore suits with long suit jackets, baggy pants, with matching hats with a feather. they also wore pocket watches with a long chain, fashion women wore ball gowns with sparkling rhinestones,...
  • Law of Tort Tutorial Question 1 (Week 5)

    Law of Tort Tutorial Question 1 (Week 5)

    Law of Tort Tutorial Question 1 (Week 5) Poon Tak Sin, Judy 50313589 Mok Shun Wah, Arthur 50340360 Intentional Physical Harm Other than Trespass to the Person Intentional Physical Harm other than Trespass to the Person ESSENTIAL ELEMENTS: D: Intention...
  • On the Efficacy of GPU-Integrated MPI for Scientific Applications

    On the Efficacy of GPU-Integrated MPI for Scientific Applications

    Ashwin M. Aji ([email protected]) Accelerator-Based System Share (out of 500) With Accelerators Jun 2009 Nov 2009 Jun 2010 Nov 2010 Jun 2011 Nov 2011 Jun 2012 Nov 2012 5 7 9 17 19 39 58 62. It is important to...