Saturday, May 30, 2020

Iterations – While Loop


Consider the given below program.



















Here we already assigned the value -999999999 to the variable largest_number. In this program, if we input a number that not equal to -1 then the program will check the entered number is greater than or less than to the given largest number.

If the entered number is greater than the given number, then the value of “largest_number” is updated to the entered number. This process will continue until we entered the -1.

If we entered the -1, then the program will be terminated.

Given below the snapshot of one of the tested output.

















Let’s consider the below program that wrote to count the number of even numbers & odd numbers that entered by user.


























Given below the snapshot of one of the tested output.
















Note:

Given below the two types of forms are equivalent.

While number ! = 0: and while number:

If number % 2 == 1 : and if number % 2 :



Using a counter variable to exit a loop







The output is given below.











A junior magician has picked a secret number. He has hidden it in a variable named “secret_number”. He wants everyone who run his program to play the Guess the secret number game, and guess what number he has picked for them. Those who don't guess the number will be stuck in an endless loop forever!

Given below the code for the above scenario.












One tested output is given below.


No comments:

Post a Comment