Description

  • continueous when it is true
  • break : breaks out of the current cloest enclosing loop
  • continue : goes to the top of the cloest enclosing loop
  • pass : Does nothing at all

Syntax of a while loop

while some_boolean_condition:
    do someting
else:
    do something different

Syntax(Pass)

x = [1,2,3]

for item in x:

print('hi')

result:
ERROR

__________________________
x = [1,2,3]

for item in x:
    pass

print('hi')

result:
hi

Syntax(Continue)

mystring = 'Sammy'

for letter in mystring:
    if letter == 'a':
        continue
    print(letter)

result:
S
m
m
y

Syntax(Break)

mystring = 'Sammy'

for letter in mystring:
    if letter == 'a':
        break
    print(letter)

result:
S

Syntax(Break)

x = 0

while x < 5:
    print(x)
    x += 1

result:
0
1
2
3
4

___________________________
 = 0

while x < 5:
    if x == 2:
        break
    print(x)
    x += 1

result:
0
1

일반 예시

x = 0

while x < 5:
    print(f'The current number of x is {x}')
    x += 1


result:
The current number of x is 0
The current number of x is 1
The current number of x is 2
The current number of x is 3
The current number of x is 4

_______________________________

x = 50

while x < 5:
    print(f'The current number of x is {x}')
    x += 1
    
else: 
    print('x is not less than 5')

result:
x is not less than 5

0개의 댓글

관련 채용 정보

Powered by GraphCDN, the GraphQL CDN