Following are the control structures that are used in C + + :

A) Decision Making Statements:-

1.if statement

2.if-else statement

3.switch statement

B) Looping statements:-

1.for statement

2.while statement

3.do-while statement

C) Miscellaneous statement:-

 1.break statement

2.continue statement

3.goto statement

if statement –

1.The general syntax of if statement is given as follows

if (conditional expression)

2.If the conditional expression results in value true then the statement part will be executed. If the conditional expression results in           value false then the statement part will not be executed.

3.The statement can be any valid C + + statement.

4. Example

if (m > = 40)

cout << “pass\n”;

if-else statement-

1.The general syntax of if-else statement is given as follows.

if (conditional expression)

statement 1;

else

statement 2;

2 .If the conditional expression results in value true then the statement 1 will executed and if the conditional expression results in value false then the statement 2 will be executed.

3.The statement can be any valid C + + statement.

4.example 1 :-

if (m>=40)

cout << “pass\n”;

else

cout << “Fail\n”;

Example 2 :-

if (m < 40)

{

cout << “Better luck next time\n”;

}

else

{

cout <<”Pass\n”;

cout<< “Congratulations \n”;

}

 

Nested if – else statements:-

1. When if- statement or if – else statement itself acts as statement part of the other if or if – else statement then that results into nested if-else statement.

2.There is no limit for the depth of nesting.

3.The nested if-else statement can result into number of combinations.Consider the following example that shows one particular combination of the nested if-else statement.

4. Example :-

if (case 1)

if (case 2)

S1;

else

S2;

else

if (case 3)

S3;

else

S4;

Explanation :-

case 1          case 2                   case 3                   executed statements

False           False                     False                              S4

False           False                     True                               S3

False           True                     False                              S4

False           True                     True                               S3

 True            False                     False                              S2

True            False                     True                               S2

True            True                     False                              S1

True            True                     True                               S1

 

Note : When we have a complicated Nested if else without any curly brackets, then to understand which else is associated with which if, we        can apply the following logic.

soln / logic :- Scan the Nested if else from top to bottom and search for the else. As      soon as you get the else, associate it with the previous nearest and un-  associated if.

1.Wap that accepts marks of a single subject out of 100. The pgm should   print the class of a student.

If marks > = 70 Then the class is distinction if marks > = 60 AND if        marks < 70 Then the class is first class.

If marks > =50 and if marks < 60 then the class is second class

If marks > = 40 and if marks <50 the class is pass class.

If marks < 40 then the class is fail.

 Ans:

  # include

# include

void main ( )

{

int m ;

clrscr ( );

cout << “ Enter marks out of 100 =” ;

cin >> m;

if (c > = 70)

cout << “ Distinction \n” ;

else

if ( m > =60)

cout << “ First class \n”;

else if ( m > = 50)

cout << “ second class \n”;

 else if ( m > = 40)

cout << “pass \n”;

else

cout << “ Fail \n”;

getch ( );

}

 

2)  WAP that accepts three integers and finds out the highest of them.

→      # include

# include

void main ( )

{

int a, b, c;

clrscr( ) ;

cout << “ Enter the value of a, b, & c \n” ;

cin >> a >> b >> c;

if ( a = = b & & b = = c)

cout << “All numbers are equal” ;

else

{

if ( a > b & & a > c)

cout<< “Highest =” << a << ‘\n’;

else if (b > a & & b > c)

cout << “Highest =” << b << ‘\n’;

else

cout << “Highest = “<< c << ‘\n’;

}

getch ( );

 }

3. WAP that accepts one positive integer and prints whether the number    is divisible by 7 or not

# include

# include

void main ( )

{

int n;

clrscr ( ) ;

cout << “Enter the value of n”<< ‘\n’;

cin >> n;

if ( n% 7 = = 0)

cout << n << “ is divisible by 7\n”;

else

cout<< n << “is not divisible by 7\n”;

getch ( );

}

 

4. WAP that accepts a positive integer and prints whether it is ever    number or not

# include

# include

void main ( )

{

int a ;

clrscr ( );

cout << “ Enter the value of a” ;

cin >> a;

if (a% 2 = =0)

cout << a << “ is a even number \n” ;

else

cout << a << “is a even number \n” ;

else

 cout << a << “ is a odd number \n” ;

getch ( );

}

 

