Basic Programming Course: Lesson #5: Control Structures Part 2: Cycles

in #devjr-s20w5last month (edited)
Black Gold Elegant Jewelry Logo_20241010_223240_0000.png
edited with canva

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.


Explain the cycles in details. What are they and how are they used? What is the difference between the while and Do-While cycle?

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


Types Of Cycles

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;

Screenshot_20241010-082408.pngScreenshot_20241010-225116.png
The code above prints numbers from 0 to 4 as loop runs more than four times

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;

Screenshot_20241010-082500.png
This programmed code prints numbers ranging from 0-4 but body executes before necessary condition is checked which is a striking difference

Difference between While and Do-While cycle

CONDITIONAL CHECKS
WHILEConditional Check occurs before the loop is executed
DO-WHILE CYCLEThis occurs after the loop is executed

LANGUAGE BASE
WHILEThe while honors the C language, Java Script, Python
DO-WHILE CYCLEThis honors the Java, C, C++, precisely

EXECUTION PREREQUISITE
WHILEif and only if the condition is false it most likely would not execute
DO-WHILE CYCLEIt executes at about once in respect to the command condition

Investigate how the switch-case structure is used and give examples

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.

Mode Of Operation

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.

Syntax (in 'C' languages such as C, C++, Java and Java Script)
Screenshot_20241010-082528.pngScreenshot_20241010-082717.png
INTERPRETATION

Switch day; switch statement checks value of variable "day". Case 3; Program prints Wednesday and exists switch block because of the use of break statement. Each case defines a possible value that day can be taken from 1 to 5. Default block highlights any value that doesn't match the specified cases.


If-Else vs Switch-Case

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.

Fall-Through Behavior

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.

Screenshot_20241010-082814.pngswitch-case pythonScreenshot_20241010-082844.pngswitch-case JavaScript
INTERPRETATION

In JavaScript, structure of switch-case works similarly as that of Python as we can see from the scenario above. Wednesday is printed as case==3 default block acts as feedback for other values that aren't handled by case


Conclusively, switch case are alternative to if-else statement when working with a single expression or variable having many values.


Explain what the following codes does

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 exitis 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.


Write a program in pseudo-code that asks the user for a number greater than 10 (Do-While) then add the numbers with an accumulator (While) Example; the user enters is The program adds up 1+2+3+4+5+6+7+8+9+10+11+12+13+14+15

Screenshot_20241010-082953.png

Here is the pseudo-code I implemented it in C++ language, it impressed me seeing the beautiful art
Screenshot_20241011-010304.png

Screenshot_20241010-083852.png
Screenshot_20241010-085406.png

INTERPRETATION

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

I invite @simonnwigwe @eliany @ruthjoe @rafk to join challenge
Sort:  

Upvoted. Thank You for sending some of your rewards to @null. It will make Steem stronger.

Congratulations! - Your post has been upvoted through steemcurator06
1000152362.jpg

Curated by : @bonaventure24 - Selective Team

Loading...

Coin Marketplace

STEEM 0.21
TRX 0.20
JST 0.034
BTC 97180.31
ETH 3331.80
USDT 1.00
SBD 3.02