Basic Programming Course: Lesson #5: Control Structures Part 2: Cycles
Hello friends and welcome to my article in the SEC: S20/W5 in this great dynamics. I would be putting efforts to make out quality posts too.
Cycles are series of programming instructions used to repeat a block of code in quick succession until a required condition is achieved. They are more important in activities which requires repetition, such as performing calculations or in a list repeatedly to achieve a desired aim. They could also be referred to as loop
For Cycles (Loop): |
---|
This can be used when iteration number sequence is known beforehand. We use iterate over a range of diversifying numbers, or list.
scenario;
While Cycle (Loop): |
---|
This repeats a block of code while a specified condition stands True
The condition is usually analysed carefully before running it. If False
the chosen loop would not be executed from the onset.
scenario;
WHILE | Conditional Check occurs before the loop is executed |
---|---|
DO-WHILE CYCLE | This occurs after the loop is executed |
WHILE | The while honors the C language, Java Script, Python |
---|---|
DO-WHILE CYCLE | This honors the Java, C, C++, precisely |
WHILE | if and only if the condition is false it most likely would not execute |
---|---|
DO-WHILE CYCLE | It executes at about once in respect to the command condition |
The switch-case
structure is a medium to curtail and control flow statement used in programming to ideally execute one block of code among several options, based on value of expression or variables. It makes code more visible, readable and beautifully arranged when analysed by a programmer.
Importantly the break statement is added after each case to prevent program from executing succeeding cases is called the fall-through
It evaluates an expression or variable. A default-block
can be added to handle case value matches variables or in absence of expression.
If-Else
is more flexible because its nature allows the flow of complex conditions and expressions while the Switch-Case
is more efficient, easier and simple to read, when there are multiple conditions to evaluate.
In program C, C++ and some other programming language, in cases where a break statement is missing, execution continues to the next case as shared in the scenario below. In JavaScript, structure of switch-case works similarly as that of Python as we can see from the scenario below respectively.
switch-case python | switch-case JavaScript |
---|
case==3
default block acts as feedback for other values that aren't handled by caseConclusively, switch case are alternative to if-else
statement when working with a single expression or variable having many values.
Analyzing the code created in the lesson by the professor, we use the switch case structure, which I would be explaining, stressing what the code does.
Variables:
op, n, a
these are integers spotted in the code.
a
is used to store the sum of number entered
exit
is known as a Boolean Flag
set to False
Program Logic:
Program runs in a loop until exit
is set to true. User is promoted to choose one of the following. Show result End program'
Based on users choice (op
) one of the following actions takes place.
CASE 1:
For each number, user is promoted to enter a value, added to accumulator variable `a. User is asked how many numbers they want to sum. Program informs user that task is completed and awaiting user to press a key.
CASE 2:
Program displays current sum (a` and waits for user to press a key)
CASE 3:
Program clears the screen and provide "Bye=)
sets exit to True
which then breaks the loop and ends the program.
Default Case:
If users enters an error code, the program notified them asking them to press a key to continue.
Features;
The loop repeats until users select the option to end the program in
case 3
The sum is persistent across different inputs which means the sum is cumulative except program starts afresh.
Here is the pseudo-code I implemented it in C++ language, it impressed me seeing the beautiful art
The get_number()
function is one that uses a loop to add all numbers from 1 to the entered number. The function accumulates result in sum variable.
Main program is known as get_number()
function used to get valid input and calls sum_number
(number] to sum and show results. I entered numbers 1 to 20 and it displayed [1+2+3+4+5.........20 =210]
Cc;
@alejos7ven
Upvoted. Thank You for sending some of your rewards to @null. It will make Steem stronger.