Updated March 31, 2023
Definition of Perl unless
Perl unless statement is opposite to if statement in perl, it will execute the multiple statements inside the body of unless statement when the given condition is false. if a statement executes the code when the statement condition is true it’s reversed in unless statement it will execute the statement when the condition is false. Perl unless statement is very useful and important to execute multiple statements at one time when a given condition is false. We can use else if statement with unless statement.
Syntax and Parameter
Below is the syntax of unless statement in perl are as follows.
Syntax #1:
Unless (Boolean expression (condition)) {
Statements -- Statement is executed in unless statement when given condition is false.
}
Syntax #2:
Unless (expression)
Block -- Used with unless statement.
Syntax #3:
Unless (Condition)
Block elsif (condition)
Block
… else
Block
Parameter:
Below is the parameter description syntax unless the statement is as follows.
- Unless: Unless statement is opposite to if statement in perl, if statement executes the code when statement condition is true it’s reverse in unless statement it will execute the statement when the condition is false. Unless the statement is very useful and important.
- Condition: Condition is defined as code in which we have to define condition block if the condition is true then unless the statement will execute the statement under unless statement.
- Statement: Statement is defined as a statement that was under unless a statement block. If the condition is true then unless the statement will execute the block of unless statement.
- Block: Block is a very important parameter in unless statement. Block is nothing but statement under the condition of unless statement.
- Expression: Expression is defined as the condition of code unless statement.
Flowchart
Below is the flowchart of unless statement is as follows. A flowchart is nothing but a step by step execution of the program code. The below figure shows flowchart of unless statement.
- Flowchart is very important to find the step by step execution of unless statement that how it works in program statements.
- The above figure shows the pictorial representation or flow of unless statement in program code. How unless is working, it shows in the flowchart.
- Flowchart of unless the statement starts with keyword name as start and end with the name as stop or we can also end the flowchart with name as end keyword.
- After the starting off the flowchart compiler will check the condition or block of code if the condition is false then it will execute the statement under the conditional block of unless statement.
- If the condition is false then the compiler will execute the statement or block of code. If the condition is true then unless the statement will skip the block of code from program statement.
- Flowchart is very important to display the pictorial flow of any program or code in perl language.
How does unless Statement Works in Perl?
Below is the working of unless statement is as follows.
- Unless statement is opposite to if statement, in if statement execute the code when statement condition is true it’s reverse in unless statement it will execute the statement when the condition is false.
- We can use unless a statement with else to define multiple conditions in a single program.
- Perl unless the statement is opposite to if statement. It will execute the multiple statements inside the body unless a statement when the given statement condition is false.
- It is very useful and important to execute multiple statements at one time when a given condition is false. We can use else if statement with unless statement.
- We can use unless statement followed by else statement. The unless statement will execute the statement from right to left when the given condition is false.
- Perl will execute the statement which was precedes by unless. If the condition statement is true then perl will skip the statement of block code.
- We have used the following form of a statement unless statement when we have to execute more than one statement.
Unless (condition) {
// Block of code
}
- If the condition of the above code is false then it will execute the block of code. If the condition of the above code is true then it will skip the block of code.
Examples of Perl unless
Below is the example of unless statement is as follows.
1. Unless in Perl Using False Condition
The below example shows that unless in perl using a false condition. We have used a false condition block in the below example.
Example:
$n = 30;
# Check the condition using unless in perl if the condition is false execute the block of statement.
unless( $n < 25 ) {
# If condition of statement is false then execute below statement.
printf "Condition false\n";
}
print "value of n is : $n\n";
$n = "";
# check the boolean condition.
unless ( $n ) {
# If condition of statement is false then execute below statement.
printf "n has n false value\n";
}
print "value of n is : $n\n";'
Output:
2. Unless in Perl Using True Condition
The below example shows that unless using a true condition. We have used a true condition block in the below example.
Example:
$n = 10;
# Check the condition using unless in perl if the condition is false execute the block of statement.
unless( $n < 25 ) {
# If condition of statement is false then execute below statement.
printf "Condition true\n";
}
print "value of n is : $n\n";
$n = "";
# check the boolean condition.
unless ( $n ) {
# If condition of statement is false then execute below statement.
printf "n has n false value\n";
}
print "value of n is : $n\n";
Output:
Recommended Articles
This is a guide to Perl unless. Here we also discuss the definition and how does unless statement works in perl along with different examples and its code implementation. You may also have a look at the following articles to learn more –