For Loops

# For Loops - For Loops Examples For Loops T Mercker...

This preview shows pages 1–7. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: For Loops Examples For Loops T. Mercker Aerospace Engineering and Engineering Mechanics The University of Texas at Austin ASE 201: Introduction to Computer Programming March 6 / 9, 2009 T. Mercker For Loops For Loops Examples Outline For Loops Syntax Special Statements Examples T. Mercker For Loops For Loops Examples Syntax Special Statements For Loops I You used while loops to find a condition (such as W air > W tot as in the balloon problem). This was useful because you did not know when to stop beforehand I Suppose you knew exactly which values you wanted to use beforehand I For example, you wanted to do the balloon problem for h starting at 0 going to 10000 with an increment of 200, and you did not care about stopping I This situation is conducive to using for loops instead of while loops I For loops will be especially useful when working with arrays T. Mercker For Loops For Loops Examples Syntax Special Statements I Syntax: for ( loop_variable = array ) Code to run while loop is active end I array can be any array that you want I At beginning of loop, loop_variable is set to the first value of the array I When “end” is reached, the for loop increments to the next value of the array I This continues until the final value of the array is reached and the loop is automatically complete I Unlike while loops, you DO NOT want to change the loop_variable because the for loop is automatically doing it T. Mercker For Loops For Loops Examples Syntax Special Statements I Examples of for loop statements: for h = 0:200:10000 % Increment from 0 to 10000 by 200 for k = 1:10 % This is like indexing for cost = [10, 50, 75, 120, 200] % Does not have to have increment values = [-3, 20, 16, -50, 27]; for j = values I Flow Diagram: T. Mercker For Loops For Loops Examples Syntax Special Statements I There are 2 special statements that you can place in a for loop ONLY 1. break I If a “break” is encountered, the for loop is terminated regardless of the value of the loop_variable I break allows the for loop to act like a while loop by having an if...
View Full Document

{[ snackBarMessage ]}

### Page1 / 18

For Loops - For Loops Examples For Loops T Mercker...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online