<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Fabala Dibbasey</title>
    <description>The latest articles on DEV Community by Fabala Dibbasey (@fabaladibbasey).</description>
    <link>https://dev.to/fabaladibbasey</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F566547%2F66904e7f-a33d-4ac4-b1f3-6066cae66f34.jpg</url>
      <title>DEV Community: Fabala Dibbasey</title>
      <link>https://dev.to/fabaladibbasey</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/fabaladibbasey"/>
    <language>en</language>
    <item>
      <title>JAVA BASIC</title>
      <dc:creator>Fabala Dibbasey</dc:creator>
      <pubDate>Thu, 11 Nov 2021 12:00:00 +0000</pubDate>
      <link>https://dev.to/fabaladibbasey/variables-in-java-2j5g</link>
      <guid>https://dev.to/fabaladibbasey/variables-in-java-2j5g</guid>
      <description>&lt;p&gt;You will learn:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Variables&lt;/li&gt;
&lt;li&gt;Data types&lt;/li&gt;
&lt;li&gt;Comments&lt;/li&gt;
&lt;li&gt;Interacting with user input&lt;/li&gt;
&lt;li&gt;Operations&lt;/li&gt;
&lt;li&gt;Casting&lt;/li&gt;
&lt;li&gt;Math Class&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The code below calculate the area and the circumference of a circle and output it to the user.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;3.142&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;3.142&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;OUTPUT&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;153.958&lt;/code&gt;&lt;br&gt;
&lt;code&gt;43.988&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;But the output are vague to the user, labeling them can give user clear understanding of each output.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Area of a circle with radius 7 is: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;3.142&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Circumference of a circle with radius 7 is: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;3.142&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Texts must be written in between double quotation mark and you can perform addition operation on two or more texts or texts and numbers to join them together.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;OUTPUT&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;Area of a circle with radius 7 is: 153.958&lt;br&gt;
Circumference of a circle with radius 7 is: 43.988&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Can you tell what the code below does?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your Guess might be:&lt;br&gt;
It multiple 4 by 3, &lt;strong&gt;YES&lt;/strong&gt;&lt;br&gt;
however, the code calculate the area of a rectangle with length 4 and width 3.&lt;br&gt;
You missed it because there is no readability in the code.&lt;br&gt;
To add readability to the code we introduce &lt;strong&gt;VARIABLES&lt;/strong&gt; &lt;br&gt;
Considering the code below, can you easily tell what it does?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;areaOfRectangle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;areaOfRectangle&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Java keyword &lt;code&gt;int&lt;/code&gt; before the variable name is &lt;strong&gt;primitive data type&lt;/strong&gt;&lt;br&gt;
Java is &lt;strong&gt;statically typed language&lt;/strong&gt;, you must declared the data types of every variable. Once a variable is declared to be of a certain data type, it cannot hold values of other data types.&lt;/p&gt;
&lt;h5&gt;
  
  
  Three data types we will be using most are:
&lt;/h5&gt;

&lt;p&gt;&lt;code&gt;int&lt;/code&gt; =&amp;gt; It holds integer values e.g&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;double&lt;/code&gt; =&amp;gt; It holds decimal point values e.g&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="no"&gt;PI&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;3.142&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;String&lt;/code&gt; =&amp;gt; It holds sequence of characters (text) e.g&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Fabala"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Values of String data types are en-quoted in double quotation mark. Only addition(+) operation can be perform on String and are called concatenation.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Halifa"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Sallah"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;fullName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: Spaces are characters and empty String is just &lt;code&gt;""&lt;/code&gt;, double quotation mark with nothing inside.&lt;/p&gt;

&lt;h4&gt;
  
  
  Exercise
&lt;/h4&gt;

&lt;p&gt;Write a program that calculate and output both the &lt;code&gt;Area and Circumference&lt;/code&gt; of a circle with &lt;code&gt;radius 3.5&lt;/code&gt;, take &lt;code&gt;PI = 3.142&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Solution
&lt;/h4&gt;

&lt;p&gt;solution will be available later!&lt;/p&gt;

&lt;p&gt;It turns out that &lt;code&gt;Variables&lt;/code&gt; does much more than adding readability to our code.&lt;br&gt;
By just declaring &lt;code&gt;PI&lt;/code&gt; variable only once, you can reuse it in evaluating area and circumference of multiple circles and if there's need for changes in &lt;code&gt;PI&lt;/code&gt;, you only need to do that in &lt;code&gt;PI&lt;/code&gt; variable.&lt;br&gt;
Code below calculate the area of three circles, A, B and C with radius 3.5, 7, and 4.5 respectively taking PI as 3.14159265358979&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;
        &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="no"&gt;PI&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;3.14159265358979&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

        &lt;span class="c1"&gt;//circle A&lt;/span&gt;
        &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;radiusA&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;3.5&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;areaOfCircleA&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;PI&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;radiusA&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;radiusA&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Area of circle A: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;areaOfCircleA&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;//circle B&lt;/span&gt;
        &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;radiusB&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;areaOfCircleB&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;PI&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;radiusB&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;radiusB&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Area of circle B: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;areaOfCircleB&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;//circle C&lt;/span&gt;
        &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;radiusC&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;4.5&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;areaOfCircleC&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;PI&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;radiusC&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;radiusC&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Area of circle C: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;areaOfCircleC&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I deliberately add &lt;code&gt;//circle A&lt;/code&gt;, &lt;code&gt;//circle B&lt;/code&gt;, &lt;code&gt;//circle C&lt;/code&gt; in the code above. Those are &lt;strong&gt;COMMENTS&lt;/strong&gt; and are use to add additional readability to code. &lt;strong&gt;COMMENTS&lt;/strong&gt; are commonly use to explain what the code does and because &lt;strong&gt;COMMENTS&lt;/strong&gt; are ignored by the compiler (are not executed), people also use it to comment codes that they do not want to execute.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;// single line comments&lt;br&gt;
  /* this is a&lt;br&gt;
     multi line&lt;br&gt;
     comments&lt;br&gt;
  */&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I want to demonstrate &lt;strong&gt;VARIABLES&lt;/strong&gt; in actions in one more instance before we actually define what they are.&lt;/p&gt;

&lt;h3&gt;
  
  
  Interacting with users
&lt;/h3&gt;

&lt;p&gt;Let's write a program that takes in two integer numbers from the user and output their sum to the screen.&lt;/p&gt;

&lt;p&gt;There are few ways to read input from the user in java and the two most commonly use are &lt;code&gt;BufferedReader class and Scanner class&lt;/code&gt; but for now we are going to use Scanner class.&lt;br&gt;
To use &lt;code&gt;Scanner class&lt;/code&gt;, we need to import it on top of our own class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.Scanner&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We import Scanner class from &lt;code&gt;Java.util&lt;/code&gt; package.&lt;br&gt;
More on import and packages later in the course. Our focus is on variables. Feel free to retype the exact codes seen below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Scanner&lt;/span&gt; &lt;span class="n"&gt;scan&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Scanner&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;in&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Enter an integer number: "&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;firstNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;scan&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;nextInt&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Enter another integer number: "&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;secondNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;scan&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;nextInt&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;firstNumber&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;secondNumber&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Sum of "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;firstNumber&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" and "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;secondNumber&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" is "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="c1"&gt;//it's a good practice to close scanner object when done&lt;/span&gt;
        &lt;span class="n"&gt;scan&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;close&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;OUTPUT&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lv8HBXeS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/idqyij77jfkezj23buqq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lv8HBXeS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/idqyij77jfkezj23buqq.png" alt="Output" width="880" height="434"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Exercise
&lt;/h3&gt;

&lt;p&gt;Write a program that reads an integer from the user and squared it.&lt;br&gt;
&lt;strong&gt;SOLUTION&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3r0yvdxB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/srj76337uksl4hd2irpu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3r0yvdxB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/srj76337uksl4hd2irpu.png" alt="Solution in screenshot" width="880" height="434"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Exercise
&lt;/h4&gt;

&lt;p&gt;Using Scanner class, read two numbers from the user and perform four basic arithmetic operations on them. Your codes should be readable using variables and user should clearly understand each output.&lt;/p&gt;

&lt;p&gt;Can you try to do the same exercise without using &lt;strong&gt;VARIABLES&lt;/strong&gt;. Good luck!&lt;br&gt;
We have now seen &lt;strong&gt;VARIABLES&lt;/strong&gt; in action.&lt;br&gt;
&lt;strong&gt;VARIABLES&lt;/strong&gt; is related to variables we have seen in Mathematics, Economics and English.&lt;/p&gt;

&lt;p&gt;In Mathematics, we solve for x and in programming we use x to map to our values in memory (think of x being descriptive and holding our value that a stored in the memory (heap) )&lt;/p&gt;

&lt;p&gt;In English and Economics, &lt;strong&gt;Variables&lt;/strong&gt; are something that are subject to changes or can assume any set of values and in programming &lt;strong&gt;variable&lt;/strong&gt; too can change from mapping one value to another value.&lt;br&gt;
for example, in the code below variable name was mapping to &lt;strong&gt;Fred&lt;/strong&gt; and it was now reassigned to map to &lt;strong&gt;Fabala&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Fred"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Fabala"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Name: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Equal to sign (&lt;code&gt;=&lt;/code&gt;) is an &lt;strong&gt;assignment operator&lt;/strong&gt;. We use it to assign values to variable names.&lt;/p&gt;

