Sunday, February 1, 2009

Control Structure in c


C language possesses such decision making capabilities and supports the following
statements known as control or decision-making statements.

1. if statement
2. switch statement
3. Conditional operator statement
4. goto statement

if Statement
The if statement is a powerful decision making statement and is used to control
the flow of execution of statements. It is basically a two-way decision statement
and is used in conjunction with an expression.

Syntax
if (conditional)
{
block of statements executed if conditional is true;
}
else
{
block of statements if condition false;
}

Example
main()
{
int x=5

if (x > 1)
{
x=x+10;
}

printf("%d", x);

}

if…else statement
The if....else statement is an extension of the simple if statement. The general form is

if (condition)
{
True-block statement(s)
}
else
{
False-block statement(s)
}


If the condition is true, then the true-block statement(s), immediately following the if
statement are executed; otherwise the false-block statement(s) are executed.

void main(void)
{
int a, b;
char ch;

printf("Choice:\n");
printf("(A) Add, (S) Subtract, (M) Multiply, or (D) Divide?\n");
ch = getchar();
printf("\n");

printf("Enter a: ");
scanf("%d", &a);
printf("Enter b: ");
scanf("%d", &b);

if(ch=='A') printf("%d", a+b);
else if(ch=='S') printf("%d", a-b);
else if(ch=='M') printf("%d", a*b);
else if(ch=='D' && b!=0) printf("%d", a/b);

}

if-else-if statement

void main(void)
{
int numb;


printf("Type any Number : ");
scanf("%d", &numb);

if(numb > 0) {
printf("%d is the positive number", numb);
}
else if(numb < 0)
printf("%d is the Negative number", numb);
else printf("%d is zero",numb);


}
Switch Statement:
The switch and case statements help control complex conditional and branching operations.
The switch statement transfers control to a statement within its body.

Syntax:
switch (expression) {
case item:
statements;
break;
case item:
statements;
break;

case item:
statements;
break;
default:
statement;
break;
}

Example:
#include

main(){
int numb;
printf(“Type any Number”);
scanf(“%d”,&numb);

switch(numb %2)
{
case 0 : printf("the number %d is even \n", numb);

case 1 : printf("the number %d is odd \n", numb);
break;
}

}

Ternary condition

The ? (ternary condition) operator is a more efficient form for expressing simple if
statements. It has the following form:

expression1 ? expression2: expression3

Example:

res = (a>b) ? a : b;
if a is greater than b than res has the value a else the res has value b.

break statement

break statement is used to exit from a loop or a switch, control passing to the first
statement beyond the loop or a switch.

With loops, break can be used to force an early exit from the loop, or to implement a
loop with a test to exit in the middle of the loop body. A break within a loop should
always be protected within an if statement which provides the test to control the exit condition.

Example

For(i=0;i<=10;i++)

{

if(i==5){

break;

}

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

}

Output:

0

1

2

3

4

continue statement

continue is similar to the break statement but it only works within loops where its effect
is to force an immediate jump to the loop control statement.

Like a break, continue should be protected by an if statement.

Example

For(i=0;i<10;i++)

{

if(i==5){

continue;

}

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

}

Output:

0

1

2

3

4

6

7

8

9


The goto statement

The goto is a unconditional branching statement used to transfer control of the program from
one statement to another.

One must ensure not to use too much of goto statement in their program because its
functionality is limited. It is only recommended as a last resort if structured solutions are
much more complicated.

2 Comments:

Anonymous said...

Pls gave compleate information(Pls tell ur id i hav so many doughts in that concpet)

Attique qasim said...

plz splet u r contact i 2 hv many probles in that