Changes between Version 46 and Version 47 of ControlSystems/SoftwareTeam/IntroToPython


Ignore:
Timestamp:
Sep 4, 2017, 6:59:41 PM (8 years ago)
Author:
cdelgigante
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ControlSystems/SoftwareTeam/IntroToPython

    v46 v47  
    5353If you want to do more in a program and actually make decisions in your code, we need to introduce control structures.
    5454
    55 == If-Then-Else:  Decide, already! ==
     55== If-Then-Else:  Decide, already! ==
    5656Take the program below, it uses an If-Then-Else control structure to ask the program to make a decision, and run code based on that decision.  You'll use these all the time.
    5757
     
    109109(Also note the text in the code that starts with a # sign.  The pound sign and everything after it on that line is called a ''comment'', and its a way to put some notes in the code that the program doesn't run.  The robotics team wants you to put comments in your code a lot--it makes the code easier to read and understand.   And we like it when we can understand you.)   Now for a couple of other control structures that you should know...
    110110
    111 == For Loop:  Stop Hitting Yourself! ==
     111== For Loop:  Stop Hitting Yourself! ==
    112112The For loop control structure runs code repeats over a variable.  Try running this one:
    113113
     
    132132For loops are handy if you want to avoid repeating yourself in code, and do the same thing over and over again.  The less code you write, the less chance you can make a mistake.  Not to mention its easier to read.
    133133
    134 == While Loop:  One more time around the block, Alfred! ==
     134== While Loop:  One more time around the block, Alfred! ==
    135135A While loop is the more charming cousin of the FOR loop, this one repeats as long as its condition is ''True''.  If the condition is false when it gets to the While loop, the While loop wont run at all.
    136136
     
    152152Oh no, I'm tired, so I'm not going to tell you the answer for this one. You should be seeing a pattern by now.  You tell me---what should it print?    Count starts out at 0, and as long as count is less than 9 it will run the code in the block, then it will print "Good bye!".   What else does it print out?
    153153
    154 == Here a Control Structure, There a Control Structure, Everywhere a Control Structure... ==
     154== Compound Control Structures:  Here a Control Structure, There a Control Structure, Everywhere a Control Structure... ==
    155155Lastly, you can combine all three of these in any sort of combination, and nest them.  This is how you do real work in a program.   For example, try running this one:
    156156