realLifeinfo

If statement in C-based programming languages

Nerd, Solutions 

When we are talking about C-based languages, we mean those programming languages that are closely related to C programming language syntactically. Those languages include Java, C++, C itself, JavaScript, C#, and PHP to name a few.

Sponsored

The if statement is one of the important statements in many programming languages. If statement is used when writing conditional code - code that will act differently based on certain conditions and criteria.

If statement is available in many programming languages, but in this article, we are going to discuss how to use and declare if statement in C-based programming languages - C, C++, Java, C#, PHP, and JavaScript.

If statement in C-based programming languages uses the same syntax. This syntax can be used in Java, C, C++, C#, PHP, and JavaScript.

Syntax Example

if (condition) {

     //code block

}

As you can see from our syntax above the if statement contains the keyword if followed by condition enclosed in brackets. After brackets is the code block that encloses the statement that will be executed based on the condition inside the brackets.

The if statement is going to evaluate the condition for a Boolean value - true or false. If the condition evaluated to true, the code inside the code block will be executed, otherwise, the execution will continue without executing the code inside the if statement block.

If statement extended

The if statement can be extended into an if...else statement. This is the same as the if statement. The only difference is when the condition evaluates to false in a normal if statement, nothing is going to be executed from the if statement, but in an if...else statement, when the condition evaluates to false the code inside the else block will be executed.

Syntax Example

Sponsored

if (condition) {

    //code block

} else {

   //code block

}

Multiple if statements

If statement can be extended further by using more than one if statement in a single statement.

Syntax Example

if (condition) {

   //code block

} else if (condition) {

   //code block

} else {

   //code block

}

This is just like the normal if statement. The only difference is this statement is going to evaluate multiple conditions. If the particular condition evaluates to true, the code inside that block will be executed, if not the if statement will pass to evaluate the next condition in its if...else if...else chain. If all conditions evaluate to false, then else block will be executed.

Another thing to note about if statement is, one if statement can be contained inside another if statement. And this can involve multiple levels of if statements.

Syntax Example

if (condition) {

        if (condition) {

               //code block

        }

}

Code example in C programming language and in JavaScript

In C:

int price = 100;

if (price < 100) {

    printf("%s ", "You can buy it, the price is below 100 dollars");

} else {

    printf("%s ", "You can't buy it, the price is higher than 100 dollars");

}

Sponsored