Question

Respond to the following in a minimum of 175 words: Most programming languages provide loop statements...

Respond to the following in a minimum of 175 words:

  • Most programming languages provide loop statements that help users iteratively process code. In Python you can write loops that handle many situations. What is the intuition behind using a loop statement? What do you gain from using loops in your code? Provide a code example to support your comments.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Dealing with redundant code and repetitive commands can be a nightmare for any programmer. Python makes use of loops, control and conditional statements to overcome this hurdle.

Loops in Python allow us to execute a group of statements several times. Lets take an example to understand why loops are used in python.

Suppose, you are a software developer and you are required to provide a software module for all the employees in your office. So, you must print the details of the payroll of each employee separately. Printing the details of all the employees will be a tiresome task, instead you can use the logic for calculating the details and keep on iterating the same logic statement. This will save your time and make your code efficient.

The illustration below is the flowchart for a loop:   

The execution starts and checks if the condition is True or False. A condition could be any logic that we want to test in our program. If its true it will execute the body of the loop and if its false, It will exit the loop.

It is used to eliminate the workload in terms of programming and for faster execution without consuming more time . For example if you need to print patten

*

**

***

It can possible to write single print out statements like

print(“*”);

printf(“**”);

like this continues . Its ok if it has maximun of 3 stars what of you need to display 1000 stars. You need to write 1000 print statements. So to overcome these difficulties looping statements are used.

The statement is looping iteratively until given condition is satisfied. The above example can be written as

for i in range(1000):

print statements

Consider the following example:

# Take user input
number = 2  

# Condition of the while loop
while number < 5 :  
    print("Thank you")
    # Increment the value of the variable "number by 1"
    number = number+1
Thank you
Thank you
Thank you

The code example above is a very simple while loop: if you think about it, the three components about which you read before are all present: the while keyword, followed by a condition that translates to either True or False (number < 5) and a block of code that you want to execute repeatedly:

print("Thank you")
number = number + 1

If you go into detail in the above code, you see that there is a variable number in which you store an integer 2. Since the value in number is smaller than 5, you print out "Thank you" and increase the value of number with one. While the value in number stays smaller than 5, you continue to execute the two lines of code that are contained within the while loop:

"Thank you"
"Thank you"

You print out "Thank you" two more times before the value of number is equal to 5 and the condition doesn't evaluate to True any more. Because the condition now evaluates to False, you will exit the while loop and continue your program if it contains any more code. In this case, there isn't any more code so your program will stop.

The above example is a bit basic, you can also include conditionals, or, in other words, an if condition, to make it even more customized. Take a look at the following example:

# Take user input
number = 2 

# Condition of the while loop
while number < 5 :  
    # Find the mod of 2
    if number%2 == 0:  
        print("The number "+str(number)+" is even")
    else:
        print("The number "+str(number)+" is odd")

    # Increment `number` by 1
    number = number+1
The number 2 is even
The number 3 is odd
The number 4 is even
Add a comment
Know the answer?
Add Answer to:
Respond to the following in a minimum of 175 words: Most programming languages provide loop statements...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT