A function can pass values, and will return a value:
Def function(somenum):
Return somenum*3
A subroutine is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code (called by name):
go sub
A procedure can receive a parameter or argument and does not have a return values (which would make it a function, essentially).
Def procedure(somenum):
print(somenum)
An iteration means the act of repeating a process with the aim of approaching a desired goal, target or result. Each repetition of the process is also called and "iteration", and the results of one iteraiton are used as the starting point for the next iteration.
Iteration is used to repeat sections of code:
number = 1
while number = <10
print("Iteration!")
number = number + 1
Iteration means less code == easier to read.
More efficient // powerful.
More manageable && maintable
More extensible with new features.
A for loop is a set of instructions that repeat until a criteria is met. It is:
- Count controlled
- Repeats a fixed number of times based on:
1. Limit
2. Increment
- Good for arrays or when number of repetitions is known in advance
While loops are condition controlled loops, so they work like IF statements. They repeat for as long as the condition is true, which can be a problem as an indefinite loop never fails.
There are two types of while loops:
While do: Condition first so may never run:
while 10 + 10 = 20
print("While do")
Do while: condition last so always runs at least once
print("do While")
while 10 + 10 = 20
ITERATION WORKSHEET
For loops
a) A for loop completes a set of instructions, repeating until a criteria is met. The number of repetitions is known in advance.
b) int i = 0; - Initialize (initializes the variable!)
i < 10; - Argument (continuation condition) - Tests if i meets certain condition (if < 10)
i++ - Adds on 1
While loops
a) for (int i = 0; i < 10; i++){
System.out.println(i)
}
---------------------------------------------
Corrected:
i = 0
While (i < 10)
print(i)
i++
b) Program a working solution that counts from 10 to 1:
for (int i = 10; i > 0; i--){
system.out.println(i)
}
c) What is the difference between a while loop and a for loop?
A for loop is count controlled and will repeat for a predetermined number of iteraitons.
WHILE is condition controlled and will repeat until a conditional statement is satisfied.
No comments:
Post a Comment