Friday, April 4, 2008

Control Structures

Control Structures decide the flow of control.

Flow of control means which code line will be executed after which code line in a program. There are 3 types of flow in a computer program

1. Sequential

2. Branching

3. Iterations or Looping

Sequential:

The natural flow of control is sequential. This means the computer executes one line after another line of a program

Branching:

Based on the outcome of a condition a program branches to different lines of coding

A condition is true if it evaluates to a non-zero value

A condition is false if it evaluates to zero

Key words used for branching are – if, switch, goto.

The format of a branch with if :

1. if ( somecondition)

some action

2. if (some condition )

{

Action if the condition is true

-----------

}

else

{

Action if the condition is false

-----------

}


Nested if :-

An if within another if is called as nesting

if (condition)

if(condition)

Someaction ;

else

someaction ;

else

Someaction

Note : An else is related to a nearest if
Code Block : If you want more than one statement to be executed when a condition is satisfied then enclose them in a code block { }

goto : The keyword goto unconditionally transfers control to a label. Avoid using goto as it results in a code difficult to understand and maintain.

Format of goto :-

goto somelabel
------

------

somelabel :

someaction

switch

switch is a keyword. It is useful for multiple branching. Based on the value of a variable or an expression the program branches to different actions

Program:

void main( )

{

int option;

clrscr();

printf(“Enter an option [1 – 3 ] : “);

scanf(“ %d”, &option);

switch ( option )

{

case 1 :

printf( “ ONE “) ; break;

case 2 :

printf( “ TWO “) ; break;

case 3 :

printf( “ THREE “) ; break;

default :

printf(“ \n You did not enter in range [1 – 3 ] “);

}

}

Note:

· Switch is not a loop.

· We can pass int or char data type or an expression involving arithmetical operators to switch .

· Matching is done only once.

· If a match is found then subsequent statements are executed till a break or a closing brace is encountered. This is called CASCADING effect of a switch.

· If a match is not found then statements following default is executed.

· Default may be placed anywhere.

/* Program: Accept a character and display if it is a vowel or not. */

void main( )
{

char ch;
clrscr();
printf(“Enter a character : “);
scanf(“ %c”, &ch);

switch ( ch )

{

case ‘A’ : case ‘a’ : case ‘E’: case ‘e’:

case ‘I’ : case ‘I’ : case ‘O’: case ‘o’:

case ‘U’ : case ‘u’ :

printf( “VOWEL “); break;

default : printf(“ \n NOT A VOWEL “);

}

}

Loops or Iteration

Loop means your program keeps on executing the same lines of codes again and again until some condition is met . This is called as a loop. The programmer must ensure that a loop terminates properly Key words used for looping are – for, while, do-while.

Programs on looping:

E.g. of For loop:

main ( )

{

int i;

clrscr();

for ( i = 0 ; i < style=""> i++ )

printf(“%d\t”, i );

getch();

return 0;

}

Output:

0 1 2 3 4

E.g. of While loop:

main()

{

int i;

i=0;

while( i<5)

{

printf(“%d\t”, i);

i++;

}

printf(“\n I = %d “, i);

getch();

return 0;

}

Output:

0 1 2 3 4

I = 5

E.g of Do while loop:

main()

{

int i;

i = 0;

do{ printf( “%d”, i); } while( i > 10);

getch();

return 0;

}

Output:

0

Nested Loops

Nested loops means one loop inside another loop. The inner loop executes fully from initialize to finish for every single execution of the outer loop

For e.g :

for(…….) /* Outer loop */

{ -----

-----

for(…….) /* Inner loop */

{ -----

}

}



for( i=0, k=1; i < 5 ; i++)

for(j=0; j<3; j++ , k++)

printf(“%d\t ”, k);

NOTE:
The inner loop executes from start to finish from every one pass of the outer loop.
The values of i, j , k can be traced as follows: ( Dry run)

i j k

0 0 1

0 1 2

0 2 3

1 0 4

1 1 5

1 2 6

2 0 7

2 1 8

2 2 9

3 0 10

3 1 11

3 2 12

4 0 13

4 1 14

4 2 15


continue and break

Continue: Transfers control to the top of the loop ignoring further statements.

Break: It breaks the execution of the loop in between and goes out of the loop.

Program:
main( )
{

int i=0;

while ( i <>

{

printf(“i=%d”, i);

i+=5;

if ( i == 5)

continue;

printf(“\n i = %d “, i);

if ( i == 10 )

break;

printf(“ \n GOOD “);

}

printf(“\n OK ! “);
getch();

return 0;

}

No comments: