C++ if...else Statement

if...else Statement ?

If else statements in C++ is also used to control the program flow based on some condition.

if a condition is true it will execute a block of statements and if the condition is false it won’t.

But what if we want to do something else if the condition is false. Here coms the else statement. We can use the else statement with if statement to execute a block of code when the condition is false.

Syntax :

  if (condition)
  {   .......
      .......
  }
  else
  {   .......
      .......
  }

Example 1:



Output :

  Enter your age: 19
  Person is Teenager
  Personl is eligible for voting

  Second run:
  Enter your age: 14
  Person is Teenager
  Person is not eligible for voating

  Third run:
  Enter your age: 21
  Person is not a Teenager
  Personl is eligible for voting

Example 2:



Output :

  number is less than 100