Page 77 - Touhpad Ai
P. 77
Program 18: To demonstrate the use of the for loop
numbers = [1, 2, 3, 4, 5]
for num in numbers:
multiplied = num * 2
print(multiplied)
Output:
2
4
6
8
10
The while Loop
The while statement executes a set of statements repeatedly, False
until the logical expression evaluates to True. When the condition Test Expression
becomes False, the control comes out of the loop.
The syntax of while loop is as follows: True
while (test expression):
Execute statements
statements
inside while block Exit loop
increment/decrement
Program 19: To demonstrate the use of the while loop
number = int(input("Enter a number to print its multiplication table: "))
counter = 1
print("Multiplication table of", number, ":")
while counter <= 10:
print(number, "×", counter, "=", number * counter)
counter += 1
Output:
Enter a number to print its multiplication table: 12
Multiplication table of 12:
12 × 1 = 12
12 × 2 = 24
12 × 3 = 36
12 × 4 = 48
12 × 5 = 60
12 × 6 = 72
12 × 7 = 84
12 × 8 = 96
12 × 9 = 108
12 × 10 = 120
Basic Concepts of Artificial Intelligence 75

