134k views
0 votes
Write a loop that reads positive integers from standard input and that terminates when it reads an integer that is not positive. After the loop terminates

User Yzzlr
by
5.9k points

2 Answers

0 votes

Final answer:

To write a loop that reads positive integers from input and terminates when a non-positive integer is entered, use a while loop.

Step-by-step explanation:

To write a loop that reads positive integers from standard input and terminates when it reads an integer that is not positive, you can use a while loop in most programming languages. Here's an example in Python:

num = int(input('Enter a positive integer: '))
while num > 0:
num = int(input('Enter a positive integer: '))

In this code, the loop continues as long as the input number is positive. Once a non-positive number is entered, the loop terminates. This allows you to keep reading positive integers until the condition is no longer met.

User Kevy
by
5.6k points
6 votes

Answer:

while(x>=0)

{

loop execution that manipulates x

}

Step-by-step explanation:

A while loop keep iterating as long as the condition inside is true. The necessary element to remember is to make your the condition variable is altered from within the loop. Otherwise, the loop will become an infinite loop.

The condition state that as long as the variable x is greater than or equal to 0, the loop will continue. If x becomes negative, the loop will terminate.

User NumenorForLife
by
5.1k points