If Else Statements in C++

If Else Statements in C++

Table of Contant

  • If

  • Else

  • Else if

  • Short Hand If...Else (Ternary Operator)

  • Condition from Mathemathic in C++

  • Examples

  1. Check if a Number is Positive, Negative, or Zero

  2. Check if a Character is a Vowel or Consonant

  3. Determine the Grade Based on Marks

  4. Check if a Year is a Leap Year

  5. Find the Largest of Three Numbers

  • And This Programes Output

If

Simple if statement in C++ is used to check whether a condition is true or false. If the condition is true, the statements inside the if block are executed. In case the condition is false, the control goes to the first statement after the if block, and the normal execution of the program continues.

if (condition) {
  // block of code to be executed if the condition is true
}

else

The else statement is used to specify a block of code to be executed if the condition in the if statement is false.

if (condition) {
  // block of code to be executed if the condition is true
} else {
  // block of code to be executed if the condition is false
}

else if

The else if statement allows you to check multiple conditions. If the first condition is false, the program.

if (condition1) {
  // block of code to be executed if condition1 is true
} else if (condition2) {
  // block of code to be executed if the condition1 is false and condition2 is true
} else {
  // block of code to be executed if the condition1 is false and condition2 is false
}

Short Hand If...Else (Ternary Operator)

There is also a short-hand if else, which is known as the ternary operator because it consists of three operands. It can be used to replace multiple lines of code with a single line. It is often used to replace simple if else statements:

Syntax

variable = (condition) ? expressionTrue : expressionFalse;

Condition from Mathemathic in C++

You already know that C++ supports the usual logical conditions from mathematics:

  • Less than: a < b

  • Less than or equal to: a <= b

  • Greater than: a > b

  • Greater than or equal to: a >= b

  • Equal to a == b

  • Not Equal to: a != b

Example

1.Check if a Number is Positive, Negative, or Zero. This

Check a Number is Positive, Negative, or Zero.Declare a Variable, Accept a number from user and store in num Variable.check positive,negative or Zero using if else statements.

Output of this program

  • Input Positive Number 10

  • input Negative Number -10

  • input zero

2.Check if a Character is a Vowel or Consonant

Check if a Character is a Vowel or Consonant.Declare a Variable, Accept a Character from user and store in ch Variable.Using if else statements and check Vowel or Consonant.

Output of This Program

  • input "i"

  • input "Y"

3.Determine the Grade Based on Marks

Determine the Grade Based on Marks.Declare a Variable,Accept mark from user and find the mark which category and display the grade A,B,C,D or F.Using in if else statement.Include else if.

Output of this Program

  • input 76

    4.Check if a Year is a Leap Year

Check if a Year is a Leap Year .Declare a Variable in int format and Accept Year From User. Check year leap year or not a year using if else statement.

Output of This program

  • input 2000

5.Find the Largest of Three Numbers

Find the Largest of Three Numbers.Declare a Variable in int for Mat and Accept three number From User.Check are number and which is largest and large to small order give Output.

Output of This program

  • input 10,20,30