&lt;p&gt;It is worth knowing that you do not have to assign value (Initial value) to a variable at the time of declaration.&lt;br&gt;
You can declare variable and assign value to it later in your code. The code below try to demonstrate the use case&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;heigh&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// declaring variable&lt;/span&gt;
    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"measuring..."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;heigh&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;13.7&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// assigning value&lt;/span&gt;
    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Heigh: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;heigh&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note in Scanner class we have more methods than nextInt();&lt;br&gt;
Other commonly used methods are: &lt;br&gt;
&lt;code&gt;next()&lt;/code&gt;, to read word from the user&lt;br&gt;
&lt;code&gt;nextLine()&lt;/code&gt; to read the whole line&lt;br&gt;
&lt;code&gt;nextDouble() or nextFloat&lt;/code&gt; to read the decimal (floating point numbers)&lt;br&gt;
&lt;code&gt;nextShort()&lt;/code&gt;, &lt;code&gt;nextLong()&lt;/code&gt;, &lt;code&gt;nextBoolean()&lt;/code&gt; and much more. We will explore more of them as we need them. &lt;/p&gt;
&lt;h4&gt;
  
  
  Exercise
&lt;/h4&gt;

&lt;p&gt;Write a program that ask user his/her name and great him/her like "Hello " + name!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Don't forget you will need &lt;code&gt;next()&lt;/code&gt; method from the Scanner class&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  Note
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Variable names should be descriptive and meaningful&lt;/li&gt;
&lt;li&gt;Variable must only start with letters or underscores and can contain any character.&lt;/li&gt;
&lt;li&gt;Reserve keywords cannot be use as variable name&lt;/li&gt;
&lt;li&gt;By convention all java variable name should start with small letter with all subsequent words capitalized. e.g &lt;code&gt;firstName&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;By convention all constant variables are written in UPPER CASE e.g &lt;code&gt;double PI = 3.142;&lt;/code&gt; and to enforce the constant nature of the variable, you can use &lt;code&gt;final&lt;/code&gt; keyword in java. 
e.g &lt;code&gt;final double PI = 3.142;&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The code below had wrongly assigned wrong values to the wrong  variable. firstName should contain the first name value, "Fabala" and lastName should contain the last name value "Dibbasey*. Our task is to correct the order of the values aka swap so that firstName and lastName will now hold their right values "Fabala" and "Dibbasey" respectively.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Dibbasey"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Fabala"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Try it out before looking at the solution.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Dibbasey"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Fabala"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Before swapping... "&lt;/span&gt; &lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"first name: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"\nlast name: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;lastName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"After swapping"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"first name: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"\nlast name: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Exercise
&lt;/h4&gt;

&lt;p&gt;Read two double number from the user and swap them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Data types
&lt;/h2&gt;

&lt;p&gt;Primitive and non primitive are the two data types in Java.&lt;br&gt;
&lt;a href="https://media.geeksforgeeks.org/wp-content/cdn-uploads/20191105122725/Primitive-Data-Types-in-Java-4.jpg"&gt;Primitive data types&lt;/a&gt;&lt;br&gt;
Non primitive data types&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Strings&lt;/li&gt;
&lt;li&gt;Arrays&lt;/li&gt;
&lt;li&gt;Classes&lt;/li&gt;
&lt;li&gt;Interfaces&lt;/li&gt;
&lt;li&gt;Objects&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;more on Non primitive data types and it's differences from primitive data type later in the course. Also, We'll learn how to create our own data types on top of java built in data types later in the course.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  Exercises
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Simple interest&lt;/strong&gt;&lt;br&gt;
Write a Java program that read principal, time and rate from the user and calculate simple interest from the given input.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quadratic equation&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://en.wikipedia.org/wiki/Quadratic_equation"&gt;Quadratic equation&lt;/a&gt;&lt;br&gt;
Using java program, read a, b and c from the user and solve quadratic equation from the input.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;use Math.sqrt(a), where a is any double number &amp;amp;&amp;amp; Math.pow(a, b), where a is the base and b is the exponent.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Time conversion&lt;/strong&gt;&lt;br&gt;
Write java program that convert days ranging from 1 to 360 to seconds. Access the day from the user!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quotient &amp;amp;&amp;amp; Remainder&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://en.wikipedia.org/wiki/Quotient"&gt;Quotient&lt;/a&gt;, &lt;code&gt;integer part of the division&lt;/code&gt;.&lt;br&gt;
&lt;a href="https://en.wikipedia.org/wiki/Remainder#:~:text=The%20number%20q%20is%20called,r%20is%20called%20the%20remainder.&amp;amp;text=The%20remainder%2C%20as%20defined%20above,d%20(for%20positive%20q)"&gt;Remainder&lt;/a&gt;, &lt;code&gt;left over integer part of the division&lt;/code&gt;.&lt;br&gt;
Write a program that ask user two input numbers, the first number being the numerator and second input being the denominator, perform division on the two numbers and output to the user both &lt;code&gt;Quotient and Remainder&lt;/code&gt; of the division.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Arithmetic Operations&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;+&lt;/code&gt; Addition Operator&lt;br&gt;
&lt;code&gt;-&lt;/code&gt; Subtraction Operator&lt;br&gt;
&lt;code&gt;*&lt;/code&gt; Multiplication Operator&lt;br&gt;
&lt;code&gt;/&lt;/code&gt; Division Operator&lt;br&gt;
&lt;code&gt;%&lt;/code&gt; Remainder Operator&lt;/p&gt;
&lt;h3&gt;
  
  
  Shorthand Operations
&lt;/h3&gt;

&lt;p&gt;for &lt;code&gt;int a = 5;&lt;/code&gt; &amp;amp;&amp;amp; &lt;code&gt;int b = 4;&lt;/code&gt;&lt;br&gt;
&lt;code&gt;a += b&lt;/code&gt; ==&amp;gt; a = a + b; // 9&lt;br&gt;
&lt;code&gt;a -= b&lt;/code&gt; ==&amp;gt; a = a - b; // 1&lt;br&gt;
&lt;code&gt;a *= b&lt;/code&gt; ==&amp;gt; a = a * b; // 20&lt;br&gt;
&lt;code&gt;a /= b&lt;/code&gt; ==&amp;gt; a = a / b; // 1 (Quotient division!)&lt;br&gt;
&lt;code&gt;a %= b&lt;/code&gt; ==&amp;gt; a = a % b; // 1&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In java 3/2 (Quotient division) is not the same as 3/2.0 (floating point division). For floating (decimal) point division, at least either numerator or denominator value must be double data type. e.g 7/2.0 or 7.0/2 or 7.0/2.0&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  Increment and Decrement Shorthand Operation
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;a++&lt;/code&gt; ==&amp;gt; a += 1; ==&amp;gt; a = a + 1;&lt;br&gt;
&lt;code&gt;a--&lt;/code&gt; ==&amp;gt; a -= 1; ==&amp;gt; a = a - 1;&lt;/p&gt;
&lt;h3&gt;
  
  
  Casting
&lt;/h3&gt;

&lt;p&gt;Remember we said that once a data type is declared to be of one data type cannot hold the value of different data type.&lt;br&gt;
With casting we can match the value of one data type to another data type. for example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;3.35&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explicit casting is when we have to do the conversion manually like the code above.&lt;/p&gt;

&lt;p&gt;Implicit casting is when casting occurred automatically as in the code below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;        &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Implicit casting happen when converting smaller data type to larger data type as it's demonstrated in flow chart below&lt;br&gt;
&lt;code&gt;Byte -&amp;gt; Short -&amp;gt; Char -&amp;gt; Int -&amp;gt; Long -&amp;gt; Float -&amp;gt; Double&lt;/code&gt; and Explicit casting follow the reversed of the flow chart.&lt;/p&gt;

