Basic programming course: Lesson #5 Control structures. Part 2. Cycles. [ESP-ENG]

in #devjr-s20w52 days ago

Hi friends, after reading around I am interested in participating in an amazing challenge this week. The challenge is titled: Lesson #5 Control structures. Part 2. Cycles. by Mr. @alejos7ven, following the contest link: Basic programming course: Lesson #5 Control structures. Part 2. Cycles. [ESP-ENG]

Previously I also invited other friends such as Mr. @irawandedy, Mr @muzaack1 , sir @sisol, sir @aneukpineung78 and my friend @cymolan.
To complete all of this, this time I will use the PHP programming language, please apologize if there are any mistakes because understandably I am learning. Here is my participation:

php.jpg

1. Describe the cycles in detail. What are the cycles and how are they used? What is the difference between While and Do-While cycles?
  • We start with loops, which are a way to run the same code repeatedly as long as a condition is still true. So, we don't have to write the same code over and over again. For example, counting from 1 to 10 or asking the user to enter a number until they give the correct input.

The fundamental difference between while and do-while

  • Well, for While Loop, we can think of it like a security guard who keeps very tight. Inside the While Loop, it will check the condition first and then get permission so that the code inside can run. So if the first condition is already wrong, then the while code will not run at all, that's how.

An example of code in php, for example I give the file name while.php.
c1.jpg
In the code that I have listed above means that as long as the value of $i is still smaller or still equal to 7, surely this program will print starting from “Number: 1”, ”Number: 2”, ”Number: 3”, ”Number: 4”, ”Number: 5”, ”Number: 6” and ”Number: 7”. once $i is greater than 7, the program will stop.
Here's after I run the program.
c2.jpg

  • Next is the Do-While Loop, which we can consider easier and lighter, because the do-while will first run the code once, then check the conditions afterwards. Even if the initial condition is wrong or incorrect, the code in the do can still be run at least once.

Here is one example of do-while in php and I named the file do-while.php:
c3.jpg
From the php code above, we will know that even though the initial $i value is 10 (of course it is a number greater than 9), then the program still prints “Number: 10” for the first time even before checking that the condition is false. Then, because the condition is false (10 is a larger number than 9), the coding will stop immediately.

Here's a look at the results of the do-while.php program above:
c4.jpg

So the clearest and most important difference between while and do-while is that in while it is to check the condition first, then run the code, while in do-while it is to run the code first just once, then just check the condition.

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

Well, I'll explain a little bit that the switch-case structure in programming is usually used in order to select and execute one option from many blocks of code, the selection is based on the value of a variable. So switch-case is similar to some regular if-else statements, but in fact it is cleaner and even easier to read when we try to check many possible values for a particular variable.

The way this switch-case works is to check the value of a variable, then run a block of code like the value of the option. However, if the value of our choice does not exist or does not match, it will automatically run the default block (if any) that functions as a replacement for it.

Here I give an example of a switch-case that we usually find in everyday life, for example in determining food menu choices. I gave the file name switch-case1.php.
c5.jpg
In the php code above, we have Input by utilizing an HTML form so that we can request input from someone. Then there are several choices that will be given including A, B, C, and D. From the Input, of course, it will be sent to the server to be able to use the POST method and can be found by the $menu variable.

Next we will take advantage of the switch-case where we can see from the $menu value, then the program will select the appropriate case as desired and will run the statement which allows echo to display the menu that has been selected by someone.

For example, if someone will choose “A”, then make sure to print the result is the text/message: “You choose Fried Rice”. If someone would make the choice other than the one that does not exist (for example, “F or E”), then the default will be executed and print “The choice is not available”. even the menu is not available.

From the program also uses a break where each case ends with a break, it all aims to ensure that the program will not run another case after finding a match for the choice.

Of course, there we have created an HTML form that allows that if there is no choice (when you first open the page), then this program will display an HTML form so that users can choose the existing and available menus only,

Here I run the PHP program:
@walictd (3).gif

Friends, switch-case is often utilized when one wants to check many possible values for a single variable. We can be sure that switch-case is neater and even more readable than utilizing many if-else statements.

3. Explain what the following code does:
Algorithm switchcase
Define  op, n, ns, a As Integer;
Define exit As Logic;

exit = False;
a=0;

