Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions Python/Loops/whileNew.ipynb
Original file line number Diff line number Diff line change
@@ -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
*/