&lt;h3&gt;
  
  
  Math class
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Math.sqrt(a);&lt;/code&gt;, return the square root of a given double number a&lt;br&gt;
&lt;code&gt;Math.abs(a);&lt;/code&gt;, return absolute number of a given double number a.&lt;br&gt;
&lt;code&gt;Math.round(a);&lt;/code&gt; round up or round down to the nearest whole number&lt;br&gt;
&lt;code&gt;Math.floor(a);&lt;/code&gt; always round down&lt;br&gt;
&lt;code&gt;Math.ceil(a);&lt;/code&gt; always round up&lt;br&gt;
&lt;code&gt;Math.min(a, b);&lt;/code&gt; return the minimum number&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;To see more Math functions in most IDE or Editor type Math. &lt;code&gt;ctrl + space&lt;/code&gt; or consult documentation.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Exercises
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Average score&lt;/strong&gt;&lt;br&gt;
Ask UTG student his/her scores from four different exams, calculate and output the average score to the student, round your answer to two decimal places.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sum of minMax&lt;/strong&gt;&lt;br&gt;
Read three numbers from the user, calculate and output to the screen the sum of the minimum and the maximum numbers to the user.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;input&lt;/code&gt;&lt;br&gt;
5&lt;br&gt;
7&lt;br&gt;
2&lt;br&gt;
&lt;code&gt;output&lt;/code&gt;&lt;br&gt;
minMax sum = 9&lt;br&gt;
&lt;code&gt;Explanation&lt;/code&gt;&lt;br&gt;
minimum number is 2 and maximum number is 7; 2 + 7 = 9&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Congratulations you've successfully completed Java Basic Concept. Looking forward to seeing you in &lt;strong&gt;Condition and Loop&lt;/strong&gt;.&lt;br&gt;
Don't forget to connect with me on &lt;a href="https://twitter.com/DibbaseyFabala"&gt;twitter&lt;/a&gt; and &lt;a href="https://www.linkedin.com/in/fabala-dibbasey-b69a27207/"&gt;linkedin&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>codenewbie</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Algorithms with Python Variables and Conditions</title>
      <dc:creator>Fabala Dibbasey</dc:creator>
      <pubDate>Tue, 07 Sep 2021 00:44:04 +0000</pubDate>
      <link>https://dev.to/fabaladibbasey/algorithms-with-python-variables-and-conditions-3733</link>
      <guid>https://dev.to/fabaladibbasey/algorithms-with-python-variables-and-conditions-3733</guid>
      <description>&lt;h3&gt;
  
  
  Continuation...
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://dev.to/fabaladibbasey/introduction-to-computer-programming-for-coders-1ga6"&gt;Previous lesson, Introduction to Computer Programming for Coders&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Since we don't have to directly deal with &lt;code&gt;0s and 1s&lt;/code&gt; anymore, thanks to Programming languages. We are going start off by installing programming language, python into our computer and setting up the environment for writing and executing codes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Downloading and setting up&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Setting up is one of the hardest challenge you'll face as a newbie, new to coding! So don't give up. It's not hard but everything is just new to you&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Head over to  &lt;a href="https://www.python.org/downloads/"&gt;Python.org website&lt;/a&gt; and download the latest version of python. If you're having trouble installing Python, refer to some random YouTube video for help. Oh right once we install python, we need some editor just like we need Microsoft word for working with texts and excel for operations on Data.&lt;/p&gt;

&lt;p&gt;Once we get all of them up and running let's now instruct our computer to greet the user.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;user_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Enter your name: "&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user_name&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;'!'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Congratulation! You've written your first python program! &lt;/p&gt;

&lt;p&gt;What have we done?&lt;br&gt;
We asked user for an input, bind user_name to the input value and finally we print out greeting which consist of the text "Hello" with Whatever user put in, in this case we're expecting user's name.&lt;br&gt;
&lt;code&gt;user_name&lt;/code&gt; here is &lt;strong&gt;Variable&lt;/strong&gt;, &lt;code&gt;input&lt;/code&gt; and &lt;code&gt;print&lt;/code&gt; are &lt;strong&gt;Function&lt;/strong&gt; and &lt;code&gt;=&lt;/code&gt; is the &lt;strong&gt;assignment operator&lt;/strong&gt;. Notice we enclose "Hello" and '!' into quote and that's exactly a way of generating texts (strings) in python. A lot of Grammars already but more on them later. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Challenge 1&lt;/strong&gt;:&lt;br&gt;
Ask user for two input, name and surname, store them in a variable name and surname respectively and print out the full name.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Hint:&lt;br&gt;
You'll need two input function&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Enter your name: "&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;surname&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Enter your surname: "&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"your full name is"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;surname&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's ask the user for the year they were born and we'll tell them their age.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;birth_year&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Which year where you born? "&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2021&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;birth_year&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"You're"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"years old"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice we pass the birth_year variable into &lt;code&gt;int function&lt;/code&gt;. This is because the input function return string (one or more characters or texts to be precise) and all what &lt;code&gt;int function&lt;/code&gt;does is to convert the string into an &lt;code&gt;Integer number&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Challenge 2: &lt;br&gt;
Ask user for two integer numbers and return the sum of the numbers&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Hint:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You'll need two input function&lt;/li&gt;
&lt;li&gt;Don't forget to cast/convert the inputs into integer number&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;first_number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Enter an integer number: "&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;second_number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Enter another integer number: "&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;sum_of_inputs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;first_number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;second_number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Sum of the two numbers is"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sum_of_inputs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Variables
&lt;/h2&gt;

&lt;p&gt;We've already seen variables, &lt;code&gt;user_name, age etc...&lt;/code&gt; in action. Can you tell what they do?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We use variable to map to our values for references purpose&lt;/li&gt;
&lt;li&gt;Variables improve the readability of our code. As in &lt;code&gt;user_name&lt;/code&gt;, whoever read the code will know exactly that the input value is user's name, and &lt;code&gt;sum_of_inputs&lt;/code&gt; will tell the reader that the value is the result of summation of inputs&lt;/li&gt;
&lt;li&gt;Re-usability of values
For instance, performing five basic operations with two variables
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;num1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="mi"&gt;9&lt;/span&gt;
&lt;span class="n"&gt;num2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;num2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;num2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num1&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;num2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num1&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;num2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num1&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;num2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At any point you can change num1 or num2 to any value and that we'll be reflected in all the print statement.&lt;br&gt;
Hence the name variable, it value can be changed or re-bind to another value and operations on variables will still remain unaffected.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;num1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;
&lt;span class="n"&gt;num1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;#num1 is re-bind to value 7
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;%&lt;/code&gt;? It is called modulo or remainder operator.&lt;br&gt;
In Python, &lt;code&gt;/&lt;/code&gt; gives floating point value (decimal), &lt;code&gt;//&lt;/code&gt; gives quotient and &lt;code&gt;%&lt;/code&gt; give you the remainder of a division.&lt;/p&gt;

&lt;p&gt;Let's explore some application of modulo operator in programming.&lt;br&gt;
Get an integer from a user and tell whether the integer is an Even or Odd number.&lt;br&gt;
Two questions we should ask ourselves before we start to approach the problem&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;How can we get an integer from a user?&lt;/li&gt;
&lt;li&gt;How can we determine Even or Odd number?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Well, We can use input function to get data from user, cast/convert our data to an integer number using int function and all what we know about Even numbers is that they're divisible by two ( can be divide by two without a remainder ) and any number that's not an Even number must be an Odd number.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;int_number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Enter an integer number: "&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;int_number&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;int_number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"is an Even number"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;int_number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"is an Odd number"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;New concept Alert!&lt;/strong&gt; Condition or Branching, if/else more on them later.&lt;/p&gt;

&lt;p&gt;Back to &lt;strong&gt;Variables&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Question: You're given two cups, orange and banana each contains a juice of it's name. orange cup contains orange juice and banana cup contains banana juice? Can you write a program that will swap the juices in these cups. When swapped orange cup should now contain banana juice and Banana cup should contain orange juice.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Hint:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Demonstrate it with two cups, orange and banana with their respective juices&lt;/li&gt;
&lt;li&gt;Try swapping the juices in the cups&lt;/li&gt;
&lt;li&gt;If still stuck look for additional container (variable)&lt;/li&gt;
&lt;li&gt;Now swap them&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt;&lt;br&gt;
Will be posted later&lt;/p&gt;
&lt;h2&gt;
  
  
  Condition/Branching
&lt;/h2&gt;

&lt;p&gt;We've seen simple version of branching, when we check whether the user's input is &lt;code&gt;Even or Odd&lt;/code&gt;. &lt;br&gt;
The syntax was,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;#some logic
&lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;#some other logic
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We used &lt;code&gt;==&lt;/code&gt; to check whether remainder is 0 when divided by 2. When &lt;code&gt;True&lt;/code&gt; we know exactly it's &lt;code&gt;Even number&lt;/code&gt; else it's &lt;code&gt;Odd number&lt;/code&gt;&lt;br&gt;
&lt;code&gt;==&lt;/code&gt; is the comparison operator since &lt;code&gt;=&lt;/code&gt; was reserved for  an assignment operator. &lt;br&gt;
Indentation is important, It define the block of codes. From the syntax above, &lt;code&gt;some logic&lt;/code&gt; belong to the body of &lt;code&gt;if condition&lt;/code&gt; while &lt;code&gt;some other logic&lt;/code&gt; belong to &lt;code&gt;else scope&lt;/code&gt;. More on scopes later.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Comparison operators&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;==&lt;/code&gt; equal&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;!=&lt;/code&gt; not equal&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;&lt;/code&gt; less than&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;=&lt;/code&gt; less than or equal to&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;gt;&lt;/code&gt; greater than&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;gt;=&lt;/code&gt; greater or equal to&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Can we now determine whether our integer input is neutral ( 0 ), negative ( less than 0 ) or positive ( greater than 0 )?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;pseudo code&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Ask user for an integer input&lt;/li&gt;
&lt;li&gt;Cast/Convert the input into integer by passing it into &lt;code&gt;int function&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Check if input is equal to 0, print 'neutral' into the console&lt;/li&gt;
&lt;li&gt;Else if input is less than 0, print 'negative' into the console&lt;/li&gt;
&lt;li&gt;Else the input must be positive then print out 'positive' to the console&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;int_number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Enter an Integer number: "&lt;/span&gt; &lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;int_number&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'neutral'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;elif&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;int_number&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'negative'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'positive'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Exercise&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Write a program that take an integer number from user and print &lt;code&gt;Fizz&lt;/code&gt; when it's divisible by 3, print &lt;code&gt;Buzz&lt;/code&gt; when it's divisible by 5 and print &lt;code&gt;FizzBuzz&lt;/code&gt; when it's divisble by both 3 and 5.&lt;/p&gt;

