Control Structure: While Loop

For iteration, Python provides a standard while statement and a very powerful for statement. The while statement repeats a body of code as long as a condition is true.


# Assignment No. 7
# Title: Print Hello World using While loop

i = 0

n = int(input("How many times do you want to print 'Hello World'? "))

while i < n:
    print("Hello world")
    i = i + 1


Output:

How many times do you want to print 'Hello World'?  5

Hello world
Hello world
Hello world
Hello world
Hello world

Process finished with exit code 0


Functions used:

1. print ()

We have used a print () function which prints whatever we type inside parentheses. Computer just ignores whatever we put inside the parentheses and prints the string in the console.

2. input ()

When the input() function is called, the program flow will be stopped until the user has given an input. The input() function accepts the user input.


Important Note:

Folks, read this statement again:

n = int ( input (“How many times do you want to print ‘Hello World’? “) )

By default, input() receives input as a string. But we will have to convert input into integer type.

Hence, we used int() function to convert the input string into int type which is later used in the while statement.


 

Leave a comment