top of page

Previously, we discussed about data types and and variables.

Now let us talk about control structures. If you already know control structures, you can skip this page.
 

Conditional Statements

 

C++ has 2 conditional statements and one conditional operators. viz. if, switch and ?:

if statement

 

if statement is used to test a condition and execute a statement if the condition is true. There can be an optional else statement which gets executed if the condition is false.

The condition to be tested is written in parenthesis.

 

Syntax :

if (condition) 

     statement1;

else 

    statement2; 

 

Let us look at an example.

Here we are reading m and testing if m is greater than 0. If the condition is true, first statement is executed and the program prints "You have entered a positive number". If the statement is false, second statement is executed and program prints "the number you typed is non-positive"

 

Note that if  clause should be followed by a single statements. If multiple statements are needed, then they must be enclosed in braces({ }) - statements inside braces becomes a block and are treated as one unit.

Let us look what happens if we try to add two statements to if block.

In the code two statements after if m(>0) the compiler gives a syntax error. To avoid this error we should write the two output statements inside curly brackets (braces).

 

We can write just an if without an else too.

           if(m<0)

           cout<<"Negative number";

        int n = m+10;

​​

Chained if and else 

 

We can write multiple  else if chains, when there is a need to check mutually exclusive conditions like the one shown below.

Let us look at another example.

 

In line one we are using a Boolean variable to test a condition. if(b) is same as if(b==true) 

In line two we are using m itself without any relational operators. if (m) as same as if(m!=0). Because in C++, 0 is false and non-zero is true.

 In line three we are using && for combining two condition with and operator.  The expression is true only if both the conditions - m==n and m>0 are true.
 

 Switch statement :

 

 When there are large number of conditions involving a single expression, instead of using if-else chain, you can use a switch statement.

Syntax

switch(expression)

{

     case value1:statement;

         statement;

         .....

         break;

    case value2:statement;

         statement;

         break;

         .....

    default: statement;

        statement;

        ....

}

There are some restrictions regarding the statement though.

  • expression used in switch must be an integer expression

  • value1, value2 etc should be integer literals. 

  • default is optional and that block is executed if none of the values match.

Let us look at an example. 

If the variable option is entered as 1, then case 1 is selected and a and b are added. And if option is entered as 2, case 2 is selected and so on.
 

 break?

break statement is used to break from switch and go out of that statement. We should use break after each of the cases.

If we omit to add break statement at the end of each case, the execution will seep through the subsequent cases.

 

In the above example, if we omit break after first case, and if option is 1, first ans=a+b; is executed, then ans = a-b; is also executed.

​​

Exercises:

  1. Write a program to read a number and print whether it is even or odd

  2. Write a program to find the product of 3 numbers read from keyboard

  3. Write a program to read 2 numbers and an option. If option is 1, add the numbers, if option is 2, subtract numbers, if it is 3, multiply numbers and if it is 4 divide the numbers.

  4. Write a program to convert read a length in cms and convert it into inches and feet. (1 inch is 2.54 cm and 12 inches is 1 foot.

bottom of page