Get control structures Homework Help from best programming assignment experts

Control Structures

The transfer of control from one statement in a program to other statement is done by using control structures.The control structures are classified as

  1. Unconditional statement
  2. Decision making statement
  3. Looping statements

Control Structures Homework Help

Unconditional statement

Goto

It is used to transfer the program control unconditionally from one point to another. The general form is

goto label;

Label is the name given to the point where the control has to be transferred. The general form is

label: statements;

Example

Goto L;

....

L: ....

Decision making statements

These are used to execute a group of statements based on the result of the program. The various decision making statements are

  • If statements
  • If....else statements
  • Nested if
  • Switch statement

If statement

Logical if statement is used to execute or skip statements or group of statements for a particular condition.

if(test condition)

{

statement;

}

next statement;

The computer first evaluates the value of the test condition. If the value is true , statement block is executed otherwise the next statement is executed sequentially.

Example:

#include

main()

{

int m;

scanf(“%d”,&mark);

if(m>35)

{

print(“pass”);

}

}

If ... else statement

It is used to execute one group of statements if the condition is true and other group of statements if the condition is

false.The general form is

if(test condition)

{

statement;

}

else

{

stament-2;

}

next statement;

The computer first executes value of the test condition. If the value is true statement 1 is executed and the control is moved to next statement. If the value is false, statement 2 is executed and the control is transferred to next statement.

Example

#include

main()

{

int m;

scanf(“%d”,&mark);

if(m>35)

print(“pass”);

else

print(“fail”);

}

Nested if statement

The joining of if...else statement either in the if block or in the else block or both. The general form is

if(test condition-1)

{

if(test condition-2)

{

Statement-1;

}

else

{

stament-2;

}

else

{

stament-3;

}

next statement;

Example:

#include

main()

{

int p,q,r;

scanf(“%d%d%d”,&a,&b,&c);

if(p>q)

{

if(p>r)

printf(‘big=%d”,p);

else

printf(‘big=%d”,q);

else

{

if(q>r)

printf(‘big=%d”,q);

else

printf(‘big=%d”,r);

}

}

}

Switch statement

This statement allows any  number of branches in the program. Any number of choices can be selected. The general form is

switch(expression)

{

case label1:

statement-1;

break;

case label2:

statement-2;

break;

.....

.....

case label1:

statement-n;

break;

default:

default statement;

break;

}

next statement

According to the user choice, the particular statement will be selected.

Example:

#include

main()

{

int d;

printf(‘enter a number between 1 and 3);

scanf(“%d”,&d);

switch(d)

{

case 1:

printf(“red\n”);

break;

case 2:

printf(“blue\n”);

break;

case 3:

printf(“pink\n”);

break;

default:

printf(“enter correct number\n”)

break;

}

}