Wednesday, June 9, 2021

Avoiding mistakes

What are mistakes?

The computers don't make mistakes, but user can tell the computer what to do and cause mistakes!

Hence it is very important to learn how to make computer avoid mistakes.

Because the mistakes turn out into big blunder

  • as in banking process balance will be made  nil within second
  • as in medical instrument wrong reading makes wrong diagnosis and leads to severe problem for the patient
  • as in design software the building may collapse due to mistake in building design

The mistakes or bugs can cause a program to show unexpected results or sometimes make the computer to stop working!

Hey Computer:

Create my_number

Remember 10 as my_number

Repeat until my_number is less than 9

Add 1 to my_number

Display my_number

 

Output:

     Not responding…

The above sequence is not responding or hangs out as my_number will never be less than 9 and the program never display the output (and get into an infinite loop).

What is debugging?

The debugging is simply means finding and correcting the bugs. It is an important part of programming.

For example the following sequence is having bug:

Hey Computer:

Remember “Dog” as my_pet

Add “Cat” to my_pet

Display my pet

 

Output:

     ----No output----

The bug is in last instruction:

 Display my pet

There is no underscore between two words. Such mistakes are called syntax errors. Note that this type of errors is easily found out by just reading the instructions carefully. The process of reading instructions is called debugging.

Logic bug

When you press space bar, the text moves to the next line, it is called as logical bug.

Sometimes, programs have logic bug so the program doesn't crash but doesn't give the desired result.

For example observe the below sequence

Hey Computer:

Create my_age

Remember 30 as my_age

IF my_age is about 35

Display “Your offer 35%!”

IF my_age is below 25

Display “Your offer 25%!”

     Output:

          ---unpredictable---

Here no check for age between 25 and 35 is done. So it is hard to find the logic bugs like this. Finding the logic bugs is also called as a debugging.

While writing the sequence, there should be a condition to check for all scenarios.

Avoiding bug

When programs become longer and complex their instructions become harder to read.

How can we avoid mistakes and understand it better? The solution is to providing comments to understand the code better.

In other words comments and meaningful variable and function names make the code easier to debug and understand.

The programmer should carry a series of tests before releasing a program as there might be critical bugs in it.

 

Summary

The mistakes in programs are called as bugs.

 The debug is the process of finding bugs in a program.

The bugs are usually logic or syntax based one.

The bugs can be avoided by commenting, using proper names and testing the program.

 

PS: I have composed this post-series based on my pdf eBook on playstore:

Basics of Computer, Programming and C

The series is posted continuously one each next day, and reader can found any post by referring blogs’ time line tab.

--till next post, bye-bye and take care.

 

Tuesday, June 8, 2021

Grouping instructions

Similar instructions

Everyday routine tasks

  • brush teeth
  • take a bath
  • get dressed
  • do homework

These are a few activities that we do daily! These activities follow a similar set of sequence and repeatedly performed in similar ways.

Ever wonder if we could get them done in just one step?

Unfortunately a real life doesn't allow us a shortcut. But computer world does!

The computer programs as well contain parts that are repeated many times. For example, what happens when user copy and paste the text?

The copy and paste process involves remembering the text and pasting it whenever required.

Figure 2

Create a group of instructions

For the computer to bundle an activity or group a set of instructions, programmer need to tell the computer, what the procedure should be called and what it should do?

Hey Computer:

Remember as (copy and paste):

Remember the selected text and

     paste the selected text

Almost everything programmer tells the computer to do (such as even copy and paste) is actually a group of instructions which we also call predefined procedure.

Hey Computer:

Create a list of my_inspirations

Add “Mark Zuckerberg”, “Bill Gates”, and “Steve Jobs” to my_inspirations

Sort my_inspirations in alphabetical order

Display my_inspirations

 

Output:

     Bill Gates

     Mark Zuckerberg

     Steve Jobs

The above sequence shows how to create a list, sort it and display it too.

Now, let's make the computer remember sorting in alphabetical order and displaying it using stored group of instructions

Hey Computer:

Remember as sort_and_display:

     Sort my_inspirations in alphabetical order

     Display my_inspirations

Create a list of my_inspirations

Add “Mark Zuckerberg”, “Bill Gates”, and “Steve Jobs” to my_inspirations

     sort_and_display

 

Output:

     Bill Gates

     Mark Zuckerberg

     Steve Jobs

In the example, program has defined a sequence of steps called sort_and_display and reuses it when required (at the end).

Don't worry if the concept is little bit confusing. The doubt will be cleared in next part when actual program is written using programming language keywords.

Procedures and functions

The procedures may be used to remember steps and perform when required.

Similarly programmer can use functions when he wants to:

  • provide input to the procedures
  • display result from the procedures

In a nutshell, procedures perform task, while functions produce a result other than doing task.

Summary

The group of similar functions are written together and stored as procedures or functions.

The procedures and functions are used to make the program reusable and shorter in length.

The functions can display the result while procedures can only carry out the tasks.

PS: I have composed this post-series based on my pdf eBook on playstore:

Basics of Computer, Programming and C

The series is posted continuously one each next day, and reader can found any post by referring blogs’ time line tab.

--till next post, bye-bye and take care.

 

Monday, June 7, 2021

Creating lists

How to store multiple values

In the previous post we studied how to store values, but how do programmer store the multiple values? The solution is to create a list of values just like Tom’s mother creates grocery list before visiting any general store nearby!

How to prepare a sequence where multiple values are stored in a single variable!

For example, Tom is preparing a vegetables list for shopping:

Hey Computer:

Remember “Cucumber” as Vegetables

Remember “Lady Finger” as Vegetables

