What Is If Statement and Use In JavaScript, PHP, Python, Java, C/C++, C#, PowerShell, Bash Programming Languages

All programming languages provide conditional statements in order to change the flow of the programs according to given conditions. If statement is most popular conditional statement provided very similar most of the programming languages. If statements provide single or most flow path according to given conditions like count, size, text, etc.

General If Statement Syntax

As stated previously If statement for different programming languages has very similar syntax. Below is the generic syntax which is used in If statement.

if (CONDITION){
   CODE
}
elseif(CONDITION){
   CODE
}

...
else{
   CODE
}
  • `if` is used for the start of the statement
  • `CONDITION` is the condition of it should be met
  • `elseif` is used to provide an extra branch according to new condition. There may be extra `elseif` with new conditions
  • `else` is used to but the last branch if previously defined branches or conditions do not meet.

Java If Statement

We can use if statements in Java like below.

Single If Statement

We will use the time conditions where if it is lower than 18 we will print Good Day to the screen.

int time = 22;
if (time < 18) {
  System.out.println("Good day.");
}

Multiple If Statements

We will use the time conditions where if it is lower than 18 we will print Good Day to the screen if not we will print Good evening to the screen.

int time = 22;
if (time < 18) {
  System.out.println("Good day.");
} 
else {
  System.out.println("Good evening.");
}

PHP If Statement

We can implement PHP if statement like below.

Single If Statement

We will use single if statement which will print salute according to the time of the day.

$time = 22;
if ($time < 18) {
  echo "Good day.";
}

Multiple If Statements

We can also use multiple if statements like below.

$time = 22;
if ($time < 18) {
  echo "Good day.";
} 
else {
  echo "Good evening.";
}

Python If Statement

Single If Statement

We can use a single if statement in Python like below.

time = 22
if time < 18 :
  print("Good day.")

Multiple If Statements

We can also use multiple if statements like below.

time = 22;
if  time < 18:
  print("Good day.")
else:
  print("Good evening.")

JavaScript If Statement

We can implement Python if statement like below.

Single If Statement

We can use a single if statement in JavaScript like below.

val time = 22;
if (time < 18) {
  console.print("Good day.");
}

Multiple If Statements

We can also use multiple if statements in JavaScript like below.

val time = 22;
if (time < 18) {
  console.print("Good day.");
}  else {
  console.print("Good evening.");
}

C/C++ If Statement

C and C++ programming languages provide if statement like below.

Single If Statement

Single if statement will be like below.

int time=22;

if( time < 18 )
   printf("Good day.");

Multiple If Statements

Multiple if statements will be like below.

int time=22; 

if( time < 18 ){
   printf("Good day.");}
else{
   printf("Good evening.");}

C# If Statement

C# provides very similar syntax to the Java for the If statement.

Single If Statement

We will use the time conditions where if it is lower than 18 we will print Good Day to the screen.

int time = 22;
if (time < 18) {
  System.out.println("Good day.");
}

Multiple If Statements

We can use multiple if statements for C# like below.

int time = 22;
if (time < 18) {
  System.out.println("Good day.");
}  else {
  System.out.println("Good evening.");
}

PowerShell If Statement

Single If Statement

Single if statement for the PowerShell will be like below.

$time = 22

if($time -le 18){
   write-host("Good day.")
}

Multiple If Statements

Multiple if statement for the PowerShell will be like below.

$time = 22 

if($time -le 18){ 
   write-host("Good day.") 
}
else { 
   write-host("Good evening.") 
}

Leave a Comment