Perl Condition Statement With An Example

10:55 Add Comment
Every programming language must have decision statement that’s the part that makes the scripting or programming language more robust and intelligent. When we talk about decision statement that first thing comes to our mind is If…else statement. We also take daily decision based in if logic only. So let’s see Perl condition statement and their types.
Perl Provide the following Condition statements:
  1. IF Statement
  2. IF…ELSE Statement
  3. IF…ELSIF...ELSE Statement
  4. UNLESS Statement
  5. UNLESS….ELSE Statement
  6.  UNLESS…ELSIF….ELSE Statement
  7. SWITCH Statement
  8. Conditional Operator ? :

IF Statement

IF Statement is simple statement that is used where some condition need to be tested if that condition is true then perform certain operation and if not then continue executing code followed by IF statement.

Following is the syntax of IF Statement:

If(Boolean expression )
{
//Statement to be executed if condition is true
}

Here is an example of IF Statement:
$x = 100;
if( $x < 200 )
{  
    printf "This will execute means condition it true\n";
}

$x = 0;
if( $x )
{
    printf "X has a true value\n";
}
print "value of a is : $a\n";
Output if the above program is:

This will execute means condition it true
value of a is : 0
If you change the value of x=0 to x=1 then the output will be:

This will execute means condition it true
X has a true value
value of a is : 1

So here what happens is o is considered as false and 1 is considered true for Boolean value. 

IF…ELSE Statement 

Sometime we come across the situation where we need to perform something even if condition false. In that case only IF statement is not sufficient. So there IF..ELSE statement comes into the picture.

Here is the syntax:
If(Boolean Expression)
{
//If condition is true then this code will executed.
}else
{
If condition is false then this code will get executed
}
Here is an example for IF…ELSE Statement:
$x = 100;
if( $x < 200 ){  
    printf "Value of x is less then 200 \n";
}else
{
    printf "Value of $x is greater then 200 \n";
}

$x = 1;
if( $x ){
    printf "X has a true value\n";
}else
{
    printf "X has a false value\n";
}
Output of the program is:

Value of x is less than 200
X has a true value

IF…ELSIF...ELSE Statement

There are some situations where you have multiple options but we must select one at a time. In this kind of situation we can use this IF…ELSIF….ELSE statement.
Here is the syntax of IF…ELSIF….ELSE statement:

If(Boolean Expression)
{
//If condition is true then this code will executed.
}elsif(Boolean Expression)
{
//If condition is true then this code will executed.
}else
{
//If both above conditions are false then this code will executed.

}

Let’s take one example to understand:

Let’s assume you have system only 3 person are allowed to access it or for any other person it will show error
$name= "Dharmesh";
# check the boolean condition using if statement
if( $name  eq  "Sanjay" ){
    # if condition is true then print the following
    printf "Welcome Sanjy\n";
}elsif($name  eq "Tejas" ){
    # if condition is true then print the following
    printf "Welcome Tejas\n";
}elsif($name  eq "Dharmesh" ){
    # if condition is true then print the following
    printf "Welcome Dharmesh\n";
}else{
    # if none of the above conditions is true
    printf "You are not authorized person\n";
}

In above example only one person at time can access the system. It will only allow Sanjay, Tejas and Dharmesh otherwise gives an error.

SWITCH Statement

Instead of using this there is another alternative that Perl provide for IF…ELSIF….ELSE statement is switch statement. Let’s take same above example for switch statement too so it will be easy for you to understand. But for that you need to specify in beginning of script “use switch”.
use switch;

$name=”Tejas”;

Switch($name)
{
case “Sanjay”
{
    printf "Welcome Sanjy\n";
}
case “Tejas”
{
    printf "Welcome Tejas\n";

}
case “Dharmesh”
{
    printf "Welcome Dharmesh\n";
}
else
{
    printf "You are not authorized person\n";
}
}
This code also works same as above. This two are alternative for each other. Let’s see one more example:
use Switch;
$var = 10;
@array = (10, 20, 30);
%hash = ('key1' => 10, 'key2' => 20);

switch($var){
   case 10           { print "number 100\n"; next; }
   case "a"          { print "string a" }
   case [1..10,42]   { print "number in list" }
   case (\@array)    { print "number in list" }
   case (\%hash)     { print "entry in hash" }
   else              { print "previous case not true" }
}
When above code will be executed, following will be the output.
number 100
number in list

UNLESS Statement:

Unless is same as conditional statement as IF statement. But only difference in UNLESS statement is that it execute block of code when condition is false. On other hand in IF Statement block of code execute only when condition is true.
Lets see an example:
$num=100;
unless ($num<200)
{
print ”This will execute when condition is false\n”;
}
 print ”The value of num is $num\n”;
When above code get executed it will produce following output:
Executing the program....
The value of num is 100

Since condtion given in UNLESS Statement was true hence This will execute when condition is false this is not printed.

UNLESS….ELSE Statement

UNLESS…ELSE is also works similar as IF…ELSE statement. It also behave same as UNLESS statement that we have seen above. Just else part is added to it. I will take same above example but we will add else part in it.
$num=100;
unless ($num<200)
{
print ”This will execute when condition is false\n”;
}else
{
 print ”The value of num is $num\n”;
}
Following will be the output of above program:
Executing the program....
The value of num is 100

UNLESS…ELSIF….ELSE Statement

This again same as IF…ELSIF...ELSE Statement but as we have seen earlier here condition must be false in order to execute the code. So let’s move on to quick example.
$num=100;
unless ($num==200)
{
print ”The value of num is equal $num\n”;
}elsif($num==400)
{
print ”The value of num is equal $num\n”;
}else
{
print ”The Actual value is $num\n”;
}
After execution we have got following output:

Executing the program....
The value of num is equal 200


But if you try this code then only else part will be executed:
$num=200;
unless ($num==200)
{
print "The value of num is equal 200\n";
$num=400;
}elsif($num==400)
{
print "The value of num is equal 400\n";
}else
{
print "The actual value of $num\n";
}

The ? : Operator

The last many called this a ?: operator or ternary operator. This also works as IF…ELSE statement. It first evaluates Boolean expression and if it’s true then it will take first parameter and if false then it will take second parameter.

Syntax:
Exp1 ? Exp2 : Exp3;

Let’s see simple example:

$name=”Dharmesh”;
$age=25;
$status=($age==25)?”Young”: ”Senior Citizen”;
print”$name is $status”;
Following will be the output of above program:
Executing the program....
Dharmesh is Young

So we have seen all decision making statement that are provided by Perl Scripting. We have seen all IF, IF…ELSE , IF…..ELSIF….ELSE , ? : operator and Unless operator. If you have any queries or suggestion please comment below. Thanks for Reading.

Type Conversion Or Type Casting In C# With Examples

10:55 Add Comment
We have already seen C# is strictly type language. As I have explained earlier it do not convert one type to another type that directly. There are specific ways of doing so. This procedure is called type conversion. We will see type conversion in c# with examples so you will get better understanding.

Refer to this: C# Data Type In Detail


In programming programmer need to sometime manipulate variables or convert one type to another type. For doing this process of converting one type to another is called Type conversion or type casting. There are 2 types of type conversion:

Implicit Type Conversion:
Implicit type conversion perform by system automatically you don’t have to do anything. You just assign one variable to another variable type and other all things will be taken care C# compiler. Lets see an example:


using System;
namespace TypeConversion
{
   class ImplicitConversion
   {
      static void Main(string[] args)
      {
     
         float f = 75.12546f;
         double d = f;
        
         Console.WriteLine("This is float value :"+f);
         Console.WriteLine("This is double value :"+d);
         Console.WriteLine("This is double value type converted to string type"+d.ToString());
         Console.ReadKey();
           
      }
   }
}

Here is the output of the program:

Executing the program....
This is float value :75.12546
This is double value :75.1254577636719
This is double value type converted to string type75.1254577636719

In above example compiler converted float type to double type automatically. You can also see we have used .ToString() method to print the double value. Since string is alphanumeric and larger than Double type it can accept almost all type. But it was reverse then it will give you error.

Explicit Type Conversion:
As name suggest you have to explicitly tell compiler to convert one value type to another value type. You can do it by 2 ways. Let’s see an example for the same:


using System;
namespace TypeConversion
{
   class ExplicitConversion
   {
      static void Main(string[] args)
      {
         int i = 75;
         float f = 53.005f;
         int convert=(int)f;
         double d = 2345.7652155478;
         float convertftod=(float)d;

         Console.WriteLine("This is integer value "+i);
         Console.WriteLine("This is Float value "+f);
         Console.WriteLine("Converted float to int "+convert);
         Console.WriteLine("This is double value "+d);
         Console.WriteLine("Converted doule to float "+convertftod);
         Console.ReadKey();
           
      }
   }
}


The output if this program is :

Executing the program....

This is integer value 75
This is Float value 53.005
Converted float to int 53
This is double value 2345.7652155478
Converted doule to float 2345.765

Here int is smaller type then float. Hence to convert the float into integer type we have exclusively instructed the complier by (int). By this compiler forcefully convert float type to int. As you can see .005 values has been truncated y compiler since integer type do not support precision values. Similarly the case with double converted to float. You can see only first 3 values after precision have been taken and value hereafter has been truncated. Since compiler converting larger type to smaller type there is always some values that loss during conversion.

Now we move on to most important part that actually useful in real life:
We are going to see how to take user input in program and manipulate it then show it as display output.
We are going to take following thing from user as input:
First Name
Last Name
Salary
Age
Self-Description
And display user his info as output.


using System;
namespace UserInputApplication
{
   class UserInput
   {
      static void Main(string[] args)
      {
          //Defining Variable
         string fName,lName,describtion;
         int age;
         double sal;
        
         //When user input the value it will initialized
         Console.writeLine(“Enter Your First Name :\n”);
         fName=Console.ReadLine();
         Console.writeLine(“Enter Your Last Name :\n”);
         lName=Console.ReadLine();
         Console.writeLine(“Tell us something about you :\n”);
         describtion=Console.ReadLine();
         Console.writeLine(“Enter Your Age:\n”);
         age=Convert.ToInt32(Console.ReadLine());
         Console.writeLine(“Enter Your Salary :\n”);
         sal=Convert.ToDouble(Console.ReadLine());
        
         Console.WriteLine("Hello"+fName+" "+lName+"nice to know you. You age is "+age+" All we know about you is "+describtion+"And your salary is "+sal);
         Console.ReadKey();
      }
   }
}


Output of this program is:

Enter Your First Name :
Dharmesh
Enter Your Last Name :
Khatri
Tell us something about you :
I am programmer and blogger
Enter Your Age:
24
Enter Your Salary :
20000

You must have noticed for accepting first name and last name and description we just assign value to variable. While accepting age and Salary we need to convert to int and float type. Why is that so? Is it necessary? The answer is as follows:
When Console.ReadLine() method accept data from the user it will by default accept the data as String format. So assigning String type to string, there is no need of conversion. But while assigning string to integer or float will be problem. Hence we have to convert it into respective data types.
Here are list of methods that can used for Type Conversion:

ToBoolean()
ToByte()
ToChar()
ToDateTime()
ToDecimal()
ToDouble()
ToInt16()
ToInt32()
ToInt64()
ToSbyte()
ToSingle() //Converts a type to a small floating point number.
ToString()
ToUInt16()
ToUInt32()
ToUInt64()

The names itself are self-explanatory of converting methods. You can check yourself by using them as we have used in above program.

So here we have seen all implicit type conversion explicit type conversion user type conversion with an example. If you have any query comment below. Thanks for reading stay tuned for more.