Remember “Carrot” as Vegetables

Display Vegetables

 

Output:

     Carrot

Tom got only carrot as a single (vegetable name) output. This is because Vegetables is a variable and it could store only single value at a time. Hence it replaced the other two and remembers only the carrot.

The correct sequence for Tom’s Vegetables list is as follows:

Hey Computer:

Create a list of my_vegetables

Remember “Cucumber” as my_vegetables

Remember “Lady Finger” as my_vegetables

Remember “Carrot” as my_vegetables

Display my_vegetables

 

Output:

     Cucumber

     Lady Finger

Carrot

Here instead of having three variables that store single vegetable, Tom can make a list that holds all the vegetables at one place.

Another example for lists is, suppose Tom wants to keep his best friends list at one place the correct sequence for that is as follows:

OK Computer[main()]:

Create a list of my_best_Friends

Remember “Dick” as my_best_Friends

Remember “Harry” as my_best_Friends

Remember “Johnny” as my_best_Friends

Display my_best_Friends

 

Output:

     Dick

     Harry

Johnny

In c language, if programmer wants to implement above task into his code form then below is the details:

main()

{

    char *my_best_Friends[] = {"Dick", "Harry","Johnny"};

     for (int i=0; i<3;i++)

        puts(my_best_Friends[i]);

}   

Output:

     Dick

     Harry

     Johnny

So, it is concluded that lists are great place to keep a set of related values.

Summary

The lists are used to store a set of related values. Items of the list can be stored, rearranged, replaced and removed. The programmer can perform tasks on each item of the list together at once.

PS: I have composed this post-series based on my pdf eBook on playstore:

Basics of Computer, Programming and C

The series is posted continuously one each next day, and reader can found any post by referring blogs’ time line tab.

--till next post, bye-bye and take care.

 

Sunday, June 6, 2021

Making decisions

Can computers make decisions for us? Note that computers can remember stuff, do repetitive tasks, but can't think their own? So programmer has to tell the computer what decisions to make!

The computers have to be told to take decisions based on conditions.

When

  • you are thirsty
  • your throat is feeling dry
  • you did not drink water for a long time

Then you should drink water.

When human meet certain conditions he takes some decision.

Using If… Then… Else

The computers use simple and implying words such as If and Then to guide their actions.

OK Computer [main()]:

IF time is After 8 PM

Then Display “Good Night!”

In c language, if programmer wants to implement above task into his code form then below is the details:

main()

{

    int currentTime = 7;

     //change 7 to 9, msg will be displayed

 

    if(currentTime > 8)    

        puts("Good Night");

}   

Output:

     [if currentTime = 7, nothing prints] 

     Good Night [if currentTime = 9,msg prints]

That was simple and pretty easy right?

The conditions help the computer to decide what actions to perform and when.

Latest try making decisions using sequences

OK Computer:

Remember marks is 50

IF marks is less than 35

Then Display “You have failed!”

          Else Display “You have passed!”

     Output:

          You have passed!

Now what if the condition is not met? In such situations programmers use the word Else.

OK Computer:

IF time is After 8 PM

Then Display “Good Night!”

          Else Display “Good Day!”

In c language, if programmer wants to implement above task into his code form then below is the details:

main()

{

    int currentTime = 7;

     //change 7 to 9, different msg will be displayed

 

    if(currentTime > 8)

        puts("Good Night");

    else

        puts("Good Day");

}   

Output:

     Good Day [if currentTime = 7]    

     Good Night [if currentTime = 9]  

This ability to perform different actions in different situations is what makes the computer look smart.

The conditions make the programs more intelligent.

Let’s take Tom’s example, in his cake shop Tom offers two types of cakes: normal cake and normal cake with decoration.

Now below is the sequence, by which preparing cake with If and Then structures:

OK Computer:

IF customer wants Decorated Cake

Then Prepare Normal Cake with Decoration

IF customer wants Normal Cake

Then Prepare Normal Cake

See how conditions make the things much easier and intelligent!

Using Else if

What if programmer has to take care of alternate scenarios? The programmer must use the mighty else if structure.

If Tom’s cake shop offers three varieties of cakes: normal cake, normal cake with decoration and normal cake with name carved out on it.

How would programmer do this sequence using if elseif else

OK Computer:

IF customer wants Named Cake

Then Prepare Normal Cake with Name

ELSEIF customer wants Decorated Cake

Then Prepare Normal Cake with Decoration

ELSE  

Prepare Normal Cake

In c language, if programmer wants to implement above task into his code form then below is the details:

main()

{

    int cakeCode = 0;

 //change cakeCode to 0,1,& 2, diff msg will be displayed

 

    if(cakeCode == 2)

        puts("Client Ordered Normal Cake with Name");

    else if(cakeCode == 1)

        puts("Client Ordered Normal Cake with Decoration");

    else

        puts("Client Ordered Normal Cake ");

}   

Output:

     [change cakeCode = 0,compile and run for last msg]   [change cakeCode = 1,compile and run for middle msg]

     [change cakeCode = 2,compile and run for first msg]

Why programmer need these If and Else, that he calls conditions?

  • They make the programs more intelligent.
  • They are based on the situations.
  • The computers are able to decide actions based on these conditions.

 

Summary

The computers can't decide themselves.

The programmer helps the computer to decide by creating conditions based on situations.

These conditions make the programs seem intelligent.

The programs with conditions help the computer to perform the right actions in the right situations.

 

PS: I have composed this post-series based on my pdf eBook on playstore:

Basics of Computer, Programming and C

The series is posted continuously one each next day, and reader can found any post by referring blogs’ time line tab.

--till next post, bye-bye and take care.