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:
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? |
---|
The fundamental difference between while and do-while
An example of code in php, for example I give the file name while.php.
Here is one example of do-while in php and I named the file do-while.php:
Here's a look at the results of the do-while.php program above:
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.
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:
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.
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
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.
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/