Showing posts with label if-else. Show all posts
Showing posts with label if-else. Show all posts

Monday, January 30, 2012

Learning PERL | Day 6


Hi friends! In our last blog learning session, we grabbed some knowledge about the flow of control in PERL programming by using ‘if-else’ statements. Let us understand today these ‘if-else’ statements by means of some examples.

Consider you are asked to build a program which can classify the customers in a bank as premium, important and regular customer on the basis of the balance they have in their bank accounts. Classification is to be made like this: 

“those customers with amount deposited over $1,000,000 are to be treated as premium; those having amount deposited between $500,000 and $1,000,000 are to be treated as important customer; and finally, those left, i.e., customers with deposits below $500,000 are to be treated as regular customers.”

This problem can be converted into a PERL program which can be used to distinguish the various classes of customers. Let us see how it is done:

if ($balance > 1000000)
{
  print “\nPremium Customer.”;
}
elsif ($balance > 500000)
{
  print “\nImportant Customer.”;
}
else
{
  Print “\nRegular Customer.”;
}


In the program illustrated above, the flow is designed in such a way that there is the need of only one condition to be specified in the body of execution. ‘Body of Execution’ is the block below each condition, it is included in the curly-brackets ( { <body> } ).

One more thing to notice here is that we do not put terminator (;) after the if or elsif or else statement. It is because the statement as a whole does not terminates at the same line, if found correct, it’s body will be executed, which is in the next line starting with ‘{‘ and ending with ‘}’. It is a common mistake to put terminator after the flow statement.

One more that I want to discuss today is the use of comments in our programs. Suppose you are working on a big-big and complex program which uses various formulae and statements. It is a usual thing that you may not be able to grasp your own coding if viewed later. So, to keep our program meaningful to us and to other programmers, we use some statements which help us in grabbing the idea about our program. These statements are completely ignored by the PERL interpreter.

In PERL, comments begin with ‘#’ symbol. In other words, if you put ‘#’ in a line, all the characters after become invisible to interpreter. Let us re-write the above program using comments to make it more illustrative.

if ($balance > 1000000)          # first condition
{
  print “\nPremium Customer.”;   #  Body of first condition
}
elsif ($balance > 500000)        # second condition
{
  print “\nImportant Customer.”; #  Body of second condition
}
Else                             #  Final condition
{
  Print “\nRegular Customer.”;  #  Body of final condition
}


This concludes today’s learning session on PERL learning blog. Hope you are finding these learning sessions interesting and easy to understand.


With Warm Regards,
Yajur Kumar
(PERL Programming Expert)

Thursday, January 26, 2012

Learning PERL | Day 5


Hi friends! Till now, I think we have grabbed sufficient to start out study of flow of control in PERL programming. By flow of control, I mean how the control of program passes from one step to another. It can be understood as the main deciding factor or program flow logic.
In our previous blog learning session, I introduced ‘if-else’ statements; however, I left their deep understanding for today. So, let us understand the functioning of these ‘if-else’ statements today.

The ‘if’ statement is used to specify a particular condition, often, a logic statement. It can be understand by following syntax:

if (<condition>)
{
   <Steps to follow>
}
Consider the following code fragment to understand this syntax:

if ($x==0)
{
    print “\nThe value of x is zero”;
}

The above code fragment is used to check whether the value of x is zero. To judge this, we first used if statement with condition “$x==0”, which means the statement that “x equals zero”. If this condition is found true, the statement body will be executed, which prints “The value of x is zero.” on the screen, otherwise, it will be skipped.

If it is needed that provided the first condition is skipped, the program will check for the second condition, ‘if-else’ or ‘else’ statement is used. The general syntax of ‘if-else’ nesting, which can be extend to the limit we need, is:

if (<condition_1>)
{
    <Steps to follow>
}
else if (<condition_2>)
{
   <Steps to follow, provided condition_1 is wrong>
}
else
{
   <Steps to follow, provided all conditions are wrong>
}

We will understand the ‘if-else’ nesting with a suitable example in our tomorrow’s learning session on this blog.
Hope you enjoyed today’s learning session on PERL.
Happy Republic Day to my readers from India.

As always, if you find anything difficult or if you have any suggestion, please feel free in discussing with me.
With Warm Regards,
Yajur Kumar
(PERL Programming Expert)