Basic programming course: Lesson #5 Control structures. Part 2. Cycles.
Assalam-o-Alaikum!!!
Greetings to my all STEEMIT members. hopefully , you all are great end enjoying your best life on STEEMIT. I am also fine ALHAMDULILLAH. And today I am going to participate in this engagement contest. And I will try my best to do this task very well done
Cycles in programming, also known as loops, are used to run a block of code over and over again, automating repetitive tasks and reducing duplicate records. It helps to write out the same code less and less to work over and over again
There are three main types of Loops:
1.For Loop
2.While Loop
3.Do While Loop
For Loop:
A block of code that repeats a specified number of iterations . In this if there is increment in this , then it will continue until exceeds
Example:
So the Output is:
2.While Loop:
Repeats a block of code until a given condition is true. To see if it is true. If it is true, it starts executing the code inside the loop. If the incrementing becomes greater than five, it wraps
- Example:
So the Output is:
3.Do While Loop:
like while loop, Do While Loop guarantees that the block of code runs at least once. It works in a way that it executes the code of blow and then the condition is also checked, if the condition is good then it is executed again. This process continues until any condition is false.
Example:
So the Output is:
key Difference between While & Do While Loop
While Loop | Do While Loop |
---|---|
The condition is checked before the code block is executed The code inside the loop can never be executed if the condition is initially false Used when you want to check the first condition are and only run the loop if it is true | The condition is checked after the code block is executed.The code inside the loop is guaranteed to be executed at least once.This is used when you want to execute the code block at least once. regardless of its condition |
Switch-Case Structure:
A switch case structure is a control flow statement that allows a variable to be tested against a list of values called cases. There is a compact alternative
The basic idea is that you switch to a value and depending on that value, different blocks of code will be executed. The switch statement is used in many programming languages, including CC plus Java, JavaScript and others. Not all programming languages such as Python natively support think-case syntax, but it can be simulated using the human hand if elif else syntax.
Example:
SO the Output is
Variable Initialization:
a, n, np, os These numbers are declared as integers. And we can used these numbers to store
op" It is used for to user the selected option
n: The used can sum the values that he want to sum
ns: It is used to input the number for more execution
a: used for sum up numbers
Exit is a logic variable that controls when the Loop ends. Initially set to False indicates that the program will continue running.
a=0 , this starts the accumulator to zero
REPEAT LOOP The program enters a repeat loop that will continue until the executor sets the loop status.
User Menu: The user is presented with a menu with three options
Sum numbers: Allows the user to enter a number of numbers
Show results: Calculates their sum The current result displays the accumulated sum of numbers
End program: Exits the program
Reading User Input:
The user is asked to select an option and the selected option is stored in the mean option.
Switch-Case Structure:
The user is asked to select an option and the selected option is stored in the mean option.
- Case 1: Summing Numbers:
In this case want to sum. After summarizing, a confirmation message is displayed and after pressing any, screen is cleared.
- Case 2: Show Results:
The program displays the current amount to one and prompts you to press any key to continue surfing, after which the screen is cleared.
- Case 3: End Program:
The screen is cleared, and a farewell message ("Goodbye =")) is printed.
The exit variable is set to true, which will cause the loop to terminate in the next iteration, terminating the program
Default Case (Invalid Input):
If all enter a value other than one, two or three, a time option message is displayed asking the user to press a key to continue and the screen is cleared.
Loop Termination:
Loop repeats until I prompt them again until all of them choose option three, which sets exit is equal to true, terminating the program.
The Program is:
So the Output is:
Finally I completed all tasks. I tried my best to complete all tasks
Invite Friends: @josepha , @ruthjoe , @suboohi
Special Thanks to @alejos7ven