If Else Statement

What is an if-else Statement?

The if-else statement allows a program to execute one block if a condition is true, and a different block if it is false.

Syntax:

if (condition) {

// Code segment triggered when the condition holds true.

}

else{

// Triggers when the evaluation of the condition returns false.

}

Explanation of this Syntax:
  • The “if” keyword is used to start the conditional statement, followed by a condition inside the parentheses. The condition should be an expression that evaluates to a boolean value (true or false).
  • If the condition specified in the “if” statement is true, the code block inside the first set of curly braces will be executed. This block contains the code that should run if the condition is true.
  • If the condition is false, the code block inside the “else” block (the second set of curly braces) will be executed. This block contains the code that should run if the condition is false.

The “if-else” statement allows you to choose between two different blocks of code based on the outcome of the condition. When the condition is true, the ‘if’ block runs; if not, the ‘else’ block takes over.

 

 

 

Visited 1 times, 1 visit(s) today

Comments are closed.

Close