&lt;p&gt;I know you must be exhausted. See you in &lt;strong&gt;Algorithms With Loops&lt;/strong&gt; &lt;br&gt;
Thanks for following the series.&lt;br&gt;
Don't forget to connect with me on  &lt;a href="https://twitter.com/DibbaseyFabala"&gt;Twitter&lt;/a&gt; and  &lt;a href="https://www.linkedin.com/in/fabala-dibbasey-b69a27207/"&gt;LinkedIn&lt;/a&gt; &lt;/p&gt;

</description>
    </item>
    <item>
      <title>Introduction to computer programming for coders</title>
      <dc:creator>Fabala Dibbasey</dc:creator>
      <pubDate>Sun, 29 Aug 2021 22:04:51 +0000</pubDate>
      <link>https://dev.to/fabaladibbasey/introduction-to-computer-programming-for-coders-1ga6</link>
      <guid>https://dev.to/fabaladibbasey/introduction-to-computer-programming-for-coders-1ga6</guid>
      <description>&lt;p&gt;&lt;strong&gt;WELCOME&lt;/strong&gt;&lt;br&gt;
In this series of lessons, I am going to take you from absolutely knowing nothing/less about programming/coding to been comfortable with instructing computers to do anything for you.&lt;/p&gt;
&lt;h2&gt;
  
  
  Computers?
&lt;/h2&gt;

&lt;p&gt;If you're reading this it means you already have a computer, Phone or PC/Laptop. Now can you answer the question, &lt;strong&gt;what are computers?&lt;/strong&gt;&lt;br&gt;
You're right! It's just the device you've with you.&lt;br&gt;
Let's discuss the device in detail. It's powered by electricity, it's capable of taking in data (input), can keep the data (storage), processing the data (algorithms) and finally give out the information (output) and this is in fact the definition of computers.&lt;/p&gt;

&lt;p&gt;Today computers have reached to a point that human don't only compare them to us but they've declared that computers are smart/intelligent than us.&lt;br&gt;
Those who said that or think so doesn't understand computers.&lt;br&gt;
Computer are stupid even though they can store billions of data (for example 1.5 million books of standard size),  and can perform a billion of calculations per second! (two operations in same time light travels 1 foot).&lt;br&gt;
They existed because of us, they only do what we instructed them to do i.e garbage in, garbage out. This means even if we mistakenly instructed them they'll still carry out the instruction because they cannot realize our mistake.&lt;/p&gt;
&lt;h2&gt;
  
  
  Computer Science and Programming
&lt;/h2&gt;

&lt;p&gt;Computer science is fundamentally problem solving. And in this series of lessons, we'll focus on speaking to computers to do tasks for us. Speaking to computers? Ohh yes, Computers speak the language 0s, and 1s just like we human speaks in decimal base for instance, &lt;code&gt;2021&lt;/code&gt; in computer it's &lt;code&gt;11111100101&lt;/code&gt;. I did high school math to convert decimal to binary. Each digit in binary represent a bit.&lt;/p&gt;

&lt;p&gt;It make sense that computers can represent decimal in binary, What about texts?&lt;/p&gt;
&lt;h2&gt;
  
  
  Representation of texts in Computer
&lt;/h2&gt;

&lt;p&gt;People before us have decided base on a standard to map each character (capital and small letters, punctuation) to a number. for instance, &lt;code&gt;"A"&lt;/code&gt; ==&amp;gt; &lt;code&gt;65&lt;/code&gt;,  &lt;code&gt;"B"&lt;/code&gt; ==&amp;gt; &lt;code&gt;66&lt;/code&gt; and so on. Read more about  &lt;a href="https://en.wikipedia.org/wiki/ASCII" rel="noopener noreferrer"&gt; American Standard Code for Information Interchange.&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1630248627025%2FA7k_L2qsi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1630248627025%2FA7k_L2qsi.png" alt="ASCII-Table-wide.svg.png"&gt;&lt;/a&gt;&lt;br&gt;
 &lt;a href="https://commons.wikimedia.org/wiki/File:ASCII-Table-wide.svg" rel="noopener noreferrer"&gt;Image Source&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Receiving the text &lt;code&gt;"HELLO"&lt;/code&gt; ==&amp;gt; &lt;code&gt;"72", "69", "76", "76", "79"&lt;/code&gt; respectively. And in bits it's &lt;code&gt;01001000, 01000101, 01001100, 01001100, 01001100&lt;/code&gt;. Each letter is typically represented with a pattern of eight bits, or a &lt;strong&gt;byte&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
8 bits == byte&lt;/li&gt;
&lt;li&gt;
1024 bytes == kilobyte&lt;/li&gt;
&lt;li&gt;
1024 kilobytes == megabyte&lt;/li&gt;
&lt;li&gt;
1024 megabyte == gigabyte and so on&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;However some think it's &lt;code&gt;1000&lt;/code&gt; instead of &lt;code&gt;1024&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;There's another standard,  &lt;a href="https://en.wikipedia.org/wiki/Unicode" rel="noopener noreferrer"&gt;&lt;strong&gt;Unicode&lt;/strong&gt;&lt;/a&gt;  that is an extension to &lt;strong&gt;ASCII&lt;/strong&gt;. It contains all those characters like ancient symbols, emojis and many more that do not exist in &lt;strong&gt;ASCII&lt;/strong&gt; standard.&lt;/p&gt;

&lt;p&gt;Then how images, videos and audios are represented in computers? &lt;/p&gt;
&lt;h2&gt;
  
  
  Images
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1630252691378%2FjUWisp5RL.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1630252691378%2FjUWisp5RL.jpeg" alt="newProfile.jpg"&gt;&lt;/a&gt;&lt;br&gt;
I don't know if you can observe that images are just thousands or millions of square boxes called pixels combined.&lt;br&gt;
We can achieve pixels by zooming the image 2000+% like the image shown below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1630254000875%2FO0xJz20e2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1630254000875%2FO0xJz20e2.png" alt="zoomedImage.png"&gt;&lt;/a&gt;&lt;br&gt;
and those square boxes or pixel are nothing but colors. So our concern should be how computers represent colors? &lt;br&gt;
There were many system of representing colors but the most commonly use is &lt;strong&gt;RGB&lt;/strong&gt;. Some amount of red, green and blue can be combine to produce millions of colors. for example, &lt;code&gt;rgb(23, 116, 223)&lt;/code&gt; will produce the color below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1630254565152%2F7pdXBDldW.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1630254565152%2F7pdXBDldW.png" alt="pixel.png"&gt;&lt;/a&gt;&lt;br&gt;
As you can see, there's numbers mapping to colors. &lt;code&gt;rgb ==&amp;gt; 23, 116, 223&lt;/code&gt;.&lt;br&gt;
The resolution of an image is the number of pixels there are, horizontally and vertically, so a high-resolution image will have more pixels and require more bytes to be stored.&lt;/p&gt;
&lt;h2&gt;
  
  
  Videos and Audios
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Videos: If we break videos down, we'll see it contains thousands of images flipping per second to create the motion as in this &lt;a href="https://www.youtube.com/watch?v=p3q9MM__h-M" rel="noopener noreferrer"&gt;Youtube video, flipping&lt;/a&gt; &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Audios uses the concept of sound wave to map sound frequency to numbers.  Read more about &lt;a href="https://teachcomputerscience.com/sound-representation/" rel="noopener noreferrer"&gt;sound representation in computers&lt;/a&gt; &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Programming Languages
&lt;/h2&gt;

&lt;p&gt;Earlier I said that computers only understand &lt;code&gt;0s and 1s&lt;/code&gt; but imagine if we are to communicate with computer to build a complex system or application like Twitter with &lt;code&gt;0s and 1s&lt;/code&gt; it will seems impossible not alone &lt;strong&gt;HARD&lt;/strong&gt;. The same humans before us has created a languages which are more human friendly. Today, we've more than 500 hundred programming languages. We can use those languages to instruct computers. The &lt;code&gt;Code&lt;/code&gt; we write in those languages will be compiled/interpreted to &lt;code&gt;Machine code&lt;/code&gt;, a codes which computers can understand.&lt;/p&gt;

&lt;p&gt;Out of 500 plus programming languages, ** What is the best programming language? **&lt;br&gt;
There is no such the best programming language. Each have their strengths and weaknesses and there are Domain specific languages, highly specialized mini-programming languages for example &lt;code&gt;regular expressions&lt;/code&gt; for string manipulation, and &lt;code&gt;SQL&lt;/code&gt; for database manipulation.&lt;br&gt;
&lt;strong&gt;Whatever can be compute in one programming language can be compute in other programming language.&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Computers vs Human being
&lt;/h2&gt;

&lt;p&gt;Let's say you're given a Phone Book which consist of the list of the following names,&lt;/p&gt;

&lt;p&gt;&lt;code&gt;[ Omar, Mamudo, Amie, Fabala, Muhammed  ]&lt;/code&gt;&lt;br&gt;
Assuming each name on the list is on a page and page start from 1.&lt;br&gt;
You can tell a human being to find the name, &lt;code&gt;Fabala&lt;/code&gt; and you're not going to tell him/her how to go about in retrieving the name, &lt;code&gt;Fabala&lt;/code&gt; from the phone book.&lt;br&gt;
S/He will have to find an approach to check if &lt;code&gt;Fabala&lt;/code&gt; is on the Phone Book? and if yes will tell us at which page. This very human being can start searching from left/right of the page and turning one page at a time. This is because human being understand &lt;code&gt;Declarative Knowledge&lt;/code&gt;. &lt;code&gt;Declarative knowledge&lt;/code&gt; is a statement of fact like find me the name, "Fabala" from the Phone Book.&lt;/p&gt;

