• admin@embedclogic.com

if-else statement

if-else statement

Many times we need to execute some statements only once when some condition gets true or false.

Statement execution only once on Condition

To complete this requirement we have several methods like –

  1. if statement
  2. if-else statement
  3. if-else-if statement
  4. Nested if statement

if statement

syntax

As shown in syntax, statement1….statementn will execute only when condition inside if gets true.

Example

In above program, we can see that execution of printing hello statement depends on the condition.

if-else statement

This method is used where we need to execute some statements when condition inside if gets true and some need to execute when condition gets false.

statements which need to execute when condition gets true will put in the if block and which need to execute on false behaviur will put in the else block.

Syntax

if block will execute when condition gets true otherwise else block statement will execute.

Example

if-elseif-else statement

This method is used where we need to execute blocks of statements when respective conditions inside if or elseif gets true.

syntax

Example

Program To find the greatest of three integers(var1,var2,var3).[See the use of Logical operator]

Possible Conditions:

Program:

Nested If statement

Nested if means use of if statement inside the master if-else statement.let’s solve the above example through if nested if statement use-

Example

Program To find the greatest of three integers(var1,var2,var3).[Nested If statement]

Pseudo Code:

Program: