Iteration Flashcards
What does indenting code mean?
Creating a new line of code and pressing tab to move the code to the write so this bit of code is in a loop/iteration.
In a for loop what does the third number do?
The third number tells the program in what intervals it should count, it is known as the step.
If you typed ‘for i in range(3)’ what would be the output and what is missing from this code?
Nothing would be outputted but the variable i would count from 0 to 2, the reason the code would not work is because there is no colon at the end.
Can you have a loop in a loop?
Yes
What should you always do at the beginning of a piece of code?
Initialize the variable / give them a starting point.
What is sudocode?
It is a type of written python that is literal so if you wrote ‘for i from 1 to 5’ you wouldn’t need the brackets etc.
Can you do a for loop with strings so like in this piece of sudocode ‘for s in myStr’
Yes, check iteration workbook page 11 if this is confusing.
What does ‘end = ‘ ‘’ do?
It switches the end of a line, for example in an iteration to just print a space, as it normally moves down a line.
What two types of loop exist?
A for loop and a while loop.
What are some real world applications of a while loop?
Passwords, PINs or Validation/Adresses
What does += do?
It takes a varibale and then adds the number displayed after the += to the original value.
Does -= or x= work?
Yes
What does the exclamation mark do in this piece of code? ‘while name!= ‘x’:’
It means while x doesn’t equal name.
What are the three main parts of an iteration program as well as initialising all variables at the begionning?
Sequence, Selection and Iteration
What is a for loop?
A loop that will repeat a given number of times, specified in the loop.
What is a while loop?
A loop that will repeat indefinitely until criteria is met or something becomes true, then the program will move on to the next part.
When writing a code, when should you use a while loop and when should you use a for loop?
Use a while loop when you want something to change, like a variable value due to the loop and it will keep changing, if told to do so, until it meets the criteria. Use a for loop if you just want something to repeat a given number of times.
What is a nested iteration?
A loop inside of another loop, they can be different types of loop.
What is the main purpose of loops?
To save time and effort, making our programs more efficient.
Explain how a nested loop works.
Everytime the outer loop completes one full loop the inner loop has completed mulitiple, as many as stated in the program.
What is an example of using a nested loop to create a code?
I used a nested iteration to make a code that had to write multiple rows of code. I used a nested iteration as it allowed the inner loop to repeat the text on the row, whilst the outer loop mover rows.