If else statement in C++

in computerprogrammin •  6 years ago 

 In this tutorial, we will learn how to use C++ for decision making with example.

Syntax of if else statement:

If the condition of if statement is correct i.e returns true then the code inside if is executed and the else part is skipped.If the condition of if returns false then code inside else block is executed.

if(condition) {
  // Statements inside body of if
}
else {
  //Statements inside body of else
}

Flow Diagram of If else statement to check if number is odd or even.

An even number is an integer that is exactly divisible by 2. Example: 0, 8, -24
An odd number is an integer that is not exactly divisible by 2. Example: 1, 7, -11, 15

Check if given number is odd or even.

#include <iostream>
using namespace std;

int main()
{
int num;
  cout << "Enter an number: ";
  cin >> num;
  if(num % 2 == 0)
  {
      cout << n << " is even.";
  } else {
       cout << n << " is odd.";
  }
  return 0;
}

Output

Enter any number: 8
8 is even.

The number entered is stored in variable num.In above program, This statement will only execute if the  above condition (num % 2 == 0) returns true.This statement will only execute if the condition specified in the “if” returns false.
 

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Congratulations @tokiobki! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!