IF and Nested IF Statements in C++

If statement in C++
If is a keyword in C++ language. It is used to execute or ignore a set of statements after testing it.
A condition is a relational or logical expression and it produces either true(means 1) or false(means 0) result.
IF the condition is true then the block of statement is executed and if the condition is false the the block of statement is ignored and the control is transferred to the next statement after if statement.
Syntax to write IF statement:

   if (relational or logical condition)


           Block of statements


Next statement after if:
Flowchart of if Statement

For Example:

A Program to check number is positive and how many digits number have


  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {int num;
  5.           cout<<" Enter Number btween 1 to 999 ";
  6.           cin>>num;
  7. if(num>0)
  8. {          cout<<num<<" is a Positive Number "<<endl;
  10.               if(num<10)
  11.             {
  12.              cout<<num<<" is a ONE digit Number ";
  13.             }
  15.             else  if(num<100)
  16.             {
  17.                 cout<<num<<" is a  TWO digit Number ";
  18.             }
  20.             else if(num<1000)
  21.             {
  22.                  cout<<num<<" is a Three digit Number ";
  23.             }
  24. }
  25. return 0;}


Hopefully the information we provide can be useful.

Related Article

Press ESC to close