Monday, January 21, 2019

If statement in c++ programming

 If statement in c++

if statement in c++ programming, learn c++ programming
learn c++ programming

If statement is a select statement which has used to choose alternative action based on condition.

There are four types of if statements in c++ 

simple if statement
if else statement
Nested if else statement
else if ladder statement



Simple if statement: 

In simple if statement we use only one if statement in the program.

syntext: 

           if(condition)
          {

           code for execution;
                  
           }

conclusion: 
in if statement if the condition is true then the code is executed which is in the { } curly brackets. and if the condition is not true then the compiler skip the if {} block. 

Example: 

            if(grade > 60)
            {

             std::cout<<"You passed your exam!";
             }

in this example when the value of grade is greater then 60 means [ grade > 60 ] then the condition of if statement is true. and then the compiler executes the code which is in between the { } curly brackets. 


else if statement:

In else if statement we used one if statement and one else statement in the program. 

syntext:

               if(condition)
               {
               code for execution;
               }
               else{
               code for execution;
               }

Conclusion: 
in else if statement if the condition of if statement is true then the code of if statement is executed and else statement is skipped by the compiler. and if the condition for the if statement is not true then the code of if statement is skipped by the compiler and code of else statement is executed by the compiler. 

Example:

              if(grade > 60)
              {

               std::cout<<"\n You passed your exam!";
              }
              else 
              {
                std::cout<<"you failed in your exam!"<<endl;
              }

In this example, if the value of grade is greater then 60. then the condition for if statement is true. so the compiler entered to the if block and print that "you passed your exam". another wise if the value of grade is not greater then 60. then the condition for if statement is not true. so compiler skipped the if block and jumped to the else statement and "print that you failed in your exam".


Nested if else statement:    

Nested if else statement is used for testing multiple cases by placing if else selection statements inside other if else selection statements.

Syntext:

              if(condition)
              {

                 if(condition)
                {
                   code for execution;
                }
                else{

                     code for execution;
                      }
              
              }

              else{

                      if(condition)
                     {
                            code for execution;
                     }
                     else{ 

                            code for execution;
                     }

              }

Example: 
               if( grade > 60)
               {

                     if(grade > 90)
                    {
                      std::cout<<"you passed your exam with A grade";
                    }
                    else {

                      std::cout<<"you passed your exam with B grade";
                   }
              }
              else 
              {
                  if( grade > 50  && grade < 60)
                 {

                   std::cout<<"you passed with C grade";
                 }
                  else 
                 {
                   std::cout<<" you failed in your exam";
                 }

             }

Conclusion: 
if the value of grade is greater then 60 then the condition for the first if block is true. so the compiler entered into first if block and then check the next if block is condition which is in the first if statement. if the value of grade is greater then 90 then the condition for the sub-first if block is true. so the compiler executes the statement which is in the if statement and print "you passed your exam with A grade:". another wise if the condition is not true for if block. then the compiler skipped the if block and go to the else block and print the message "you passed your exam with B grade:". and if the condition for the first if block is not true then the compiler skipped the first if block and then go to the first else block and execute them. 



else if ladder statement:

In else, if ladder statement we used one if statement and n number of else if statements and at the end, we use one else statement.

syntext:

            if(condition)
           {

           }
           else if(condition)
           {

            }
           else if(condition)
           {

           }
           else 
          {

           }

Example:

           if( grade > 90)
           {
            std::cout<<" \n you passed your exam with A grade";
           }  
           else if( grade > 80 && grade <= 90)
           {
           std::cout<<" \n you passed your exam with B grade";
           }    
           else if( grade > 70 && grade <=80)
           {
           std::cout<<"\n you passed your exam with c grade:";
           }
           else
           {
           std::cout<<"\n you passed your exam with D grade:";
           }

Conclusion: 
in the above code, if the value of grade is greater then 90 then the condition for the first if block is true. so the compiler executes the code which is in the first if block. another wise if the condition for first if block is false then the compile should skip the first if statement and then go to the next else if statement and continuous checked the statement and if the conditions are satisfied then the compiler execute the code which is in the block. likewise in this example, if the value for the grade is in between 80 to 90 then the condition for first else if block is true. so the compiler prints the message "you passed your exam with B grade:". 


More on If statement in c++ programming:

we can also combine the two conditions in else if statements. like 

Example: 

            if(a<=10 && a>=1)
            {

            cout<<"your value is in between 1 to 10:";
            }

I think now you can totally understand what is if statement and how we can use it in the c++ programming.

please share this post and also comment me your questions, and for more details just subscribe to our site using your email. 

0 comments: