Components Required

Image Component Quantity Available in Kit
evive 1
USB A-B Cable 1

Introduction

In this tutorial you will learn about if statement, if-else statement and if-else-if statement, which are also known as decision making statements.

if statement

Given below is the structure of an if statement:

 If (conditional expression)  {
Body of the if statement
}

Conditional expression can be anything which can give true or false. Hence is the statement is true, the code in the body of the statement run. If the conditional statement evaluates to false. Given below is an example of if statement:

The result on Serial Monitor is:

The student1 marks is greater than 33. He passed the exam.

The student2 marks is less than or eqaul to 33. He failed the exam.

In the above code is Student 1 has marks greater than 33 hence the first statement is true and gets executed. For Student2 second statement is true, hence second stat ement is executed.

evive Explore
Explore: Try changing numbers and see what happens.

if-else statement

When using an if statement, the code in the body of the if statement is run only when the if statement evaluates to true. When it evaluates to false, program execution skips the code in the body of the if statement and continues below the body of the if statement.

By adding an else statement, code in the body of the else statement will run only when its corresponding if statement evaluates to false.

If (conditional expression)  {
Body of the if statement when conditional expression is true
}
else {
Body of the else statement when conditional expression is false
}

Hence, When the conditional expression evaluates to true:

  • Code in the body of the if statement runs.
  • Code in the body of the else statement does not run.

When the conditional expression evaluates to false:

  • Code in the body of the if statement does not run.
  • Code in the body of the else statement runs.

Given below is an example of if else statement:

The result on Serial Monitor is:

The student1 marks is greater than 33. He passed the exam.

The student2 marks is less than or eqaul to 33. He failed the exam.

if-else-if statement

The if-else-if construct allows further conditional expressions to be evaluated than the if-else construct.

Basic structure is given below:

if (conditional expression 1)  {
Body of the if statement when conditional expression 1 is true
}
else if (conditional expression 2)  {
Body of the else-if statement when conditional expression 1 is false and conditional expression 2 is true
}
else {
Body of the else statement when conditional expression 1 and 2 are both false
}

Hence, When the conditional expression 1 evaluates to true:

  • Code in the body of the if statement 1 runs.
  • Code in the body of the else-if statement and else statement will not run.

When the conditional expression 1 evaluates false and conditional expression 2 evaluates to true:

  • Code in the body of the else-if statement runs.
  • Code in the body of the if statement and else statement will not run.

When the conditional expression 1 and 2 evaluates to false:

  • Code in the body of the else statement runs.
  • Code in the body of the if statement and if-else statement will not run.

Given below is an example of if-else-if statement:

The result on Serial Monitor is:

Your grade is: A

Try changing marks and check each if-else-if statement.