while test_expression: block # Program to add numbers upto n where n is provided by the user # sum = 1+2+3+...+n # take input from the user n = int(input("Enter n: ")) # initialize sum and counter sum = 0 i = 1 while i <= n: sum = sum + i i = i+1 # update counter # print the sum print("The sum is",sum) Output # Example to illustrate the use of else statement with the while loop counter = 0 while counter < 3: print("Inside loop") counter = counter + 1 else: print("Inside else")