Hello Viabyte, in this article we will discuss about while loop in Python programming language.
Looping is one of the fundamental concepts in programming. Looping is used to execute one or more commands repeatedly as long as a certain condition is met. In Python, there are several types of loops, one of which is the while loop.
The while loop is a type of loop in Python that is used to execute one or more commands repeatedly as long as a certain condition is met. The while loop will continue to run as long as the given condition is true.
The syntax of the while loop is as follows:
while condition: command
In the syntax above, ‘condition’ is the condition that must be met for the loop to run and ‘command’ is one or more commands that will be executed as long as the condition is met.
Example of Using While Loop
Here is an example of using while loop in Python:
x = 1 while x <= 5: print(x) x += 1
The output of the code above is:
1 2 3 4 5
In the example above, we use a while loop to execute the ‘print(x)’ command repeatedly as long as the value of ‘x’ is less than or equal to 5. Every time the ‘print(x)’ command is executed, the value of ‘x’ will be incremented by 1. The loop will continue to run as long as the value of ‘x’ is less than or equal to 5.
It is important to remember that if the condition given in the while loop never becomes false, the loop will continue to run forever and cause the program to become unresponsive. Therefore, make sure the given condition can be met and properly ends.
Using a While Loop with Break
A while loop can also be used with the ‘break’ statement to forcefully stop the loop. The ‘break’ statement is used to exit the loop and continue program execution to the next command outside the loop.
Here is an example of using a while loop with the ‘break’ statement in Python:
x = 1 while x <= 5: print(x) if x == 3: break x += 1
The output of the code above is:
1 2 3
In the example above, we use a while loop to repeatedly execute the command ‘print(x)’ as long as the value of ‘x’ is less than or equal to 5. However, if the value of ‘x’ is equal to 3, the ‘break’ command will be executed and the loop will be forcibly stopped. Therefore, the output of the code above is only up to the number 3.
Using While Loop with Continue
In addition to the ‘break’ command, while loops can also be used with the ‘continue’ command to continue the loop to the next iteration without executing the commands below it.
Here is an example of using a while loop with the ‘continue’ command in Python:
x = 1 while x <= 5: if x == 3: x += 1 continue print(x) x += 1
The output of the code above is:
1 2 4 5
In the example above, we use a while loop to repeatedly execute the command ‘print(x)’ as long as the value of ‘x’ is less than or equal to 5. However, if the value of ‘x’ is equal to 3, the ‘continue’ command will be executed and the loop will continue to the next iteration without executing the ‘print(x)’ command. Therefore, the output of the code above does not print the number 3.
Using While Loop with Else
In addition, while loops can also be used with the ‘else’ command to execute one or more commands when the condition of the while loop is not met or evaluates to false.
Here is an example of using a while loop with the ‘else’ command in Python:
x = 1 while x <= 5: print(x) x += 1 else: print("Perulangan while selesai")
The output of the code above is:
1 2 3 4 5 while loop is finished
In the example above, the while loop will continue to run as long as the value of ‘x’ is less than or equal to 5 and print the value of ‘x’ on each iteration. After the loop is finished, the command inside the ‘else’ block will be executed and print the message “While loop finished”.
Problem :
1. Write a program that prints the numbers from 1 to 10 using a while loop.
2. Write a program that takes a number as input and prints all the even numbers from 0 to that number using a while loop.
3. Write a program that takes a number as input and prints its multiplication table using a while loop.
4. Write a program that takes a string as input and counts the number of vowels in the string using a while loop.
5. Write a program that takes a number as input and checks if it is a prime number using a while loop.
6. Write a program that takes two numbers as input and finds their greatest common divisor (GCD) using a while loop.
7. Write a program that takes a string as input and checks if it is a palindrome using a while loop.
8. Write a program that takes a string as input and checks if it is a pangram using a while loop. (A pangram is a sentence containing every letter of the alphabet at least once.)
9. Write a program that takes a list of numbers as input and prints the smallest number in the list that is greater than 0 and not in the list using a while loop.
10. Write a program that takes a number as input and checks if it is an Armstrong number using a while loop.
Solution :
1.
num = 1 while num <= 10: print(num) num += 1
2.
num = int(input("Enter a number: ")) i = 0 while i <= num: if i % 2 == 0: print(i) i += 1
3.
num = int(input("Enter a number: ")) i = 1 while i <= 10: print(num, "x", i, "=", num*i) i += 1
4.
string = input("Enter a string: ") vowels = "aeiou" count = 0 i = 0 while i < len(string): if string[i] in vowels: count += 1 i += 1 print("Number of vowels in the string:", count)
5.
num = int(input("Enter a number: ")) i = 2 while i < num: if num % i == 0: print(num, "is not a prime number") break i += 1 else: print(num, "is a prime number")
6.
num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if num1 > num2: smaller = num2 else: smaller = num1 i = 1 while i <= smaller: if num1 % i == 0 and num2 % i == 0: gcd = i i += 1 print("GCD of", num1, "and", num2, "is:", gcd)
7.
string = input("Enter a string: ") i = 0 j = len(string) - 1 while i < j: if string[i] != string[j]: print(string, "is not a palindrome") break i += 1 j -= 1 else: print(string, "is a palindrome")
8.
import string string = input("Enter a sentence: ") alphabet = set(string.ascii_lowercase) i = 0 while i < len(string): if string[i].lower() in alphabet: alphabet.discard(string[i].lower()) if len(alphabet) == 0: print("The sentence is a pangram") break i += 1 else: print("The sentence is not a pangram")
9.
num_list = [int(i) for i in input("Enter a list of numbers separated by space: ").split()] i = 1 while True: if i not in num_list and i > 0: print("The smallest number not in the list is:", i) break i += 1
10.
num = int(input("Enter a number: ")) temp = num sum = 0 while temp > 0: digit = temp % 10 sum += digit ** 3 temp //= 10 if num == sum: print(num, "is an Armstrong number") else: print(num, "is not an Armstrong number")
Conclusion
The while loop is one type of loop in Python that is used to execute one or more commands repeatedly as long as a certain condition is met. The while loop will continue to run as long as the given condition is true. It is important to note that if the condition given in the while loop never becomes false, the loop will continue to run indefinitely and cause the program to become unresponsive.
The while loop can also be used with the ‘break’ command to stop the loop prematurely, the ‘continue’ command to continue the loop to the next iteration without executing the command below it, and the ‘else’ command to execute one or more commands when the condition in the while loop is not met or becomes false.
In general, the use of the while loop is recommended for conditions that are not known in advance or do not have a definite number of iterations. However, if the number of iterations is already known in advance, the for loop may be a better choice.
That is the discussion about the while loop in Python. Hopefully, this article can help readers understand the basic concept of while loops and how to use them in Python. Thank you for reading and see you again in another interesting article!