Changes between Version 46 and Version 47 of ControlSystems/SoftwareTeam/IntroToPython
- Timestamp:
- Sep 4, 2017, 6:59:41 PM (8 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
ControlSystems/SoftwareTeam/IntroToPython
v46 v47 53 53 If you want to do more in a program and actually make decisions in your code, we need to introduce control structures. 54 54 55 == If-Then-Else: 55 == If-Then-Else: Decide, already! == 56 56 Take 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. 57 57 … … 109 109 (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... 110 110 111 == For Loop: 111 == For Loop: Stop Hitting Yourself! == 112 112 The For loop control structure runs code repeats over a variable. Try running this one: 113 113 … … 132 132 For 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. 133 133 134 == While Loop: 134 == While Loop: One more time around the block, Alfred! == 135 135 A 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. 136 136 … … 152 152 Oh 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? 153 153 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... == 155 155 Lastly, 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: 156 156