Back to Top

Sunday 5 April 2015

Begginer's Guide to C++- Chapter 3: Selection Statements

Its time for the 3rd chapter. I hope you have understood the basics and were able to write your first program and had no trouble doing all the practice questions. In this chapter, we will learn to specify conditions on the statements that are being executed.


Learnt in previous chapter:
  1. Header files
  2. void main()
  3. Input/Output
  4. Your first program


if statement:
if specifies the condition under which a block of code should be executed. Understand this as "If this then what to do?" statement.
Syntax-
if(condition)
     {Your favorite code;
      }
But what if you want to specify more than one condition, you can use else statement.
Syntax-
if(condition 1)
     {First code;
      }
else if(condition 2)
     {Second code;
      }
else
     {Third code;         // consider else as a default statement, if no condition is specified, else block will be executed, you can skip this "else" block entirely if you wish.
      }
Code continues;


Eg- If I want to display "excellent" if x=1, "good" if x=2 and "OK" on any other value of x, we will write the following code.
if(x==1)             // Pay attention, I have written x==1, remember I told you about an == operator, well this is how you use it to check values.
{cout<<"Excellent ";
}
else if(x==2)
{cout<<" Good ";
}
else
{cout<<"OK";
}
cout<<" Can I get a +1 :-)"; //Execution resumes here, after 1 of the blocks above have been executed.


switch statement:
switch statement is also like a if, but it is just a bit different.
Syntax -
switch (variable name)                                  // Should only be an int or a char
{case 1:Your favorite code;               //For char, you can write case ‘a’ instead of case 1  break;
case 2:Code 2;
break;
default: default code;
}


Eg - If I want to write the previous example in switch statement,
switch(x)
{case 1:cout<<”Excellent”;
break;
case 2:cout<<”Good”;
   break;
default: cout<<”O.K.”
}


You will now want to ask me that how will you be able to check for inequalities or ranges in switch statement, the answer is that you can’t. It only checks the value for a constant, in short ranges are not allowed, in switch, for ranges you NEED to use if statement.


You can make menu driven programs from these statements, by creating a dummy variable, say int choice; and storing user choice in it, and then checking it for the corresponding value.
Eg- if I want to make a program with a menu like this-


What do you want to do?
  1. Add
  2. Subtract
  3. Exit


I can make the user enter the choice in a variable, and use it in switch case to execute that specific code. Now, let us proceed with the questions. But first, I want to know your opinion on this, some programmers have said that what I am currently teaching is extremely basic (Which according to me should be, as you all are beginners), so I have decided to upload a specific sequence of chapters which will have extra information, only for those who are interested, please let me know how you feel about it, by commenting below.


Practice Questions:


Q1- Write a program that takes 2 numbers, and a character like ‘+’, ‘-’, ‘*’, ‘/’, and does the appropriate calculation. Do this using-
i. if statement
ii. switch statement


Q2- Make a menu-driven program, that calculates the area of a triangle, or a circle, or a square, as selected by the user.