Switch statement :-

1.The general syntax of the switch statement is as follows.

 

switch (expression)

{

case 1 : statement 1 ; break ;

case 2 : statement 2 ; break ;

case 3 : statement 3 ; break ;

…………………

…………………

case n : statement n ; break;

deafault : statement p ;

}

Explanation :-

1. First of all the expression that is written within the switch will be solved to result into a singe value.

 2. If the value of the expression matches with case 1 then the statement 1 will be executed and the corresponding break statement will take the     control out of the switch statement.

3. If the value of the expression is not matching with case 1 then it will be compared with case 2 and if the match is found then the statement 2 will be executed and the corresponding break statement will take the control out of the switch statement.

4.If the value of the expression is not matching with case 2 then it will be compared with further cases. If the value of the expression does not match with any of the case then the default statement i.e. the statement p will be executed. Since, the default part is physically written at the end of the switch statement, the control will come out of the switch statement without writing the break statement with the default part.

Note :- The default part of the switch statement need not be written at the end of the switch . It means we can write it at the beginning of the switch or it can be written anywhere in between within the switch. Writing the default part within the switch statement is not compulsory.

example ;-

int a ;

cout < < “Enter one integer = ” ;

cin > > x ;

switch (x)

{

case 1 : cout << “3\n”; break ;

case 2 : cout << “6\n” ; break ;

case 3 : cout << “ 9\n” ; break ;

case 4 : cout << “12\n” ; break;

default : cout << “30\n” ; break ;

}

O/P

x                 1        2        3        4        Anything other than 1 to 4

out put        3        6        9        12      30

 

1.WAP that accept a digit that represent reading on a die when thrown.  If the digit is 1 or 3 or 5 then display the message “Odd Number”. If   the digit is 2 or 4 or 6 then the message displayed should be “Even      Number”. Otherwise the message should, be “Faulty Die”.

# include

# include

void main ( )

{

int x ;

clrscr ( );

cout << “Enter the value of x =” ;

cin >> x ;

{

case 1:

case 3 :

case 5: cout << “Odd Number” ; break;

case 2:

case 4:

case 6: cout << “Enter Number” ; break;

default : cout << “Faulty die” ;

getch ( ) ;

}

2 .WAP that accepts one alphabet and prints the colour name starting with that alphabet.

# include

# include

void main ( )

{

char x;

clrscr ( );

cout << “Enter one alphabet =”;

cin>> x ;

switch (x)

{

case r :

case R : cout<< “Red \n” : break;

case g :

case G : cout << “Gray, Green \n” ; break ;

case b :

case B : cout << “ Blue, Black \n” ; break ;

default : cout << “Unknown color” ;

}

getch ( );

}

 

 3 . WAP that will ask user to enter one digit and depending on the digit entered by user the pgm should display the suitable message.

1. If the digit is 2 or 3 or 6 or 7 then the message should be “Good Morning”}

2. If the digit is 1 or 4 or 5 or 8 then the message should be “Good     afternoon.

3. If the digit is 0 or 9 then the message should be “Good Evening”

4.If user enters anything other than 0 to 9 then the message should be         “Good Night”

# include

# include

void main ( )

{

int x;

clrscr ( );

cout << “Enter the value of x”;

cin >> x;

switch (x)

{

case 2 :

case 3 :

case 6 :

case 7 : cout << “Good Morning\n ; break ;

case 1:

case 4:

case 5:

case 8: cout << “Good Afternoon”; break;

case 0:

case 9: cout << Good evening “; break;

default : cout << “Good Night”;

}

getch ( );

}

 

4. WAP that accepts option from user. The options can be ‘+’ or ‘–‘or ‘*’ ‘/’ . After accepting the option, the pgm should accept the two and should print the appropriate result depending on the option.

# include

# include

void main ( )

