Basic programming course: Lesson #5 Control structures. Part 2. Cycles.

in #devjr-s20w5last month

steemit-engagement-challenge-cover.jpeg

Greetings Steemit friends

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

Cycles, in other words, are commonly known as loops. Loop means letting a repeat of an instruction over and over. It is used to handle repetitive tasks or do the same thing until a different condition is met. Cycles are used for data processing, auto-repeating tasks, input validation, etc. Cycles are an important aspect of programming, using conditions to get the right data. No matter the number of times needed to loop based on the condition.

The server type of cycle (loops), but I will try to explain the commonly used ones.

For loop: This cycle is used when you know the number of iterations you want to run in your program. Here you assign the number of loops, by applying a starting point and point. The For loop is broken into the starting point, condition(which needs to be true), and number of times the cycle will go.

for (starting point; condition; number_of_time) {
// code block to be repeated
}

Screenshot 2024-10-13 195405.jpg


For i = 1 to 5 Do
Print I
EndFor

While Loop: This cycle has a condition met before the iteration is executed. It will continue the cycle by executing the iteration as long as the condition is true. The cycle only stops when the condition is false.

while (condition) {
// code block to be repeated
}

Screenshot 2024-10-13 195822.jpg

i = 1
While (i <= 5) Do
Print I
i = i + 1
EndWhile

Do-While Loop: This is the opposite of the previous cycle mentioned above. Here the code is executed before the condition is checked. The iteration is completed no matter the outcome of the condition. So even if the condition returns false, the iteration was done already.

do {
// code block to be repeated
} while (condition);

Screenshot 2024-10-13 200848.jpg

Screenshot 2024-10-13 200939.jpg


i = 1
do
print i
i = i + 1
while i <= 15

Differences Between While and Do-While Cycles

  • The While loop will execute if the condition is not true while the Do-While will execute the first time before checking if the condition is true or false.
  • The While loop condition is checked before the loop starts, while the Do-While loop condition is checked after the first loop.

Investigate how the Switch-case structure is used and give us an example.

The switch case is a conditional structure not too different from the If conditional structure we saw in the past session. The switch loop is used to pick out a block of code from multiple options to execute. This structural condition deals with multiple options and helps to break down logic. As I mentioned, it has multiple options with each option having its specific logic to be executed.

Switch is broken into three sections, the variable, case, and default.

Variable is the container holding what needs to be evaluated
Case (which can be as many as you want) is the different options. Any that will match the variable, the block of code in that case will be executed. note, that whenever a case is matched, the rest are ignored
Default comes in a situation where none of the cases (options) match the value of the variable. Default is optional, but best practices are recommended.

  • Example:

I will use the switch to ask the program to do a different task on different days of the week,

Monday: Check emails.
Tuesday: Attend meetings.
Wednesday: Work on a project.
Thursday: Review project.
Friday: Review reports.
Saturday: Plan for the next week.
Sunday: Work on the Community Account.

Screenshot 2024-10-13 201745.jpg


Print "Enter a number from 1 to 7 to select the day of the week:"
Read day
Switch day Do
Case 1:
Print "Monday: Check emails."
Case 2:
Print "Tuesday: Attend meetings."
Case 3:
Print "Wednesday: Work on a project."
Case 4:
Print "Thursday: Review project."
Case 5:
Print "Friday: Review reports."
Case 6:
Print "Saturday: Plan for the next week."
Case 7:
Print "Sunday: Work on the Community Account."
Default:
Print "Invalid input! Please enter a number between 1 and 7."
EndSwitch

Explain what the following code does:

To have a better understanding, I start with the variables defined, the conditional structure used and the logic to be executed.

Variables op, n, ns, and a, which will hold integer values. The variable a is assigned a value of 0. Then we had the variable exit, which will be used to end the loop with a value of False.

Next, we have the Do-While loop which runs the program, waiting for the user to input a value. The loop has three options, while the op variable waits to hold the selected option or the condition will check to exit the program.

Next is the switch loop case, for another logic. The switch makes use of the value of the op. Our program has three cases.

Case 1
the user is asked how many numbers they want to sum and is stored in the variable n. Here we have a for loop, which will have to loop n times. Each loop will sum a to the new number enter (ns). Note initially a = 0 and after the first loop, a = a (previous for loop a result) + the second number enter (ns). And so on unstill, we get the last entry value for n. Each loop accumulator sum and store in a.

Case 2
When the user selects option 2, the user wants to see the results, which is the value accumulated and stored in a.

Case3
The user selects option 3. The user is satisfied or decides to change his or her mind. This case makes use of the clear screen logic.

Default
This is to help users if an invalid option is selected.

Note that if option 1 was not initiated. Option two will return zero(0) because the a is equal to 0.

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

Screenshot 2024-10-13 202931.jpg

Define num, total, counter As integer;

So before we check if the user has entered a number greater than 10. We make use of a Do-while loop to ask the user to input a number.

Do
Print "Enter a number greater than 10: ";
Read num;
While (num > 10)

So, at this point. The do-while loop will continue to loop until the correct value is input and the condition is true.

Next, I introduce a counter variable and another variable to hole the total sum. My counter will start from 1, and that is the reason for assigning 1 as the first value.

total = 0;
counter = 1;

Next, we have the while loop, which logic demands the program to do an increase (+1) to the counter until we get to the number the users enter. In our case, it is 15 as required. For each loop a sum operator is executed, adding the counter to the total.

While (counter <= num) do
total = total + counter;
counter = counter + 1;
EndWhile

At the end of the loop, the total variable holds the sum of all numbers from 1 to the number the user input.

Print "The sum of all numbers from 1 to ", num, " is: ", total;



Cheers
Thanks for dropping by
@fombae

Sort:  
Loading...

Upvoted! Thank you for supporting witness @jswit.