Repeat 
    Print  "Select an option:";
    Print  "1. Sum numbers.";
    Print  "2. Show results."
    Print  "3. End program."; 
    Read op;
    
    Switch op Do
        case 1:
            Print "How much nums do you want to sum?";
            Read n; 
            For i from 1 to n Do
                Print "Enter a number: ";
                Read ns; 
                a = a + ns;
            EndFor
            Print "Completed! Press any button to continue";
            Wait Key;
            Clear Screen;
        case 2:
            Print "This is the current result: " a;
            Print "Press any button to continue.";
            Wait Key;
            Clear Screen;
        caso 3: 
            Clear Screen;
            Print "Bye =)";
            exit = True;
        Default:
            Print "Invalid option."; 
            Print "Press any button to continue";
            Wait Key;
            Clear Screen;
    EndSwitch
    
Until exit
EndAlgorithm

Alright guys, let's take a look at the code contained in the program above well, and all of it is an interactive menu using the switch-case structure where there we will be given three types of choices:

  • The number one function is to sum numbers.
    If we select lift 1 then it will all work to add up some numbers that we will enter. The program will certainly ask us how many numbers we want to sum, then the program will ask someone to enter each number one by one. As for the sum result, it will be stored in variable a.

  • The second function is to show the result of the sum of the numbers we have calculated in number 1.
    If we choose the second option, then of course we will see the result of the sum of the numbers that have been done before in option 1. Then the program will display the value of a above, which is the overall result of all the numbers that have been summed up.

  • As for the 3rd option, it serves toend the program.
    If we specify the option number 3 then surely it will close all programs and will display the message “Bye =)” and then the program really stops.

How this program works is
First, the code will run a program that will ask for input choices from us. Furthermore, by utilizing switch-case it will carry out actions such as: by selecting lift 1 then it means we will add numbers, if we choose lift 2 then it will function to see the results of the addition, while if you choose the number 3 it will certainly exit and stop working, now if we choose Other options then surely the program will show a message containing “Invalid choice”, so this program will continue to repeat until we choose 3 whose purpose is to exit or stop the program.

Well friends, for more details I will try the following php program, which I named the file switch-case.php.
c6.jpg
This program is very similar to the contents of the previous coding, only this is php-based. Here are the results when I run it. Of course, don't forget to turn on XAMPP > open Browsing for example Google Chrome and type http://localhost/.... (folder name and php file name that we will run).
@walictd (4).gif

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

Here I again utilize the php programming language to run a web to fulfill the request in number 4. In this program, it will ask us to enter a number greater than 10, then it will add up the numbers starting from number 1 until the number we enter according to our choice, for example 15, then it will be added up from 1 to 15. After that, the results will be saved by utilizing the session.

The following is the coding that I made using php and gave the file name jumlah.php
c7.jpg
In the program above, we first start the Session where the program starts with session_start(); with the aim of storing user data during an active session. Next, we will continue with variable initialization, which if the $_SESSION['sum'] variable does not exist or does not exist, the program initializes the value to 0. Then this will be used so that it can store the total of the sum. Next we will check the reset button where by we press the reset button, the session fails everything and uses session_destroy(), it will all delete the data, finally we will be directed back to the initial page and clean.

In this program we also accept input in the form of numbers, which is the program's purpose to check if we have filled the form with numbers. If we enter a number greater than 10 then it will be stored in $_SESSION['number']. But if not (the number we enter is less than 10), surely our program will give an error message displayed, or even will be empty. When the process of calculating the addition then the valid number has been stored, at that time our program will utilize the While loop with the aim of summing the numbers starting from 1 up to the number entered. So that the result of the summation will be stored in $_SESSION['sum'] which will then be displayed on the screen.

On the input and reset form, even if there is no valid number, the input form is still displayed with the purpose of asking us to immediately enter a number that is more than 10. The reset button will allow us to start the calculation from the beginning or the same as before we did anything. I also added the bottom of the page as a copyright mark that shows the name of the owner of this program, hehe.

Next I will show the results when running the program above. As usual, we first turn on XAMPP so that it is already alive, then just open the browser to run the program.
@walictd (6).gif

Source :
https://steemit.com/devwithseven/@alejos7ven/basic-programming-course-lesson-5-control-structures-part-2-cycles-esp-eng
https://hijriyani.web.ugm.ac.id/2016/11/13/struktur-kontrol-statement-php-while-do-while-for-break-continue-dan-exit/
https://www.geeksforgeeks.org/difference-between-for-while-and-do-while-loop-in-programming/
https://www.w3schools.com/php/php_switch.asp
https://www.malasngoding.com/belajar-php-penggunaan-switch-case-pada-php/

Best Regard
@walictd
baawah.png

Sort:  
Loading...

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

This post has been upvoted/supported by Team 5 via @philhughes. Our team supports content that adds to the community.

image.png