&lt;p&gt;Whereas, Computers understand &lt;code&gt;Imperative Knowledge&lt;/code&gt;, a recipe or how to knowledge. If the same problem, finding "Fabala" from the phone book given to Computer, you need to tell it how to go about in finding the name, "Fabala".&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Instructing Computers to find "Fabala" from the phone Book&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Pick up the Phone Book&lt;/li&gt;
&lt;li&gt;Start from the first page on left side of the Phone Book&lt;/li&gt;
&lt;li&gt;Check if name on the page is "Fabala", tell us the page number and stop searching&lt;/li&gt;
&lt;li&gt;Else, Go to the next page and go back to step &lt;strong&gt;3&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;When no page is left to be search, tell us "Fabala" doesn't exist in the Phone Book &lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

&lt;p&gt;The steps/approaches we took to find the name is what we called an &lt;strong&gt;Algorithms&lt;/strong&gt;, step-by-step instructions for solving problems.&lt;br&gt;
What we did above is representation of our algorithms in precise English and that is what is refer to as &lt;code&gt;Pseudocode&lt;/code&gt;, representing algorithms in Human Languages.&lt;/p&gt;
&lt;h3&gt;
  
  
  Actual code
&lt;/h3&gt;

&lt;p&gt;I do not expect you to fully understand the code below. I just want you to see how &lt;code&gt;Actual code&lt;/code&gt; is different from the &lt;code&gt;Pseudocode&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;phone_book&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Omar&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Mamudo&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Amie&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Fabala&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Muhammed&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;  &lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;find_name&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;phone_book&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sh"&gt;'''&lt;/span&gt;&lt;span class="s"&gt;
    input: phone book consisting of names, and the name to be search
    output: return the page number of the name if it exist
    else return &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Not existed&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;

    &lt;/span&gt;&lt;span class="sh"&gt;'''&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;phone_book&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;phone_book&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Page &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;page&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Not existed&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;find_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;phone_book&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Fabala&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code above is written in &lt;strong&gt;Python Programming Language&lt;/strong&gt; which we're going to use throughout this series to represent our logic but we're going to focus more on problem solving (Programming) than the coding part of it.&lt;/p&gt;

&lt;p&gt;Thanks for reading and see you in the next topic, &lt;a href="https://dev.to/fabaladibbasey/algorithms-with-python-variables-and-conditions-3733"&gt;&lt;strong&gt;Algorithms&lt;/strong&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Don't forget to like, share with those it will help and finally lets connect on  &lt;a href="https://twitter.com/DibbaseyFabala" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; and  &lt;a href="https://www.linkedin.com/in/fabala-dibbasey-b69a27207/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; &lt;/p&gt;

</description>
      <category>beginners</category>
      <category>programming</category>
      <category>codenewbie</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>Learn to manipulate HTML DOM in JavaScript by building To Do List App</title>
      <dc:creator>Fabala Dibbasey</dc:creator>
      <pubDate>Wed, 28 Jul 2021 07:03:21 +0000</pubDate>
      <link>https://dev.to/fabaladibbasey/learn-to-manipulate-html-dom-in-javascript-by-building-to-do-list-app-3017</link>
      <guid>https://dev.to/fabaladibbasey/learn-to-manipulate-html-dom-in-javascript-by-building-to-do-list-app-3017</guid>
      <description>&lt;h2&gt;
  
  
  Let's learn to manipulate HTML document object model (DOM) in JavaScript by building To Do List App
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mEJCD5Z4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1627080009579/gFD_4bgci.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mEJCD5Z4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1627080009579/gFD_4bgci.png" alt="toDoListApp.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Goal
&lt;/h3&gt;

&lt;p&gt;At the end of this project you'll have proficient capability of working with HTML element right inside JavaScript, you'll be able to add, remove, style HTML elements dynamically and be able store elements in your browser using web storage API&lt;/p&gt;

&lt;h3&gt;
  
  
  Topics covered
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
Create HTML elements inside JavaScript&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Selecting &amp;amp;&amp;amp; Manipulating the DOM elements&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Template literals (plus createRange() and createContextualFragment() )&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;DOM traversal&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Events (raising and handling events)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;ES6 syntax&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Array methods&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Web storage API (local storage, session storage and cookies)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Prerequisite
&lt;/h3&gt;

&lt;p&gt;I expect you to know basic HTML, CSS  and JavaScript&lt;/p&gt;

&lt;h3&gt;
  
  
  DOM
&lt;/h3&gt;

&lt;p&gt;What's DOM?&lt;br&gt;
The Document Object Model (DOM) is the data representation of the objects that comprise the structure and content of a document on the web. It defines HTML elements as objects, Properties for all HTML elements, Methods for all HTML elements, and Events for all HTML elements.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--R3m5Mvmt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1627212470527/vDW_lY1J4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--R3m5Mvmt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1627212470527/vDW_lY1J4.png" alt="DOM.png"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Note
&lt;/h4&gt;

&lt;p&gt;The browser model the structure after receiving HTTP response from the server!&lt;br&gt;
It represents the page so that programs ( eg. JavaScript) can change the document structure, style, and content. &lt;/p&gt;

&lt;p&gt;We'll discuss more about the tree structure of the DOM which shows the parent child relationship of nodes &lt;strong&gt;in DOM traversal section&lt;/strong&gt;. For now we'll need the first node of the tree (root node) called &lt;strong&gt;document&lt;/strong&gt; to access any object in an HTML page.&lt;/p&gt;
&lt;h3&gt;
  
  
  Changing document structure, style and content
&lt;/h3&gt;

&lt;p&gt;We said that DOM defines Methods for all HTML elements and we can use one of it's method, createElement(specify the element) to create any specified HTML element&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;heading&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;h1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;h1&amp;gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To create a text, we can use the method, createTextNode(text)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;heading_text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createTextNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello world!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why createTextNode instead of createText as in createElement?&lt;/p&gt;

&lt;h4&gt;
  
  
  Node
&lt;/h4&gt;

&lt;p&gt;Every object located within a document is a node of some kind. In an HTML document, an object can be an element node but also a text node or attribute node.&lt;/p&gt;

&lt;p&gt;lets now append the textNode, heading_text we've created into our heading element, h1 using appendChild() method&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;heading&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;heading_text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;h1&amp;gt;Hello world!&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Recall the structure of HTML&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Title&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
 &lt;span class="c"&gt;&amp;lt;!-- Any visible contents --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt; element defines the document's body, and is a container for all the visible contents, such as headings,&lt;br&gt;
To make our heading element visible we need to append it to &lt;br&gt;
 &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt; element&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;heading&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="cm"&gt;/*&amp;lt;body&amp;gt;
  &amp;lt;h1&amp;gt;Hello world!&amp;lt;/h1&amp;gt;
  &amp;lt;/body&amp;gt;
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Db2qST-q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1627229169108/U79ax7aUA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Db2qST-q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1627229169108/U79ax7aUA.png" alt="h1Element.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  exercise
&lt;/h3&gt;

&lt;p&gt;Using createElement(), createTextNode(), and appendChild()&lt;br&gt;
Make the following HTML snippet codes below and append it to body element. try it yourself before viewing my solution&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt; &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Now I can create HTML element right inside JavaScript&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  solution
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;div&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;paragraph&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;p&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;paragraphText&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createTextNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Now I can create HTML element right inside javaScript&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;paragraph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;paragraph&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;paragraph&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Element.append()&lt;/code&gt; is similar to &lt;code&gt;Node.appendChild()&lt;/code&gt; but with extra power. Let me show you!&lt;br&gt;
let's remake our previous solution with &lt;code&gt;Element.append()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;div&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;paragraph&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;p&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;paragraph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Now I can create HTML element right inside JavaScript&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;paragraph&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This time with fewer codes! &lt;code&gt;Element.append()&lt;/code&gt; saves us from creating text node and appending it to the paragraph as in the first solution.&lt;/p&gt;

&lt;p&gt;Ohh! thank you &lt;code&gt;Element.append()&lt;/code&gt;, now I will never use &lt;code&gt;Node.appendChild()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Smart learner, you jumped to conclusion so soon! &lt;/p&gt;

&lt;p&gt;&lt;code&gt;Element.append()&lt;/code&gt;  has no return value, whereas &lt;code&gt;Node.appendChild()&lt;/code&gt; returns the appended Node object.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SRT1wuI1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1627255835371/hIxk7a11ly.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SRT1wuI1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1627255835371/hIxk7a11ly.png" alt="append&amp;amp;&amp;amp;appendChild.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Maybe this time &lt;code&gt;Node.appendChild()&lt;/code&gt; won in a case where you want your appended Node object.&lt;/p&gt;

&lt;p&gt;Another battles, &lt;code&gt;Element.append()&lt;/code&gt; vs &lt;code&gt;Node.appendChild()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Element.append()&lt;/code&gt; allows you to also append DOMString objects, whereas &lt;code&gt;Node.appendChild()&lt;/code&gt; only accepts Node objects.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bzKCUBGh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1627257207688/jl9unMr4s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bzKCUBGh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1627257207688/jl9unMr4s.png" alt="node&amp;amp;&amp;amp;domString.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Element.append()&lt;/code&gt; can append several nodes and strings, whereas &lt;code&gt;Node.appendChild()&lt;/code&gt; can only append one node.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--A3WZPHhU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1627259156277/bfjqY9l3I.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--A3WZPHhU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1627259156277/bfjqY9l3I.png" alt="multipleNodes.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  exercise
&lt;/h3&gt;

&lt;p&gt;create the following elements structure inside JavaScript from what you've learned so far&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;I am learning DOM&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
  This is so cool
  &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;with my new power i can now create html structure inside javascript, ohh how cool is that&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;h3&amp;gt;&lt;/span&gt;this is heading three inside second div&lt;span class="nt"&gt;&amp;lt;/h3&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;intresting, this is second paragraph in second div&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Solution
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;heading1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;h1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//&amp;lt;h1&amp;gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;span class="nx"&gt;heading1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I am learning DOM&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//&amp;lt;h1&amp;gt; I am learning DOM &amp;lt;/h1&amp;gt;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;div1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;div&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//&amp;lt;div&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;p1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;p&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;with my new power i can now create html structure inside javascript, ohh how cool is that&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;div1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;This is so cool&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;div2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;div&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;heading3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;h3&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//&amp;lt;h3&amp;gt;&amp;lt;/h3&amp;gt;&lt;/span&gt;

&lt;span class="nx"&gt;heading3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;this is heading three inside second div&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;//&amp;lt;h3&amp;gt;this is heading three inside second div&amp;lt;/h3&amp;gt;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;p2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;p&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;p2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;intresting, this is second paragraph in second div&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nx"&gt;div2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;heading3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;p2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;heading1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;div1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;div2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ohh right, so far all what we did is to create HTML elements from JavaScript and insert it to our &lt;code&gt;&amp;lt;body&amp;gt;&amp;lt;/body&amp;gt;&lt;/code&gt; element for visibility. What if we want to get access to a specific element from HTML, index.html file and manipulate it in JavaScript?&lt;/p&gt;

&lt;h3&gt;
  
  
  Selecting &amp;amp;&amp;amp; Manipulating the DOM elements
&lt;/h3&gt;

&lt;p&gt;Well, We can use selectors as it's done in style sheet.&lt;/p&gt;

&lt;p&gt;That's it for this part, I can't wait to see you in the next part, &lt;strong&gt;Selecting &amp;amp;&amp;amp; Manipulating the DOM elements&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I'll love us to connect on  &lt;a href="https://twitter.com/DibbaseyFabala"&gt;Twitter&lt;/a&gt; and  &lt;a href="https://www.linkedin.com/in/fabala-dibbasey-b69a27207/"&gt;LinkedIn&lt;/a&gt;  &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>codenewbie</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Data structures and algorithms __Evaluating Program Efficiency and sorting</title>
      <dc:creator>Fabala Dibbasey</dc:creator>
      <pubDate>Sun, 18 Apr 2021 19:05:43 +0000</pubDate>
      <link>https://dev.to/fabaladibbasey/data-structures-and-algorithms-part-2time-complexity-and-sorting-52b1</link>
      <guid>https://dev.to/fabaladibbasey/data-structures-and-algorithms-part-2time-complexity-and-sorting-52b1</guid>
      <description>&lt;h1&gt;
  
  
  &lt;strong&gt;Data structures and algorithms walk through in java&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;Part 2, &lt;br&gt;
In &lt;a href="https://dev.to/fabaladibbasey/data-structures-and-algorithms-walk-through-part-1-linear-and-binary-algorithm-27g1"&gt;part one&lt;/a&gt; of this series we introduce search algorithms, Linear and Binary search of input set and finalize that Binary search is a better approach but can only be use on sorted array and that had introduced us to a discussion of a new topic, Sorting algorithms and that’s exactly what we’re going to cover in this part 2 of the series. If you missed part 1 you &lt;a href="https://dev.to/fabaladibbasey/data-structures-and-algorithms-walk-through-part-1-linear-and-binary-algorithm-27g1"&gt;can check it here&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;GOALS:&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
    • To be able to know the different ways of determining the run time complexity of a program (timing, counting operations and big ohh)&lt;br&gt;
    • why big ohh&lt;br&gt;
    • be able to form an equation from any given program and express it in asymptotic notation&lt;br&gt;
    • To be able to write selection sort in no time&lt;/p&gt;

&lt;p&gt;Note:&lt;br&gt;
It contains exercises. If you’re learning, you’re encourage to do the exercises.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“You only get it by doing and not by watching”__Fabala Dibbasey&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  Quick review:
&lt;/h2&gt;

&lt;p&gt;In part 1 we’ve seen something like O(1), O(n), O(log n)! But what really are they? Big Ohh notation, exactly you’re right and what’s big ohh notation? Maybe google can help us. &lt;br&gt;
Big ohh notation is use to express the growth of program’s run time as&lt;br&gt;
input size grows. It does not need to be precise, it’s uses order of and not exact. It measures an upper bound on the asymptotic growth, aka order of growth.&lt;/p&gt;
&lt;h4&gt;
  
  
  Our Goal for measuring program Efficiency:
&lt;/h4&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;• To evaluate algorithms
• To evaluate scalability
• To evaluate in terms of input size
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h4&gt;
  
  
  Why Big Ohh?
&lt;/h4&gt;

&lt;p&gt;Surely, we can use different approaches such as timing and counting operations to measure the efficiency of a program. But why Big Ohh?&lt;br&gt;
We’re going to use these three different approaches to measuring program efficiency on the sample program below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="nf"&gt;evaluateEfficiency&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;inputSize&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;inputSize&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Timing:
&lt;/h4&gt;

&lt;p&gt;We can use stop watch to time our program. &lt;a href="https://stackoverflow.com/questions/8255738/is-there-a-stopwatch-in-java"&gt;Learn to use stop watch in java here&lt;/a&gt;.&lt;br&gt;
// call the method in your main method with different inputSize.&lt;/p&gt;

&lt;p&gt;startTime&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;evaluateEfficiency&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;30000000&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;endTime&lt;br&gt;
timeTaken = endTime – startTime&lt;/p&gt;

&lt;p&gt;We noticed that time varies with different inputSize, It increases as inputSize increases. Good!&lt;br&gt;
What else can you notice?&lt;br&gt;
If nothing, not a big deal! But believe me if you’re using another machine different from mine you’re likely to have less timeTaken for every the same inputSize we passed in. For example, if we both pass in  30000000 into our method it took 52 second to complete in my machine. If it took less second than that in your machine is exactly what I expected else hahahah! I laughed at you because my machine is better than yours, Maybe not! The point here is,&lt;br&gt;
Time varies in different Machines. Bad!&lt;/p&gt;

&lt;p&gt;Implement the same program using recursion and time it?&lt;/p&gt;

&lt;p&gt;There will be time different with the same input. Hint: use milliseconds instead of seconds.&lt;br&gt;
That’s to say time varies with different Implementations of the same program, another limitation of timing to measures the  program efficiency.&lt;/p&gt;
&lt;h4&gt;
  
  
  Limitation of timing approach for measuring program efficiency:
&lt;/h4&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    ◦ time varies with different machines or computers
    ◦ time varies with different implementations
    ◦ time cannot express the relation between input and time
    ◦ time is not predicable base on small input
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Due to these limitations timing is discourage. Now lets see Counting Operations to measuring program efficiency. &lt;/p&gt;
&lt;h4&gt;
  
  
  Counting Operations:
&lt;/h4&gt;

&lt;p&gt;let us assumes that every operation is 1.&lt;br&gt;
Operations are + - * / % =  == &amp;lt; &amp;lt;= &amp;gt; &amp;gt;=&lt;br&gt;
Note: we’ll also assume that System.out.println() and return  is 1 operation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="nf"&gt;evaluateEfficiency&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;inputSize&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;inputSize&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;


    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;from the method, evaluateEfficiency:&lt;br&gt;
before for loop:&lt;br&gt;
    long sum = 0; 1 Operation&lt;br&gt;
for loop:&lt;br&gt;
    int i = 0; 1 Operation&lt;br&gt;
    i &amp;lt;= inputSize; 1 Operation&lt;br&gt;
    i++ 2 Operations, adding and assigning  (i.e i = i + 1) &lt;br&gt;
    Inside for loop,  sum += i; is  2 Operations, adding and assigning  (i.e sum = sum + i) &lt;br&gt;
    All these Operations in for loop will be carry out n times where n is the inputSize.&lt;br&gt;
    Total Operation  1 + 1 + 2 + 2 = 6 for n times aka 6n&lt;br&gt;
Outside for loop:&lt;br&gt;
    return sum; 1 Operation&lt;/p&gt;
&lt;h4&gt;
  
  
  Overall Operations:
&lt;/h4&gt;

&lt;p&gt;operations before for loop + operations in for loop + operations after for loop &lt;br&gt;
1 + 6n + 1 and that’s the total operation of our program. &lt;br&gt;
Ohh Yes! Counting is better because:&lt;br&gt;
    • It depends on algorithms&lt;br&gt;
    • The same for any computer and &lt;br&gt;
    • It varies between different inputSize and can express the relationship between the input and count. &lt;br&gt;
but still&lt;br&gt;
    • there’s no real definition of which operations to count&lt;br&gt;
    • varies for different implementations. Help me prov that please. Count the operations in the recursive implementation of the method, evaluateEfficiency(int inputSize). If you have not implement the recursive version of this method please just do that right now and count the operations in it. &lt;/p&gt;

&lt;p&gt;We still need better way for measuring program efficiency, an approach which meet all of Our Goal for measuring program Efficiency,  we specified above.&lt;/p&gt;
&lt;h4&gt;
  
  
  Big Ohh Notation:
&lt;/h4&gt;

&lt;p&gt;Unless you jumped straight to this section, big ohh is not new to us anymore!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="nf"&gt;evaluateEfficiency&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;inputSize&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;inputSize&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Recalled we count this method under Counting Operations  and this 1 + 6n + 1  was our equation.&lt;br&gt;
Big Ohh notation which measures upper bound on the asymptotic growth. We said it does not need be exact but on the order of growth.&lt;br&gt;
Ignores the additive and multiplicative constants and focuses on dominant terms.&lt;br&gt;
  O(n)  Linear time complexity &lt;br&gt;
More examples:&lt;br&gt;
n^2 + 2n + 2 ignores both additive and multiplicative constants. Will be left with n^2  and n. n^2 dominate hence   O(n^2)   Quadratic time complexity&lt;br&gt;&lt;br&gt;
n^2 + 99999n + 72000  :   O(n^2) ==&amp;gt; Quadratic time complexity &lt;br&gt;
log(n) + n + 100 :     O(n) ==&amp;gt; Linear time complexity&lt;br&gt;&lt;br&gt;
3*n*log(n) + 10n :   O(n log n) ==&amp;gt; Log Linear or Linearithmetic time complexity &lt;br&gt;
5n^30 + 2^n :  O(2^n) ==&amp;gt; Exponential time complexity&lt;br&gt;
434 + 600: O(1) ==&amp;gt;  Constant time complexity&lt;/p&gt;

&lt;p&gt;Hope you get the idea we ignores additive and multiplicative constants and focuses on dominant terms&lt;/p&gt;
&lt;h4&gt;
  
  
  Exercise:
&lt;/h4&gt;

&lt;p&gt;Arrange the running time complexity of these asymptotic notations above in ascending order (Best to Worst without repeating any notation) and comment your answer.&lt;/p&gt;
&lt;h4&gt;
  
  
  Big ohh notation:
&lt;/h4&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;• describe the worse case which is the bottle neck when program runs
• evaluate algorithms
• independent of machines
• independent of implementations
• can express the rate of growth of a program relative to input size
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;We’ve done a lot! Let’s take 3 minutes break here:&lt;/p&gt;

&lt;p&gt;Welcome back!&lt;/p&gt;
&lt;h4&gt;
  
  
  Sorting algorithms:
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt; &lt;span class="o"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Suppose I asked you to sort the given array, list above how will you go about it?&lt;br&gt;
Let me Guess! You can rate my guessing in the comment please.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You will first try to find the smallest elements in the list.&lt;/li&gt;
&lt;li&gt;You’ll either swap it with the first element in the list or move your smallest element to a new array lets call it mySortedArray&lt;/li&gt;
&lt;li&gt;You’ll repeat yourself until you’re done.
ohhhh hope I make good guesses?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let’s translate the same thing into java code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="nf"&gt;selectionSort&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="c1"&gt;//We're going to say our smallest element is at the first     index in the list because that's the element we've seen so far                                                          &lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;smallest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
    &lt;span class="c1"&gt;// search for smaller element &lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="c1"&gt;// yes                      &lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;smallest&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                    &lt;span class="c1"&gt;// swap         &lt;/span&gt;
                    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
                    &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
                    &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
                    &lt;span class="n"&gt;smallest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
                &lt;span class="o"&gt;}&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;


&lt;span class="cm"&gt;/*At the end of this inner for loop, smallest element will be 
 *positioned at index i of the outer for loop then i will be 
 *incremented. Inner for loop will start it's searching at i 
 *that's to say it's ignoring all elements at index less than 
 *index i.
 */&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;                     
    &lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two more Exercises  for you&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Count the operations for this method, selectionSort and from the equation determine the Big ohh notation (You should get O(n^2)  Quadratic time complexity ).&lt;/li&gt;
&lt;li&gt;Implement the same algorithm finding the smallest element and move it to new sorted array to be return.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you’ve done these two exercises Congratulations!&lt;br&gt;
Now, can you tell me the differences between the one you implemented and the one we implemented together?&lt;br&gt;
The one we implemented together is mutating the original array, It’s called impure function in functional programming. And while the one you implemented yourself is the pure function in functional programming terms because it return new array (this case sorted version) instead of changing the state of the original array that was passed into it.&lt;/p&gt;

&lt;p&gt;I don’t know of you but I’m tired and I’m going terminate my recursion hahaaaa!&lt;/p&gt;

&lt;p&gt;Next, part 3:&lt;br&gt;
    • Bubble sort&lt;br&gt;
    • Bogosort&lt;br&gt;
    • merge sort&lt;/p&gt;

&lt;p&gt;Remember the topics of the series:&lt;br&gt;
    • Searching and Sorting Algorithms&lt;br&gt;
    • Data Structures (Stacks, Queues, Linked list, Graphs and more).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.linkedin.com/posts/fabala-dibbasey-b69a27207_data-structures-and-algorithmsevaluatingprogram-activity-6789728991914860544-OSVg"&gt;Want the pdf version&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Author Fabala Dibbasey&lt;br&gt;
&lt;a href="https://twitter.com/DibbaseyFabala"&gt;at Twitter&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.linkedin.com/in/fabala-dibbasey-b69a27207/"&gt;at LinkedIn&lt;/a&gt;&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>sorting</category>
      <category>java</category>
      <category>bigo</category>
    </item>
    <item>
      <title>Data Structures and Algorithms walk through part 1(linear and binary algorithm)</title>
      <dc:creator>Fabala Dibbasey</dc:creator>
      <pubDate>Fri, 02 Apr 2021 08:17:01 +0000</pubDate>
      <link>https://dev.to/fabaladibbasey/data-structures-and-algorithms-walk-through-part-1-linear-and-binary-algorithm-27g1</link>
      <guid>https://dev.to/fabaladibbasey/data-structures-and-algorithms-walk-through-part-1-linear-and-binary-algorithm-27g1</guid>
      <description>&lt;h1&gt;
  
  
  Data Structure and Algorithms
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Goal:&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;At the end of this article you should be able to:&lt;br&gt;
 understand and be able to implement both linear and binary search algorithms using both recursive and            iterative version,&lt;br&gt;
 understand the time complexity associated with each of them and the differences between them.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;We're going to implement our first algorithm on search method called Binary Search method; and in fact if you're at this level am sure you've already written your first algorithm, you might not realize it but you've.&lt;/p&gt;

&lt;p&gt;Scenario:&lt;br&gt;
//declaring and assigning an array&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Question:&lt;/strong&gt;&lt;br&gt;
Find me the index of number 5 from the given array, arr above?&lt;br&gt;
As human being, you might start from the either end, left or right and sequential moving to retrieve number 5 while keeping track of index. You’re doing an algorithm called Linear Algorithm.&lt;/p&gt;

&lt;p&gt;You can achieve the same Algorithm using loops; and I’m going to use for loop for this demo.&lt;br&gt;
Let’s create a method call linearSearch below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;linearSearch&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="cm"&gt;/*
         * takes int array and int value
         * return the index of the value if found 
         * otherwise -1 indicating not found
         */&lt;/span&gt;

    &lt;span class="c1"&gt;//declare current element&lt;/span&gt;

    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;currEl&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Assigning current element&lt;/span&gt;
        &lt;span class="n"&gt;currEl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
        &lt;span class="c1"&gt;//checking whether we found value to return its      index &lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;currEl&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="cm"&gt;/*
     * after searched through the entire array, list         * return not found index, -1
     */&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
     &lt;span class="o"&gt;}&lt;/span&gt;  
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That was linearSearch method. We started searching from the left of the array and sequentially looking for each element in the array to check whether it’s the value we’re looking for, if it’s we return its index otherwise is not found in the array after we’ve checked all the element in the array, so we return -1 indicating that is not found.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Analyzing the algorithm complexity associated with this method&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quiz:&lt;/strong&gt;&lt;br&gt;
let N = the number of steps needed&lt;br&gt;
Remember arr is the variable of our array above&lt;/p&gt;

&lt;p&gt;How many N steps will it take us to find the first element, 1 in the array, arr? 1 steps, you got it!&lt;br&gt;
    This is what we called best case scenario&lt;br&gt;
    no matter the length of an array, it will  always take us one step to find the first element in an array&lt;br&gt;
    In Big Ohh notation its  O(1), constant time operation&lt;br&gt;
How many N steps will it take us to find an element which does not exist in an array, arr? 10 steps, Correct!&lt;br&gt;
    Because we’ve to search the entire array, arr which will took us arr.length (10)  times before    returning not found, -1&lt;br&gt;
How many N steps will be needed if an elements that does not exist in arr, if arr increases to N numbers of elements?  N steps, Brilliant&lt;br&gt;
    In Big Ohh notation, it’s represented as O(N), linear time complexity&lt;/p&gt;

&lt;p&gt;Better way to Search an element in an array!&lt;/p&gt;

&lt;p&gt;We can do better search, in fact an algorithm called Binary Search.&lt;br&gt;
Binary search algorithm reduces the problem by halve in very look up!&lt;br&gt;
You don’t get that, that’s cool, I don’t too. Just kidding, However do me a favor, smile! Great let’s continue.&lt;/p&gt;

&lt;p&gt;Let’s bring  down our array above&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At first when I asked you to find me the index of element 5 from the array, arr you started from one end maybe left and was going sequentially to find the index of element 5. That approach was great but expensive compared to this, binary search approach aka divide and conquer algorithm. &lt;br&gt;
This time I want you to start your search from the middle, that’s Math.floor(arr.length / 2) I’m using Math.floor(float) to round down the value in case of floating figure; and don’t worry if you’re assigning arr.length / 2 to an int variable that would implicitly round it down for you. e.g int mid = (arr.length / 2);  &lt;/p&gt;

&lt;p&gt;While the  mid =  5;  arr[mid] = 6 and we’re looking for the element 5, NOTE: the mid is the index.&lt;/p&gt;

&lt;p&gt;Our first look up give us element 6 while we’re looking for an element 5, then what do we do?&lt;br&gt;
Straight in our mind, we should know that element 5 can never be found from the mid index and above because they're all greater than element 5;  Hence element 5 must be at some index below the mid index. This is interesting, let’s throw unnecessary right halve of the array. This is our sliced array below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt; &lt;span class="n"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;repeating the same step;  mid = (arr.length / 2);  arr[mid] = 3&lt;br&gt;
comparing element 3 with element 5, we know that element 5 is greater than element 3 hence it cannot be found from any index at mid and below. So let’s discard all elements form mid and below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt; &lt;span class="n"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;repeating the same approach; mid = (arr.length / 2);  arr[mid] = 5&lt;br&gt;
comparing arr[mid] which is element 5 with the element 5 we’re looking for, they’re the same so we should return the mid. Ahhh smile, you’ve found it and return your mid; However we ran into a bug called the index bug, not actually; Notice, this return index 1 because that’s where element 5 was found in  the recent array, arr. Whereas in the original/first array element 5 is at index 4, that is the correct index and that’s what we wanted to return. This approach is completely find if we’re just checking the existence of an element in an array; return true if found else false; but since we care about the index of the element we’re looking for, we should take tract of our indexes. That would required us two more variables, let’s call them low and high for mapping our low and high indexes respectively.&lt;/p&gt;

&lt;p&gt;Here’s the code of the concept we explained so far. Note this code below doesn’t care about the index.&lt;br&gt;
The function will return true if element found, false otherwise.&lt;br&gt;
I’m going to implement it recursively and I would like you to implement it using loop.&lt;/p&gt;

&lt;p&gt;Takeaway:  You only get it by doing and not by watching. Concepts matters but practical defeat theories.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;binarySearch_recursive&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;find&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; 
    &lt;span class="c1"&gt;// for an empty array we're done&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// our mid index&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;find&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nf"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;find&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;find&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// ignore the left halve&lt;/span&gt;
        &lt;span class="n"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;copyOfRange&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; 
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;binarySearch_recursive&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;find&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;// ignore the right halve&lt;/span&gt;
        &lt;span class="n"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;copyOfRange&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="c1"&gt;// slicing an array&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;binarySearch_recursive&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;find&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We’ve done a lot, I will encourage you take a break here at least 7 minutes and when you come back we’ll explore binary search that count indexes.&lt;/p&gt;

&lt;p&gt;Welcome back!&lt;br&gt;
Hopefully you’ve implemented your loop version of the above code, if you don’t I encourage you to go back and try it. Don’t just be reading, read and practice! If you tried and still don’t get it don’t hesitate to Google. In fact while writing this article I Googled “slicing an array in java” and stackoverflow.com was helpful. &lt;/p&gt;

&lt;p&gt;Enough talk, let’s code!&lt;/p&gt;

&lt;p&gt;This time around, I’m going to implement binary search method that return index of an element if found or -1 if not found. I’m going to use the loop version and you’re encourage to implement the recursive version of it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;binarySearchIteratively&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;high&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;low&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;    
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;high&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;low&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;low&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;high&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nf"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// search right&lt;/span&gt;
            &lt;span class="n"&gt;low&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;high&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;low&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="c1"&gt;// search left&lt;/span&gt;
            &lt;span class="n"&gt;high&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="err"&gt;–&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;high&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;low&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Brief explanation about the code&lt;br&gt;
We assigned:&lt;br&gt;
high to be the length of the array&lt;br&gt;
low to be the at index 0&lt;br&gt;
mid = (low + high) / 2 note because mid is an int variable there’s implicit floor division that safe us from using Math.floor(float)&lt;/p&gt;

&lt;p&gt;while loop will continue as long as low &amp;lt;= high and that’s very important. &lt;br&gt;
Inside while loop, we update low, high and mid depending on which portion to be search after comparing our key with a[mid].&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Run Time Complexity:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We have already explained this above or if you could observe the code, for every iteration we’re reducing the problem by halve meaning we throw away the unnecessary portion. I.e for the first iteration the problem is now N / 2, second iteration N / 4 or (N/2)/2 and so on.&lt;br&gt;
This in asymptotic notation aka Big Ohh is O(logN) , Logarithms complexity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Good to know:&lt;/strong&gt;&lt;br&gt;
Do you know that first binary search was published in 1946; and the first bug-free one was published in 1962.&lt;br&gt;
There was a bug in Java’s Arrays.binarySearch() and was discovered in 2006.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Linear Search vs Binary Search!&lt;/strong&gt;&lt;br&gt;
We know that Binary Search is a faster algorithm in term of time complexity however they can only be use on sorted array. That brought us to another topics, sorting algorithms, we’ll get there where we’ll implement quick sort, merge sort, selection sort, bubble sort and stupid/buggo sort.&lt;/p&gt;

&lt;p&gt;This is it for part 1 of our Data Structure and Algorithm walk through.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/fabaladibbasey/data-structures-and-algorithms-part-2time-complexity-and-sorting-52b1"&gt;You can Check out Part two here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Author&lt;/em&gt; Fabala Dibbasey&lt;br&gt;
&lt;a href="https://twitter.com/DibbaseyFabala"&gt;at Twitter&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.linkedin.com/in/fabala-dibbasey-b69a27207/"&gt;at LinkedIn&lt;/a&gt;&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>java</category>
    </item>
    <item>
      <title>Variables, var, const, and let in conversation</title>
      <dc:creator>Fabala Dibbasey</dc:creator>
      <pubDate>Thu, 18 Mar 2021 15:10:27 +0000</pubDate>
      <link>https://dev.to/fabaladibbasey/variables-const-let-and-var-3l60</link>
      <guid>https://dev.to/fabaladibbasey/variables-const-let-and-var-3l60</guid>
      <description>&lt;p&gt;Unveiling the Drama: Variables, const, let, and var in JavaScript—A Riveting Conversation!&lt;/p&gt;

&lt;h2&gt;
  
  
  Variables:
&lt;/h2&gt;

&lt;p&gt;Hey there! If you think I'm just a container, you're missing the big picture. Greetings, my components! Without you, my declaration is incomplete.&lt;/p&gt;

&lt;h2&gt;
  
  
  var:
&lt;/h2&gt;

&lt;p&gt;Hello &lt;em&gt;'const'&lt;/em&gt; and &lt;em&gt;'let'&lt;/em&gt;! Hey &lt;em&gt;'let,'&lt;/em&gt; you've been getting all the attention lately, but what makes you so special?&lt;/p&gt;

&lt;h2&gt;
  
  
  let:
&lt;/h2&gt;

&lt;p&gt;Umm, well...&lt;/p&gt;

&lt;h2&gt;
  
  
  const:
&lt;/h2&gt;

&lt;p&gt;Hold on, &lt;em&gt;'let'&lt;/em&gt;! Let me introduce myself first. I am the &lt;em&gt;'const'&lt;/em&gt; keyword, the guardian of constant variables. Once declared, there's no turning back.&lt;/p&gt;

&lt;h2&gt;
  
  
  let:
&lt;/h2&gt;

&lt;p&gt;I declare variables, and yeah, they can be reassigned everywhere in your program. I see all scopes, by the way.&lt;/p&gt;

&lt;h2&gt;
  
  
  var:
&lt;/h2&gt;

&lt;p&gt;Oh, &lt;em&gt;'let'&lt;/em&gt;, I only see function scope. How can we simplify things for developers?&lt;/p&gt;

&lt;h2&gt;
  
  
  let:
&lt;/h2&gt;

&lt;p&gt;Take a look at this demo:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;diffVarAndLet&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Extra scope&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Look, I'm in a scope&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="c1"&gt;// 'let' -&amp;gt; My life starts and ends only in this scope&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;See, I don't care about scope!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: See, I don't care about scope!&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Remember, I warned you about my life! Output: ReferenceError: x is not defined&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In short, I'm here to solve a problem you didn't even know existed—&lt;em&gt;scopes&lt;/em&gt;!&lt;/p&gt;

&lt;h2&gt;
  
  
  var:
&lt;/h2&gt;

&lt;p&gt;Excellent! Thank you. Together, we can elevate JavaScript. If you're new to JavaScript, go with 'let'—it rarely disappoints. Only the intermediates and experts truly understand when I'm needed.&lt;/p&gt;

&lt;h2&gt;
  
  
  const:
&lt;/h2&gt;

&lt;p&gt;Variable, you claim not to be a container—so what are you?&lt;/p&gt;

&lt;h2&gt;
  
  
  Variable:
&lt;/h2&gt;

&lt;p&gt;I'm used to mapping to a value in memory for reference purposes."&lt;/p&gt;

&lt;p&gt;Get ready for an exhilarating journey through the world of JavaScript variables! 🚀&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
