Javatpoint Logo
Javatpoint Logo

Arduino if-else and else-if

The else and else-if both are used after specifying the if statement. It allows multiple conditions to be grouped.

If else

The if-else condition includes if ( ) statement and else ( ) statement. The condition in the else statement is executed if the result of the If ( ) statement is false.

The flowchart is shown below:

Arduino if-else and else-if

Let's understand with an example.

The else( ) statement can also include other if statements. Due to this, we can run multiple statements in a single program.

The flowchart is shown below:

Arduino if-else and else-if

The statements will be executed one by one until the true statement is found. When the true statement is found, it will skip all other if and else statements in the code and runs the associated blocks of code.

Code Example

Let's understand if else statement with the help of two examples.

Example 1:

Consider the below code.

In the above example, the values are initialized to the variables a and b. The message concerning the satisfied condition will be printed.

Example 2:

Consider the below code.

Output: LED1 will light +++

If the initialized value of x is less than 100, the message ' LED1 will not light ' will be printed in the output.

Else if

The else if statement can be used with or without the else ( ) statement. We can include multiple else if statements in a program.

Let's understand with an example.

Code Example

Let's understand else-if statement with the help of an example.

Example 1:

Consider the below code.

Output: J is greater

The else if ( ) statement will stop the flow once its execution is true.

What is the difference between Else and Else If?

The Else ( ) part is executed if one or all the If ( ) conditions present in the code comes out to be false.

The else if ( ) will stop the program flow if it becomes true.


Next TopicArduino for Loop





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA