For making a C++ program we basically use two types of statements which are conditional statements and loops

 

So first we will learn about conditional statements

 

In Conditional statements we put a particular condition and depending on the output of the statement that is true or false the decision regarding the execution of a particular set of statement is done

There are two conditional statements in C++

They are if and switch

if

The if statement has the same meaning as in English i.e. if this is true then this otherwise that ok I understand you are not clear let us take an example but before that let me tell you that along with if we have a else set of statements also.

Syntax:

if(condition)

{

Statements

}

else

{

Statements

}

example

#include<iostream.h>

void main()

{

int a;

cout<<”enter value of a:”;

cin>>a;

if(a!=0)

{

cout<<”a is nonzero”;

}

}

 

Now as you can see first we enter the number a and then the control comes to the if statements .here first the compiler checks the condition if the condition is true i.e. a not = 0 then the statements under the if braces are executed but if the condition is not true then the statements under the if are not executed.

 

Let me now demonstrate the use of else

#include<iostream.h>

void main()

{

int a;

cout<<”enter the value of a:”;

cin>>a;

if(a!=0)

{

cout<<”a is non zero”;

}

else

{

cout<<”a is zero”;

}

}

In this program we use if and else together .when the same condition as in the above program is encountered the compiler has to make decision weather to execute the if statements or the else statements. Here the decision is taken depending on the condition given if the condition is true then the statements  under if are executed but if it is false the statements under else are executed.

 

switch

The other conditional statement is switch. It is use only on a limited basis for some specific tasks. It is used to check a variable and depending upon the value given it executes the statements corresponding to the value entered .ok give me some time to explain. Take a look at its syntax first

Syntax;

switch(variable)

{

case 1:

Statements ;

break;

case 2:

Statements;

break;

Blah blah blah

case n:

Statements;

break;

default:

Statements;

}

For example:

 

 

#include <iostream.h>

void main() {

    cout << "enter the choice";

        int s

  cin>>s;

 

    switch (s)

 {

    case 1:

        cout << "Monday "

 

        break;

 

    case 2:

        cout << "Tuesday "

                     break;

 

    case 3:

        cout << "Wednesday";

        break;

 

    default:

        cout << "wrong choice "

            

     

    }

}

As you can see, the case statements is executed if the case constant matches the variable passed to switch, the default statement is executed if the variable does not match any case constant .it is to be noted that break is actually not required in the syntax of the switch but if we do not write break than the statement s following the matched case constant will also be executed i.e. break only sends the control to the end of the switch block. So you miss break and all statements following the matched case are executed which may not produce error but may harm the logic of the program entirely.

Note

To use the if statement you need some operators.these operators are given below

1.   ==  stands for equality operator checks weather the two numbers are true or not that’s

2.   != stands for not equal to this checks weather the two numbers are different or not

3.   >, <, >=, <= stand for the greater, less, greater or equal to and less or equal to operators, respectively.

4.   && is the logical and operator its working can be seen in the program blog.

5.   || is the logical or operator.

6.   ! is the logical not operator. It negates

 

Precedence

For a complex statement consisting of a number of operators then it becomes difficult for us to determine which operation will be executed first and which after that. So here the precedence of operators play a very important role. The precedence of operators is in the following order .It goes on decreasing as we go down.

++    ,   --                                     (increment and decrement operator)

*        ,    /     ,%                          (multiply, divide, modulus)

<        ,   <=      ,  >       ,  >=       (less than, greater than)

==     ,   !=                                   (equal, not equal)

&&                                              (logical and)

||                                                   (logical or)

?:                                                  (conditional expression)

=                                                   (assignment operator)

We can also use parenthesis for specifying the operations to be taken into consideration first and thereafter.



 

Make a Free Website with Yola.