Python

In this round, we had learned about python. Python is a high-level programming language, which used to code in a computer and others program. Throughout the class, we had learned about the condition of the code or the outline. The outline we learned this round are booleans, comparison, indentation, if statement, else statement, elif statement, and And Or, Not. Those outline we learned at the beginning of the round. Learning this program is not easy to learn, because it similar to learn another language. In order to get through those programming languages, we needed to put our effort and passion. After we understood about those outline, we continue to learn about different loops. There For loop and While loop that we learned. For loop is a loop to perform the action that follows the amount number of times. In For loop, there’s break statement, continue statement, and range function.

For loop example: ( break statement, and continue statement )

Break Statement example:

animals = [“bear”, “dog”, “deer”]

for x in animals:

   if x == dog:

       break

print x

So the answer, it will print only bear and dog, because break statement used to stop the loop. The code will read the animals one by one, and if the code read the animals in the list by a dog the code will stop running, because in the loop we set the program to stop when it reaches the dog. Which mean break statement is used to stop the loop.

 

Continue Statement example:

animals = [“bear”, “dog”, “deer”]

for x in animals:

   if x == “dog”:

       continue

   print x

So the answer, it will print the bear and the deer only, because continue statement used to skip something in the list. The code will read the animals one by one in the list, and if the code read the animals in the list by a dog the code will skip the dog, because in the loop we set the program to continue or skip the dog to others animals that are in the list. Which mean that continue mean to skip something in the loop.

 

So hopefully, this explanation might help you a little bite of understanding what is a break and continue statement in For loop are.

 

Let’s continue to While loop. We used While loop to perform the action until the condition is false. In While loop there’re infinite while loop, break statement, continue statement and else statement. Let’s  do some example of break and continue statement of While loop and see what the different, but While loop mostly people use it with a number.

 

While loop example ( break statement & continue statement )

Break Statement example:

i = 1

while i < 6:

print i

if i == 3:

break  

i += 1

 

So the answer, it will be 1, 2,3, because break statement used to stop the loop. In the loop we set that print i, and i is added up one until it reaches six, but we set that when i add up by one and it equals to three stop the loop. Which mean this break statement stop the code by at adding the number until the condition is false.

 

Continue statement example:

i = 0

while i < 6:

i += 1

if i == 3:

continue  

print i

So the answer for this exercise, it will print 1,2,4,5,6, because continue statement used to skip something the loop. In the loop we set that print i, and i is added up one until it reaches six, but we set that when it adds up by one and it reaches to 3, it skip number 3 and go to others number that is the loop. Which mean continue statement use to skip something in the loop, and it will stop the code when the condition is false.

 

So basically, you might understand something through this explanation and you might know the difference between break and continue statement in For loop, and break and continue statement in While loop.

 

Throughout the process, we are not just learning about those two loops. We are also doing some exercise with those two loops to understand more and get used to it.

 

It always feel impossible until it done! ( Nelson Mandela )

 

Leave a Reply

Your email address will not be published. Required fields are marked *