Tuesday, June 18, 2013

Condition Checking (if else and switch case)


Motivation :
suppose you want to write a program that takes a particular action 
depending on the user's input. That is your want your program to execute a particular section of code if the user gives a particular input and not otherwise. For condition like these we need to do what is known as condition checking
c++ provides two methods to do these :
1> if else :
syntax :
                //1st indentation style
                if(expression or condition) {
                                     //body of the 'if' write the program logic here
                } else {
                                     //body of the 'else' write the program logic here
                }
                
                //2nd indentation style
             if(expression or condition) 
               {
                                      //body of the 'if' write the program logic here
                } 
                else
               {
                                     //body of the 'else' write the program logic here
                }
note : you do not need to give the curly brackets if the if's body contains only one statement.
Working : When the control reaches the if(condition) line then it checks the condition and if found true then the control enters the if body and if found false it enters the else part.

Let's see an example to see the use of if and else
(it checks if a entered no  is odd or even)











































Explanation : The program is simple here we take a number 'n' as input from the user using 'cin' and then use the modulus(%) operator to check if after dividing it by 2 the remainder is 1 or 0. If it is even it will leave a remainder of '0' so it will enter the if body and if it is 1 then the if condition will be false and so it will enter the  else part. 


                                  if - else if ladder


Motivation : now let's assume that you want to write a program for calculating your electric bill, then you need to check a lot of conditions as different different ranges have different different values.
for that c++ provides something known as if - else if ladder.

Syntax : 
               //you can figure the next indentation style by yourself
             if(condition) {
                      //your code
              
              } else if(condition) {
                       //control enters here only if the first if's condition is false
                      //your code
            
              } else if(condition) {
                      //control enters here only if the previous else if's condition 
                      // is false
                     //your code
              } else {
                     //control enters here only if none of the conditions
                     // are true
                    //your code
               
              }


Nested ifs ans elses :

Motivation : now let's assume that you want to write a program where 
you want to check a contition that is too long to write in a line 
(becomes to messy if written in one line) 
or 
condition is too complicated to be written all at once than for your 
safety(safety as in to prevent you from making errors) and to make the program more readable. you should use what is known as nested if else.

Nested : if's are said to be nested when the body of an if is completely encapsulated within the body of another if.

Syntax :

              if(condition) { // this is the outer if's body
                 
                          if(condition) { //this is the encapsulated if's body
               
                          } 
              }
            
               or 
               if(condition) { // this is the outer if's body
                 
                          if(condition) { //this is the encapsulated if's body
               
                          } else {
                            
                          }
                 }
if-else if ladder :
let's look at a code  for more clarity :
Explanation the first if checks the the age to be between 10 and 20 remember the and logical operator now the control enters the 
else if (age < 30) only when the first if is false and it will enter the next if-else if only if the previous one is false and so on.

same code could also be written with only if's and else(i leave it as an exercise )
will upload an hint in the google drive you can refer to that if you are having problem

nested if else :
let's look at a code  for more clarity :

Explanation : the above program uses nested if concept first the age of the user is checked and then the profession is checked making the code look elegant and easy to read this same effect could have been obtained by the and logical operator.
like this way : 
                       if(age > 20 && age < 30 && profession == 'a') {
                                      cout<<"your code here";
                    }
here is the output :

2>  switch case :              

motivation :  well all of you are already motivated. When you have a single decision variable (the variable which determines what part of code to execute)
then switch case is really helpfull.

Syntax :
               switch(variable) {
                            
                           case val1 :
                                              //control enters here if value of variable = val1   
                                             // you code here
                                             break;
                           case val2 :
                                              //control enters here if value of variable = val2
                                             // you code here
                                             break;
                           case val3 :
                                              //control enters here if value of variable = val3
                                             // you code here
                                             break;
                                  .
                                  .
                                  .
                                  .
                           case valn :
                                              //control enters here if value of variable = valn
                                             // you code here
                                             break;
                           default :
                                              //control enters here if value of variable not  
                                              //equal to anything
                                             //warning that the variable does not match any
                                             //of the given values
               }

Break : it is statement which when encountered the control skips the current active block and moves to the first statement of the outer block.
 (not so clear!!!) ok the diagram below will make it more clear.
the use of break in switch case is to ensure that only the case with matches the value of the variable is executed. If it is not given then all the cases after the case that matches the value will be executed.  
In order to understand i encourage you to download the source code of the program below from google drive link at the end of the post and remove the break statements to see what is happening.

here is a program illustrating the use of a switch statement :

the source file of the programs are uploaded in google drive in Learning c++  folder




















No comments:

Post a Comment