Table of Contents
*This post may contain affiliate links. As an Amazon Associate we earn from qualifying purchases.
Conditional logic is one of the most powerful concepts in computer programming. Conditional statements, constructs, and expressions allow a program to evaluate data and take different actions based on one or more Boolean ? that is, true or false ? conditions.
While if/then is the most familiar conditional statement, another useful one programmers will frequently employ is if/else. We?ll look below at exactly how the if/else statement is used in different programming languages.
The If/Else Statement in 15 Programming Languages
1. Ada
Ada is a high-level, object-oriented language extended from Pascal. It was developed for the U.S. Department of Defense from 1977-83 and was named after Ada Lovelace, who is recognized as the first computer programmer.
The Ada if/else statement:
with Ada.Text_IO;
with Ada.Integer_Text_IO;
use Ada.Text_IO;
procedure Exercise is
Item : Integer;
begin
Put_Line(?Enter the type of car you want to buy?);
Put_Line(?1. Honda?);
Put_Line(?2. Lexus?);
Put_Line(?3. Mercedes?);
Put_Line(?Your Choice? ?);
Ada.Integer_Text_IO.Get(Item);
if Item = 1 then
Put_Line(?Car Type: Honda?);
else
Put_Line(?Another type of car?);
end if;
end Exercise;
2. C++
A general-purpose programming language, C++ was initially released in 1985. It?s particularly effective for applications which are resource-constrained. It is the follow-up to the C programming language developed in the early 1970s.
The C++ if/else statement:
// This program checks if a number is negative or positive
// In this program 0 is positive number
#include <iostream>
using namespace std;
int main()
{
int number;
cout << ?Input a number: ?;
cin >> number;
if ( number <= 0)
{
cout << ?You entered a negative number: ? << number << endl;
}
else
{
cout << ?You entered a positive number: ? << number << endl;
}
cout << ?This line will always be printed.?;
return 0;
}
3. C#
C# was developed by Microsoft and first released in 2000. Part of Microsoft?s .NET initiative, it was also designed to be part of the Common Language Infrastructure (CLI). It can be used for both embedded and hosted applications.
The C# if/else statement:
// Try with j = 11 and then with j = 4.
int j = 11;
int k = 12;
if (j > 10)
if (k > 10)
{
Console.WriteLine(?ResultA?);
}
else
{
Console.WriteLine(?ResultB?);
}
4. F#
Encompassing object-oriented, imperative, and functional programing, F# is a multi-paradigm language which can produce graphics processing unit (GPU) and JavaScript code. It can also be used as a CLI programming language. Developed by Microsoft and the F# Foundation, it was originally released in 2005. The F# Foundation also provides a cross-platform, open-source compiler.
The F# if/else statement:
The F# if/else statement:
let c : int32 = 50
if (c < 50) then
printfn ?c is less than 50n?
else
printfn ?c is not less than 50n?
printfn ?Value of c is: %d? c
5. Haskell
A purely functional programming language, Haskell has static typing and non-strict semantics. Its type system uses lazy evaluation, lambda expressions, type inference, and pattern matching. Used extensively in industry and higher education, it?s based on the programming language Miranda?s semantics. It was released in 1990 followed by a stable release in 2010.
The Haskell if/else statement:
describeLetter :: Char -> String
describeLetter c =
if c >= ?A? && c <= ?Z?
then ?Upper case?
else if c >= ?a? && c <= ?z?
then ?Lower case?
else ?Not a valid ASCII character?
6. Java
Java is an object-oriented, class-based, and general-purpose programming language. It is one of the most popular programming languages in the world with more than 9 million developers. Its guiding principle is ?write once, run anywhere? (WORA). That is, once Java programs are compiled, they can run without needing to be recompiled on any platform which supports Java.
The Java if/else statement:
class IfElseDemo {
public static void main(String[] args) {
int coursegrade = 83;
char grade;
if (coursegrade >= 92) {
grade = ?A?;
} else if (coursegrade >= 89) {
grade = ?A-?;
} else if (coursegrade >= 87) {
grade = ?B+?;
} else if (coursegrade >= 80) {
grade = ?B?;
} else if (coursegrade >= 70) {
grade = ?C?; }
} else if (coursegrade >= 60) {
grade = ?D?;
} else {
grade = ?F?;
}
System.out.println(?Course Grade = ? + grade);
}
}
7. JavaScript
JavaScript, commonly known as ?JS,? is an interpreted, high-level, multi-paradigm language. It is one of the three baseline technologies ? along with CSS and HTML ? for the World Wide Web. Most websites use Java, and there is a JavaScript engine built in to all major browsers. Despite its similarity in name to Java, the two languages are very different in functionality and design.
The JavaScript if/else statement:
if (hour < 12) { greeting = ?Good morning?;} else { greeting = ?Good day?;}
8. Perl
Perl actually consists of two languages: Perl 5 and Perl 6. It was originally developed in the late 1980s as a Unix scripting language for report processing. Perl 5 began to be used extensively for CGI scripting and GUI applications in the 1990s. Perl 6 was intended to be a Perl 5 redesign, but then the two languages began to be developed independently.
The Perl if/else statement:
use strict;
use warnings;
print ?How old are you? ?;
my $age = <STDIN>;
if ($age >= 16) {
print ?In most countries you are old enough to drive a car.n?;
} else {
print ?You are not old enough to drive a carn?;
}
9. PHP
A server-side scripting language, PHP is a general-purpose language originally released in 1994. HTML code can have PHP code embedded in it. It can also be used with web frameworks and web-based content management systems. PHP can be used for free on almost every platform and operating system.
The PHP if/else statement:
<?phpif ($c < $d) { echo ?c is smaller than d?;} elseif ($c == $d) { echo ?c is equal to d?;} else { echo ?c is larger than d?;}?>
10. Python
Originally released in 1991, Python is used for general-purpose coding and utilizes a format incorporating white space and other design aspects to aid code readability. It has a wide-ranging standard library and comes with multiple programming paradigms.
The Python if/else statement:
# Program checks if the number is negative or positive
# And displays a message with the answer
num = 2
# Try these variations too.
# num = -3
# num = 0
if num < 0:
print(?Negative number?)
else:
print(?Positive number or zero?)
11. Ruby
Developed in the 1990s, Ruby supports multiple programming paradigms: imperative, functional, and object-oriented. Influenced by earlier programming languages such as Ada, Lisp, and Perl, major implementations include Mruby, MacRuby, YARV, and JRuby.
The Ruby if/else statement:
if my_account_balance > 25.00
puts ?I?m drinking craft beer! :)?
else
puts ?I?m drinking Keystone :(?
end
12. SQL
Structured Query Language (SQL) manages data in relational database management systems (RDBMSs). It is particularly well suited for accessing databases with millions of rows or columns with or without an index and can do so with a single command.
The SQL if/else statement:
DECLARE @cat_value INT;
SET @cat_value = 5;
IF @cat_value < 4
PRINT ?You have the right amount of cats?;
ELSE
PRINT ?You are a crazy cat person?;
GO
13. Swift
Swift was developed by Apple for Mac operating systems as well as Linux. It utilizes the LLVM open-source compiler and has, at times, been in the top 10 most popular programming languages. Its different versions, however, have been largely incompatible with each other, which has caused many developers to quit using it until a stable version is released.
The Swift if/else statement:
let num = 5
if (num > 0) {
print(?the number is positive?)
} else if num < 0 {
print(?the number is negative?)
}
14. Visual Basic (classic)
Visual Basic is a descendant of BASIC and was released in 1991. Its final version, Visual Basic 6.0, came out in 1998, and in 2008, it was designated with a legacy status. The Microsoft Visual Basic Team, however, is dedicated to keeping VB 6.0 applications compatible with Microsoft operating systems.
The Visual Basic if/else statement:
Dim password As Integer
username = Nothing
password = Nothing
Console.WriteLine(?Please enter your username?)
username = Console.ReadLine()
Console.WriteLine(?Please enter your password?)
password = Console.ReadLine()
If username = ?Alan? And password = 656 Then
Console.WriteLine(?Welcome, Alan!?)
Else
Console.WriteLine(?Access is denied. Security is being alerted!?)
End If
Console.ReadLine()
15. Windows PowerShell
Windows PowerShell consists of a scripting language and command-line shell. As of August 2016, it?s both cross-platform and open source. WMI and COM are fully accessible, and administrative tasks can be performed remotely or locally. It also has a robust, console-based help system.
The Windows PowerShell if/else statement:
$Data = ?Honda?
If ($Data -eq ?Lexus?) {
?This is a Lexus?
} ElseIf ($Data -eq ?Ford?) {
?This is a Ford?
} ElseIf ($Data -eq ?Honda?) {
?This is a Honda?
} Else {
?Cannot determine what type of vehicle this is?
}