Basic programming course: Lesson #4 Control structures. Part 1. Conditionals

in #devjr-s20w43 days ago

Hi friends, after reading around I am interested in participating in an amazing challenge this week. The challenge is titled: Lesson #4 Control structures. Part 1. Conditionals. by Mr. @alejos7ven, following the contest link: Basic programming course: Lesson #4 Control structures. Part 1. Conditionals. [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:

My Business Ideas  Registration, License, and Taxes (2).jpg

1. Explain your understanding of conditional structure and mention 2 daily life situations where you can apply this concept.

First of all, I explain a little about the conditional structure in PHP which is usually used to control the execution flow of a program. So with this kind of structure, of course, a program may be able to make a decision based on certain conditions, a simple example is whether a variable is true or even the variable is false, then which code will be executed accordingly. Usually, this structure is often utilized in PHP, including if, else if, and else.

Here I will give 2 examples of applying it in daily activities.

  1. When checking the battery of our cell phone or laptop or so on:
  • If the phone battery is less than 20%, we will charge it.
  • If the battery is between 20% and 80%, we don't need to charge it again.
  • If the battery is more than 80%, then we will stop charging.

Here's an example of the program:

  <code><?php
 // Percobaan nilai baterai ponsel kita
$baterai = 19;  // Nilai ini yang akan kita Gantikan dengan nilai baterai yang berbeda untuk pengujiannya

// Untuk mengecek status baterai kita dengan kondisional
if ($baterai < 20) {
echo "Baterai mungkin terlalu rendah! Silakan isi dulu dayanya.";
} elseif ($baterai >= 20 && $baterai <= 80) {
echo "Baterai cukup, tidak perlu diisi daya.";
} else {
echo "Baterai lebih dari 80%, Silahkan anda hentikan pengisian daya.";
}
?></code>

image.png
I named the php file above with cekbateraiponsel.php. Then to run the program, I started XAMPP first.
image.png
After that I will run the program, where I set the battery at 19 (this number can be changed according to the desire to check it). Make sure the php file is stored in the htdocs folder and I also added a folder inside with the name “course”. Next, use Google Chrome to run the program and give the command “http://localhost/kursus/cekbateraiponsel.php”, then the results will appear as follows:
image.png
Then when I replace the battery number above 80, the result will be like this:
image.png

  1. When checking the temperature to turn on the air conditioner:
  • If the temperature in the room is more than 30°C, then we will turn on the air conditioner.
  • But if the indoor temperature is between 25°C and 30°C, then we may just open the window.
  • But when the temperature in the room is below 25°C, then we no longer need to turn on the air conditioner.

Here's an example of the program:

  <code><?php
 // Percobaan pada nilai suhu ruangan
 $suhu = 25;  // Nilai ini bisa di gonta-ganti dengan nilai suhu yang berbeda untuk pengujiannya

 // Mengecek status suhu dengan kondisional
 if ($suhu > 30) {
 echo "Suhu saat ini terlalu panas! Silahkan hidupkan AC.";
} elseif ($suhu >= 25 && $suhu <= 30) {
   echo "Suhu mulai terasa hangat, Silahkan membuka jendela.";
} else {
 echo "Suhu dingin, saya rasa tidak menghidupkan perlu AC.";
}
?></code>

image.png

Then I give the name of the php file with ceksuhu.php. Then I will run the program and not forget to turn on XAMPP, here are the results.
http://localhost/kursus/ceksuhu.php

image.pngimage.png
2. Create a program that tells the user “Welcome to the room. What do you want to do?”, if the user writes 1 it will pop up a message saying ‘You have turned on the light’, if the user writes 2 it will pop up a message saying ‘You have left the room’. Use conditionals.

When we want to create a simple program with the aim of solving this problem, I make use of HTML and PHP forms only. In this simple program, of course, we will ask to choose the option to turn on the lights or leave the room. The first step we will create a web framework is to create HTML and then create PHP.

  1. We will create an HTML form that contains the input and we name it program1.html, here is the HTML coding:
    image.png
    In the HTML form above, we ask to enter the number 1 or 2. Then the data will be shared to the PHP file that I named (program1.php) by utilizing the POST method.

  2. We will create a PHP file that we name program1.php to Process Input:
    image.png
    In the HTML form that we have created above, we will capture using $_POST['action']. While we will use the conditional if to see if the number 1 or 2 has really been entered. So if you have chosen 1, of course the program will give a message that the lights have been turned on. while if you choose the number 2, of course the program will give a message that you have left the room. However, if we enter a number other than 1 or 2, the message will appear that the choice is invalid.

Let's run the program by using the following command in Google Chorme or other browsing “http://localhost/kursus/program1.html”, but not before running Xampp first. and the results are as follows:

image.pngimage.pngimage.png
image.pngimage.png

@walictd.gif

3. Create a program that prompts the user for 4 different scores, calculates the average score, and if the result is greater than 70 then a message will appear stating the section passed, otherwise, a message will appear stating the section can be improved.

To create a program to enter 4 different values, with the aim of calculating the average, then we will give a message based on the average result.

As usual, we will create a series of programs with an HTML Form that we name program2.html. Here is the HTML coding:
image.png
In the HTML Form above, we are asked to put 4 values that use input type number. Furthermore, the value data will be shared with PHP (program2.php) which will be created shortly and still use the POST method.

Next, we will assemble a PHP File with the aim of processing Input and to find out the average value, here is the program:
image.png
In the PHP script above, of course, we will see whether all the values have been entered perfectly.
Using a variable to store the value inputted by the user from the form. The average value will be calculated by totaling all the values and then we will divide it by the number of values, namely (4).Remember we will structure if utilized so that it can see if the average value is greater than 70, so that it gives a message according to its own results.

Let's run the program using the http://localhost/kursus/program2.html command in Chrome:

image.pngimage.pngimage.png
image.pngimage.png

@walictd (1).gif

Source:
https://artikelkejuruan.blogspot.com/2017/10/struktur-kondisi-dan-perulangan-dalam.html
https://www.petanikode.com/php-sintak/
https://www.canva.com/

Best Regard
@walictd
baawah.png

Sort:  
Loading...

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

Your post has been rewarded by the Seven Team.

Support partner witnesses

@seven.wit
@cotina
@xpilar.witness

We are the hope!

Wow langsung dikomemtari oleh elon misk ya di X 😀😀😀

Gaklah bang, itu kebetulan aja berdampingan sama postingan beliau. Tapi kayaknya bakal dikomen hehehe

Coin Marketplace

STEEM 0.18
TRX 0.16
JST 0.029
BTC 62555.95
ETH 2435.55
USDT 1.00
SBD 2.64