diff --git a/Python/Loops/whileNew.ipynb b/Python/Loops/whileNew.ipynb new file mode 100644 index 0000000..acf3c5b --- /dev/null +++ b/Python/Loops/whileNew.ipynb @@ -0,0 +1,57 @@ +/* + Q1. What is While Loop? + Python While Loop is used to execute a block of statements repeatedly until a given condition is satisfied +*/ + + +/* +Syntax of while Loop in Python + +while test_expression: + Body of while +*/ + +//Examples - 01 + +// Program to add natural +// numbers up to +// sum = 1+2+3+...+n + +// To take input from the user, +// n = int(input("Enter n: ")) + +n = 10 + +// 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 - 01 +/* +Enter n: 10 +The sum is 55 +*/ + +// Example - 02 +counter = 0 + +while counter < 3: + print("Inside loop") + counter = counter + 1 +else: + print("Inside else") + +//Output - 02 +/* +Inside loop +Inside loop +Inside loop +Inside else +*/