{

char option ;

int a , b ;

clrscr ( ) ;

cout << “Enter your option ( + - * /) =” ;

cin >>option ;

if ( option = = ‘ + ¦¦ option= = ‘_’ ¦¦option = = ‘*’ ¦¦ option = = ‘/’ )

{

cout <<”Enter two integer =” ;

cin >> x>> y ;

switch (option)

{

case ‘ +’ : cout << x + y << ‘/in’ ; break ;

case ‘-‘ : cout << x - y << ‘/in’ ; break ;

case ‘*‘ : cout << x * y << ‘/in’ ; break ;

case ‘/‘ : cout << float (x) / y << ‘/in’ ; break ;

}

}

else

cout << “Invalid option \n” ;

getch ( ) ;

}

5. WAP that accepts three numbers representing coefficients of a        quadratic equation. Your pgm should print roots of the quadratic     equation by considering all possibilities.

Logic : ax2 + bx + c = 0 ( a ≠ 0)

 If b2 – 4ac > 0 roots are real & unequal

If b2 – 4ac = 0 roots are equal & real

If b2 – 4ac < 0 roots are imaginary & unequal

The quantity b2-4ac is called discriminate and denoted by D.

# include

# include

# include

void main ( )

{

int a, b, c, r1, r2;

cout << “Enter the values of a, b, c :” <

cin >> a;

cin>>b;

cin>> c;

if (( b* b – 4* a *c) >0)

{

r1=(–b + sqr + (b*b – 4*a*c)) / (2*a)) ;

r2=(–b – sqr – (b*b – 4*a*c)) / (2*a)) ;

cout << “One root is” << r1<< endl ;

cout << “Second root is” << r2<

}

else

If ((b*b – 4*a*c = + 0))

{

r1= –b /(2*a);

r2= – b/(c2*a);

cout << “root1is” << r1endl;

 cout<< “root2is”<< r2endl;

}

else

If ((b*b – 4*a*c) <0)

{

r1 = (– b + sqr + (b*b – 4*a*c)) / (2*a);

r2 = (– b –sqr + (b*b – 4*a*c)) / (2*a);

cout << “Root 1 is imaginary” << r1;

cout << “Root 2 is imaginary” << r2;

}

getch( ) ;

}

 

Loops :-

  1. The loops are used to repeat the execution of a statement or group of statements.
  2. The loops are useful for the application in which certain tasks are to be repeated.
  3. The or statement, while statement, and do-while statement are the loops used in C ++

For Statement:-

  1. The general syntax of for statement is as follows

for (expression1; expression2; expression 3) statement;

  1. The expression 1 is called as initialization expression. The expression 2 is called as conditional expression. The expression 3 is called as modifier expression.
  2. The statement can be any valid C ++ statement.
  3. Initially the control of execution will come to the expression 1 and it will be executed. After that the control of execution will come out of for loop without executing the statement part at all. If the expression 2 results in value true then the statement part will be executed and after executing the statement part the control goes to the expression 3 and it is executed.

 I.After executing the expression. The control comes back to the expression 2 and it will be executed. If the expression 2 is true again, then the statement part will be executed again and the control will go back to expression 3 again and it will be evaluated. The control will then be back to the expression 2.

II.The above process will be repeated as long as the expression 2 is true and as soon as the expression 2 becomes false then the control will come out of the for loop.

Example:-

for (i = 1 ; i < = 4 ; i ++)                     

cout << i << ‘\n’                 

 

o/p  

  1

2

3

4

5

 

While statement :-

1) The general syntax of the while loop or while statement is as follows.

while (conditional expression) statement;

2) First of all, the control of execution will come to the conditional     expression and it will be executed. If the conditional expression is        false in the very first comparison then the control of execution will       come out of the while loop without executing the statement part at all.

3) If the conditional expression results in value true then the statement part will be executed. After executing the statement part, the control of execution will be back to the conditional expression and it will be reevaluated. If it is true, again the statement part will be executed the control of execution will be back to the conditional expression again.

4) This process will be repeated as long as the conditional expression is       true. As soon as, the conditional expression becomes false, the control        of execution will come out of the while loop.

Example :-

1)      i = 1;                                          output : 1

while (i < = 5)                                           2

{                                                               3

cout << i << ‘\n’ ;                                     4

i + + ;                                                       5

}

2)      i = 1                                           output : 1

while (i < = 5)                                           2

cout << i + + << ‘\n’ ;                             3

4

