-->




IF and Nested IF Statements in C++

12 comments
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

 Code:


  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;
  9.  
  10.               if(num<10)
  11.             {
  12.              cout<<num<<" is a ONE digit Number ";
  13.             }
  14.            
  15.             else  if(num<100)
  16.             {
  17.                 cout<<num<<" is a  TWO digit Number ";
  18.             }
  19.            
  20.             else if(num<1000)
  21.             {
  22.                  cout<<num<<" is a Three digit Number ";
  23.             }
  24. }
  25. return 0;}


12 comments:

  1. yup....in this program else if ladder will be used...

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. #include
    #include
    void main()
    {
    int num;
    cout<<" Enter Number between 1 to 999 ";
    cin>>num;
    if(num>0)
    { cout<<num<<" is a Positive Number "<<endl;

    if(num<10)
    {
    cout<<num<<" is a ONE digit Number ";
    }

    else if(num<100)
    {
    cout<<num<<" is a TWO digit Number ";
    }

    else if(num<1000)
    {
    cout<<num<<" is a Three digit Number ";
    }
    }
    else
    cout<<num<<" is a negtive number or zero";
    }

    ReplyDelete
    Replies
    1. Hello how about using switch in the C++ whats the program goes ????

      Delete
  4. sar please tell us sepratly about nested if and nested if else

    ReplyDelete
    Replies
    1. See Here
      http://fahad-cprogramming.blogspot.com/p/list-of-all-chapters.html

      Delete
  5. Thanks a lot. I finally understand about if nested in C++

    ReplyDelete
  6. I can't find nested if program can u write again?

    ReplyDelete