for, continue, break Explain the flow of iterative processing in Python3-Part 1

After I started programming, I managed to write simple for statements and while statements, but ...

** continue statement? break statement? How do you use it? ?? ** ** ** Double loop? ?? Triple loop? ?? And what happens to the processing order if you add an if statement? ?? ?? ** **

It is my study record that got stuck.

Part 1 is about single loops (for, if, continue, break statements). Finally, a little double loop comes out as a bonus. (I thought I'd write a double / triple loop, but it was too long, so I'll do it next time. Sweat)

All of the following programs are numbered on the left side to make it easier to understand the code reading flow. If you would like to check the operation of the program, please type it in by yourself. It's a hassle, but it's a good study. (I'm doing it while checking, but I'm sorry if I make a mistake)

Single loop

1.1 for statement

01 for i in range(4):
02     print(i)

Execution result

0
1
2
3

Let's start with a simple for statement. When you execute the above program, you can see that the process of 1 → 2 → 1 → 2 → 1 → 2 → 1 → 2 is repeated. (Hereafter → omitted) Processing order: 12121212

1.2 for + if statement

1 for i in range(4):
2     if i < 2:
3         print(i)

Execution result

0
1

It goes to 123123, but since ʻi = 2 and 2 becomes ʻi <2 == False, the process of 3 is not executed. The same is true for the next ʻi = 3`. Processing order: 1231231212

But what if there is an else statement here?

1.3 for + if + else statement

1 for i in range(4):
2     if i < 2:
3         print(i)
4     else:
5         print('a')

Execution result

0
1
a
a

It is the same until the middle, but it does not return to 1 at the place where the process of 3 was not executed earlier, and continues with 45. Processing order: 123123124451245

Next, put a continue statement instead of else

1.4 for + if + continue statement

1 for i in range(4):
2     if i < 2:
3         print(i)
4         continue
5     print('a')

Execution result

0
1
a
a

Although the program is different, the execution result is the same. (The person who wrote it was also surprised !?) However, the order of processing is different. When the continue statement is executed, the repetition below the continue statement is skipped. In the case of the above program, the print ('a') of 5 is not executed. Processing order: 12341234125125

By the way, what is the execution result if only the continue statement of the above program is deleted? This is a bonus. If you like, please think about it or run the program yourself.

1.4 Bonus: If you delete the continue statement ...

1 for i in range(4):
2     if i < 2:
3         print(i)
4     print('a')

Execution result

0
a
1
a
a
a

How was it, was it as expected? In 1.4, print ('a'), which was skipped by the continue statement, was always executed. As you can see, the continue statement is useful when you have a program that you want to leave unprocessed under certain conditions. Processing order: 12341234124124

1.5 for + if + break statement

1 for i in range(4):
2     if i < 2:
3         print(i)
4         break
5     print('a')

Execution result

0

Next is the break statement. I just changed the continue statement in 1.4 to a break statement The execution result has changed significantly compared to 1.4. If you proceed to 123 and execute the break statement of 4, the loop processing will be forcibly terminated. So it was supposed to loop 4 times, but it ended up in 1 time. Processing order: 1234

Finally, as a bonus, I will introduce what happens if there is a break statement in a double loop and finish it.

1.5 Bonus 1 Double loop (for + if statement)

1 for i in range(4):
2     if i < 2:
3         print(i)
4         for j in range(2):
5             print(j+10)

Execution result

0
10
11
1
10
11

First, check the basic double loop. 1 for i in range (4): Further inside the for statement of When there is a for statement of for j in range (2): of 4. The order of processing goes to 123 and becomes 4545, where it returns to 1.

In other words, after finishing all the inner loop processing, it returns to the outer loop processing.

And as before, if 2 becomes ʻi <2 == False`, 3 will not be executed. Processing order: 12345545123445512212

Next is the case where there is a break statement in the double loop of the main subject.

1.5 Bonus 2 Double loop (for + if + break statement)

1 for i in range(4):
2     if i < 2:
3         print(i)
4         for j in range(2):
5             print(j+10)
6             break

Execution result

0
10
1
10

I just added break to the end of the double loop statement. If the break statement is in the outer loop, the result is the same as 1.5, so I put it in the inner for statement.

As before, go to 12345 and execute the break statement at 6, then the break statement will be Forcibly terminates the inner loop processing.

Then go back to the outer loop and continue with 123456 to return to 1. And when 2 becomes ʻi <2 == False`, 3 is not executed. Processing order: 12345612345612112

Finally

Thank you for reading this article. I hope it helps you even a little. Since this was my first article, it may have been difficult to read in many places. In that case, please let me know in the comments. It will be very helpful as it will lead to the next reflection.

Actually, I was planning to write a while statement, **'It's not much different from the sentence, you don't have to write it' ** I do not think that. Sorry for lack of study sweat

** Please refer to this quote for proper use of'for statement and while statement! Don't study more! !! '** Tsukkomi is welcome. Thank you for your cooperation.

This article is based on the following sites.

http://www.yamamo10.jp/yamamoto/comp/Python/loop/index.php

Recommended Posts

for, continue, break Explain the flow of iterative processing in Python3-Part 1
View the result of geometry processing in Python
Image processing? The story of starting Python for
Check for the existence of BigQuery tables in Java
[For beginners] I want to explain the number of learning times in an easy-to-understand manner.
Check the operation of Python for .NET in each environment
Get the number of occurrences for each element in the list
The image display function of iTerm is convenient for image processing.
Google search for the last line of the file in Python
Explain the code of Tensorflow_in_ROS
[Python] Iterative processing (for, while)
Wrap (part of) the AtCoder Library in Cython for use in Python
Real-time display of server-side processing progress in the browser (implementation of progress bar)
Explain in detail the magical code for IQ Bot table items
A function that measures the processing time of a method in python
Dockerfile with the necessary libraries for natural language processing in python
Paper: Music processing in the brain
The story of the "hole" in the file
The meaning of ".object" in Django
Approach commentary for beginners to be in the top 1.5% (0.83732) of Kaggle Titanic_3
[Ota Ward] Check the status of application processing for special flat-rate benefits
Guidelines for reincarnating in the world of linux programming development (C / C ++ language)
I want to explain the abstract class (ABCmeta) of Python in detail.
Receive a list of the results of parallel processing in Python with starmap
Approach commentary for beginners to be in the top 1.5% (0.83732) of Kaggle Titanic_1
Get the key for the second layer migration of JSON data in python
Approach commentary for beginners to be in the top 1.5% (0.83732) of Kaggle Titanic_2