# While Loop – printing oscillating series in Python

In Python, we can use the while loop to print an oscillating series. An oscillating series is a series of numbers that alternates between positive and negative values. One of the most commonly used oscillating series is 1 -1 1 -1 1 -1. In this tutorial, we will demonstrate how to print this oscillating series in Python using the while loop.

## Program to print oscillating series in Python using while loop without if-else

a = 1
b = 1

while a <= 10:
print(b, end=” “)
b *= -1
a += 1

### OUTPUT

1 -1 1 -1 1 -1 1 -1 1 -1

In this example, we initialize two variables, a and b. The counter variable( i.e. a) is used to count the number of iterations and stop the loop when it reaches 10. The b variable is used to print the numbers and alternate between 1 and -1.

We start by setting the counter variable to 1 and the b variable to 1. We use a while loop to execute the code block until the counter variable( i.e. a) is greater than 10. Inside the while loop, we print the b variable using the end parameter to separate the numbers with a space instead of a newline. We then multiply the value variable by -1 to alternate between 1 and -1. Finally, we increment the counter variable by 1 with each iteration.

## Program to print oscillating series in Python using while loop and if else statement

counter = 1
while counter <= 10:
if counter % 2 == 1:
print(“1″, end=” “)
else:
print(“-1″, end=” “)
counter += 1

### OUTPUT

1 -1 1 -1 1 -1 1 -1 1 -1

In this example, we start by setting the counter variable to 1. We use a while loop to execute the code block until the counter variable is greater than 10. Inside the while loop, we use an if-else statement to determine whether to print 1 or -1. If the counter variable is odd, we print 1. If the counter variable is even, we print -1. We use the end parameter to separate the numbers with a space instead of a newline.

## Conclusion

Printing an oscillating series in Python using the while loop is a simple and effective way to alternate between positive and negative numbers. By using a counter variable , we can easily control the output of the series. The while loop is a powerful programming construct in Python that can be used in a variety of applications, from simple output statements to complex data processing algorithms.