Iteration, an essential programming concept, involves repeating a set of instructions until a certain condition is fulfilled. It's utilized to automate repetitive tasks.
There are three main types of iteration:
Example in Pseudocode:
count ← 1
FOR i <= 10
OUTPUT count
NEXT i
Example in Python:
for count in range(1, 11):
print(count)
Example in Java:
for(int count=1; count<=10; count++){
System.out.println(count);
}
Example in Visual Basic:
For count = 1 To 10
Console.WriteLine(count)
Next count
Example in Pseudocode:
INPUT temperature
WHILE temperature > 37 DO
OUTPUT "Patient has a fever"
INPUT temperature
END WHILE
Example in Python:
temperature = float(input("Enter temperature: "))
while temperature > 37:
print("Patient has a fever")
temperature = float(input("Enter temperature: "))
Example in Java:
Scanner input = new Scanner(System.in);
float temperature = input.nextFloat();
while (temperature > 37) {
System.out.println("Patient has a fever");
temperature = input.nextFloat();
}
Example in Visual Basic:
temperature = InputBox("Enter temperature")
Do While temperature > 37
Console.WriteLine( "Patient has a fever")
Console.WriteLine("Enter temperature")
temperature=Console.ReadLine()
Loop
Example in Pseudocode:
REPEAT
INPUT guess
UNTIL guess = 42
Example in Python:
Postcondition loops don’t exist in Python and would need to be restructured to a precondition loop
Example in Java:
Scanner input = new Scanner(System.in);
int guess = 0;
do {
System.out.print("Enter guess: ");
guess = input.nextInt();
} while (guess != 42);
Example in Visual Basic:
Do
Console.WriteLine("Enter guess")
guess = Console.ReadLine()
Loop Until guess = 42
92 docs|30 tests
|
1. What is iteration in programming? |
2. How does iteration help in simplifying code? |
3. What are the common types of iteration structures used in programming? |
4. Can iteration be used to iterate over data structures like arrays or lists? |
5. How does iteration contribute to the efficiency of a program? |
|
Explore Courses for Year 11 exam
|