5

 

3)      i = 5 ;                                         output : 5

while (i > = 1)                                           4

{                                                               3

cout << i << ‘\n’;                                      2

i – – ;                                                        1

}

4)      i = 5 ;

while (i > = 1)

cout << – – i << ‘\n’;

Output :-         4

3

2

1

0

do–while statement :-

1) The general syntax of the do-while statement is as follows:

do

{

statement 1;

statement 2;

.   .   .   .

. .   .   .   .

} while (conditional expression);

2) First of all, the control of execution will come to the statement part and it will be executed. After executing the statement part, the control

of execution will come to the conditional expression and it will be checked.

3) If the conditional expression is false in the very first comparison then the control of execution will come out of the do-while loop.

4) If the conditional expression is true, then the control of execution will go back to the statement part and it will be executed again. After executing the statement part, again the conditional expression is checked. If the conditional expression is true again then the control will go back to the statement part.

 5) The process will be repeated as long as the conditional expression is true. As soon as the conditional expression is false, the control of execution will come out of the do-while loop.

 

Example :-   i = 1;                               Output :-

do                                                         1

{                                             2

cout <      3

i + +;                                    4

}                                                    5

while (i < = 5);

 

Break statement:-

1        The break statement is used to take the control of execution abruptly out of the existing control structure. Thus, we can say that the break statement breaks the normal sequential flow.

2        The break statement can be used within

a        switch statement

b        For, while and do-while statement

→      break statement with in switch statement:-

Refer the topic, switch statement for examples and programs.

→      break statement within the loop:-

When the break statement within the loop gets executed then the control of execution will come out of the loop abruptly. Consider the following examples.

example:-

for(i =1;i < = 100; i++)

if (i > 5) break;

else cout << i ;

output:-

1 2 3 4 5

Explanation:-

When the value of i will become 6 then the break statement will executed and the control of execution will come out of the for loop abruptly. We can avoid the break statement in the above

example and can still get the same output. Thus, the same output can be obtained with the better logic, as follows

Better Logic:-

for (i =1; i < = 5; i++)

cout << i;

 

Explanation:-

When the value of i is 1, then the condition i%7!=o is true and hence the continue statement gets executed. Due to the execution of the     continue statement, the control of execution will continue to the next iteration i.e. the expression 3 of the for loop i.e. i++ and thus the cout is skipped.

Similar will be the action for all the values of i from 2 to 6. When the value of i will become 7, then the condition i%7!=0 will become false and hence the continue statement is not executed and the vale 7 is printed and the control of execution will go to the next iteration normally.

The process will be repeated for the further iteration. During the process, all the multiples of 7 upto 100 will be printed. The same output can be obtained without using continues statement. The better logic to give the same output without using continues statement can be given as follows.

for (i =1; i < = 100; i++)

if (i % 7 = = 0)

cout << i << ‘ ’ ;

All the values for which the condition i % 7 = = 0 is true are printed i.e. all multiples of 7 upto 100 are printed

1        The continue statement is used within the loops. When the continue statement within the loop gets executed, then the control of execution will continue to the next iteration of the loop. All the further statements in the current iteration of the loop are skipped.

2        When the continue statement within the for-loop gets executed then the control of execution continues to the expression 3 of the for loop.

3        When the continue statement within the while loop gets executed then      the control of execution continues to the conditional expression of the while loop.

4        When the continue statement within the do-while loop gets executed then the control of execution contributes to the conditional expression of the do-while loop.

Example 1:-

for (i =1; i < =100; i++)

{

if(i %7!=0)continue;

cout << i << ‘ ’;

}

output:-

7        14      21      28      35      42      49      56      63      70      77 84 91      98

 

 

 goto statement:-

1.The goto statement is used to `take the control of execution abruptly anywhere within any portion of the pgm.

2.The general syntax of the goto statement is as follows.

goto label

When the goto statement is executed then the control of execution will go to the labelled statement.

consider the following example.

fact = 1;

i = 1;

abc : fact = fact* i;

i++;

if (i < = n) goto abc;

cout<< “Factorial of” << n << “ = ” << fact;

 

 


Like it on Facebook, Tweet it or share this article on other bookmarking websites.

No comments