“For i in range” is a type of loop statement in Python that allows you to iterate over a sequence of numbers. Here is an example of pseudocode for a “for i in range” loop:
1 2 3 4 |
for i in range(start, end, step): # code to be executed |
In this example, “start” is the starting value of the loop, “end” is the ending value of the loop, and “step” is the increment value (default is 1). The loop will iterate from the “start” value to the “end” value, incrementing by the “step” value each time. For example, if start is 0, end is 5, and step is 1, the loop will iterate over the values 0, 1, 2, 3, 4, 5.
Here are some examples of “for i in range” loop in pseudocode:
Print the numbers 0 to 9:
1 2 3 4 |
for i in range(0, 10): print i |
Print the even numbers from 0 to 10:
1 2 3 4 |
for i in range(0, 11, 2): print i |
Sum the numbers from 1 to 100:
1 2 3 4 5 |
sum = 0 for i in range(1, 101): sum = sum + i |
Multiply all the elements in an array:
1 2 3 4 5 6 |
array = [1, 2, 3, 4, 5] result = 1 for i in range(0, len(array)): result = result * array[i] |
Print the characters in a string:
1 2 3 4 5 |
string = "Hello World" for i in range(0, len(string)): print string[i] |
Iterate through a dictionary and print its keys and values:
1 2 3 4 5 |
dict = {'a':1, 'b':2, 'c':3} for i in range(0, len(dict)): print dict.keys()[i] + ": " + dict.values()[i] |
Note that these examples are in pseudocode and may not run correctly in a real programming language. The syntax may vary depending on the programming language you are using.
Here’s an example of a “for i in range” loop in Python:
Here’s an example of a “for i in range” loop in Python:
1 2 3 4 5 6 |
# Print the numbers 0 to 9 for i in range(10): print(i) |
In this example, the loop starts at 0 (the default start value) and ends at 9 (the default end value is exclusive), incrementing by 1 (the default step value) each time. The output will be:
1 2 3 4 5 6 7 8 9 10 11 12 |
1 2 3 4 5 6 7 8 9 |
Another example :
1 2 3 4 5 |
# Print the even numbers from 0 to 10 for i in range(0,11,2): print(i) |
In this example, the loop starts at 0, ends at 11 and step is 2. The output will be:
1 2 3 4 5 6 7 8 9 |
0 2 4 6 8 10 |
You can also use variable to define your range:
1 2 3 4 5 6 |
start = 2 end = 7 for i in range(start, end): print(i) |
In this example, the loop starts at 2, ends at 7 and step is 1. The output will be:
1 2 3 4 5 6 7 |
2 3 4 5 6 |