In the last session, we discussed loop statements and various methods. Now, we will learn the importance of the while statement.

Let’s start,

## WHILE STATEMENT

- We can run the statement when the condition is true, false means exit.
- While statement plays a very important role in projects.

**Syntax**

**Flow chart**

**A simple example of a while statement**

```
Input:
a=1
while a<=6:
a=a+1
print(a)
Output:
2
3
4
5
6
7
```

**Printing the given values mentioned times**

```
Input:
count=0
while count<5:
print(2,end=' ')
count=count+1
Output:
22222
```

**Numbers in descending order consequently**

```
Input:
last=int(input("Enter the last number:"))
while last>0:
print(last)
last=last-1
Output:
Enter the last number:5
5
4
3
2
1
```

**Ascending order with 2 jumps ( odd )**

```
Input:
first=1
last=int(input("Enter the last number:"))
while first<=last:
print(first)
first=first+2
Output:
Enter the last number:5
1
3
5
```

**Ascending order with 2 jumps ( even )**

```
Input:
first=2
last=int(input("Enter the last number:"))
while first<=last:
print(first)
first=first+2
Output:
Enter the last number:9
2
4
6
8
```

**Multiples of 3 using while loop**

```
Input:
first=0
last=20
while first<=last:
first=first+3
print(first)
Output:
3
6
9
12
15
18
21
```

**Multiples of 3 with 2 jumps**

```
Input:
first=1
last=50
while first<=last:
if first%3==0:
print(first)
first=first+2
Output:
3
9
15
21
27
33
39
45
```

**Printing ( i x 3 ) values using loop**

```
Input:
first=3
while first<=100:
print(first)
first=first*3
Output:
3
9
27
81
```

**Number divisible by 2 and 3 using while and nested if**

```
Input:
number=1
while number<=70:
if number%2==0:
if number%3==0:
print(number)
number=number+1
Output:
6
12
18
24
30
36
42
48
54
60
66
```

**Number divisible by 2 and 3 using while, if, and logical operator**

```
Input:
number=1
while number<=80:
if (number%2==0 and number%3==0):
print(number)
number=number+1
Output:
6
12
18
24
30
36
42
48
```

**Sum of first n consecutive numbers**

```
Input:
number=1
last=100
total=0
while number<=100:
total=total+number
number=number+1
print(total)
Output:
5050
```

**While else using loop condition**

```
Input:
name='mark zuckerberg'
person=''
while person!=name:
person=input("Please tell me your name:")
else:
print("I got your name")
Output:
Please tell me your name:larry page
Please tell me your name:zuckerberg
Please tell me your name:mark
Please tell me your name:mark zucker
Please tell me your name:mark zuckerberG
Please tell me your name:Mark ZuckeRbeRg
Please tell me your name:mark zuckerberg
I got your name
```

**Printing a word using index**

```
Input:
language ='Python'
i=0
while i<len(language):
print(language[i])
i=i+1
Output:
P
y
t
h
o
n
```

**Game using a while loop statement**

```
Input:
mind = 7
match_not_found = True
while match_not_found == True:
guess = int(input("\nGuess my memory number between 1 & 20 : "))
if guess == mind:
print("Your guess is correct")
match_not_found = False
elif guess > mind:
print("Your guess is Higher")
else:
print("Your guess is Lower")
Output:
Guess my memory number between 1 & 20 : 15
Your guess is Higher
Guess my memory number between 1 & 20 : 10
Your guess is Higher
Guess my memory number between 1 & 20 : 5
Your guess is Lower
Guess my memory number between 1 & 20 : 6
Your guess is Lower
Guess my memory number between 1 & 20 : 9
Your guess is Higher
Guess my memory number between 1 & 20 : 7
Your guess is correct
```

I hope now you can understand about While Loop. If you have any query please ask me. We'll see a more interesting topic in the future.