Tutorial on conditional statement in C and C++

conditional statement
In the previous sections, you have learnt about data type and the first program in C and C++ with visual studio. In this section, you will learn an important term which is necessary not only in C and C++ but also any other programming languages named conditional statement. It will help you to make the program so professional.



There are three ways to build your conditional statement in C / C++ language. They are

  •       Only if condition 
  •       if-else condition
  •       if-else ladder


Conditional statement using only if keyword 

If you need to take decision only single condition say, if you have $5, you can buy two apples, you can use only if condition. In C and C++ language, you can declare if condition like this…

          if (condition)
               {
                    // body goes here.
               }

 If the condition is true, the body will be executed otherwise the compiler will ignore the body of the statement.


Conditional statement using if-else condition

If it so happen that if you fulfill the condition, you will get a result otherwise you will get another result, you can use if-else condition. Say, if you get 40 marks, you will pass otherwise you will fail. At this case you should use if-else statement. Rule to declare if-else statement is given below…
                                          
               If (condition)
                    {
                           // body goes here
                     }  


             else

                 {

                      // body goes here.

                 }



In the above rule, if the condition of ‘if statement’ is true, the body of this statement will be executed otherwise the body of else statement will be executed. Here you can see one body must be executed and other will be ignored.


Conditional statement using if-else ladder

When you need to take decision from multiple stages, you should use if-else ladder statement. Say, if you get 80 marks, your score will be A+ or if you get 60 marks, you will get B or if you get 40 marks, you will pass otherwise you will fail. To take decision for this situation you can use if-else ladder statement. Declaring rule goes below…..

  if (condition 1)
    {
      // body goes here
    }   
      
 if else (condition 2)
   {
      // body goes here.
   }

else
  {
     // body goes here
  }

In this case, if the first condition is true, first body will be executed and other body will be ignored. Now if the first condition is false, second condition will be checked. If it is true, the 
 
 

1 comment:

  1. Here are some ideas i experinced myself and in addition read at
    other blogs. In 2009 Deadmau5 was nominated for a Grammy for his remix of the
    track "The Longest Road" by Morgan Page featuring Lissie in
    the category of "Best Remixed Recording, Non-Classical"In 2010 he won a Juno Award in the category of Dance Recording of the Year for
    the album "For Lack of a Better Name". · The 3D technology in
    "TRON: Legacy" was developed post-"Avatar" and represents the most advanced in the world
    today.

    my homepage daft punk 2013 rar

    ReplyDelete