answersLogoWhite

0


Best Answer

A control statement is any statement that affects the flow of execution.

The simplest control statement is the if statement:

if (x!=0) x++;

When execution reaches this statement, the control expression, x!=0, is evaluated. If it evaluates false (in which case x is zero) execution moves onto the next statement. However, if it evaluates true (in which case x is non-zero), the remainder of the statement is executed, incrementing x before moving onto the next statement.

The controlled statement may also be a compound statement:

if (x!=0) { x++;

x*=2;

}

As before, x!=0 is evaluated. If false, execution passes to the statement following the closing brace. If true, the compound statement is executed, first incrementing x and then doubling x. Execution then falls through to the next statement after the closing brace.

Sometimes we may wish to execute a different statement when the control expression evaluates false. In this case we use an if-else statement:

if (x==0)

x++;

else

x*=2;

Here we evaluate x==0. If true, we increment x but if false, we double x. Only one or the other statement executes, never both. Whichever is executed, execution falls through to the next statement. Again, the controlled statement may be a compound statement.

The else clause of an if statement can also be another if statement, known as a nested if:

if (x==0)

x++;

else if (x==1)

x*=2;

else if (x==2)

x*=3;

else

x*=4;

Nested ifs are evaluated in the order they appear. However, when the control expressions contain a common sub-expression (here, x), a switch is usually a better choice:

switch (x) {

case 0: x++; break

case 1: x*=2; break;

case 2: x*=3; break;

default: x*=4;

}

Here, x is evaluated and execution passes to the corresponding case label. Execution continues from that point unless a break statement is encountered, at which point execution passes to the statement that follows the closing brace of the switch statement.

Note that a break statement is also a control statement. We can also use return (another control statement) to break out of a switch.

Loops are also control statements:

for (int x=0; x<10; ++x) printf ("%d ", x);

Here we assign 0 to x, then evaluate the control expression, x<10. This is true (zero is less than ten), so the printf() function executes. We then execute the action expression, ++x, before evaluating the control expression again. this time x is 1 and 1<10 is true, so the printf() function executes again. We continue incrementing x and evaluating x<10. So long as x<10 remains true, the printf() function is invoked. After 10 iterations, x will be 10, at which point x<10 evaluates false, so execution now passes to the next statement.

We can implement the same loop using a while statement:

int x = -1;

while (++x<10) printf ("%d ", x);

Here, the initialiser is outside the loop, thus x is now in scope when the loop ends. We can do the same with for loops, provided the control variable is in scope before we enter the loop. Note that the action (++x) is now a sub-expression of the control expression. It is a matter of taste whether this code is more readable than a for loop, however for loops are arguably better suited to counted loops.

While loops are better suited to "infinite" loops such as message queues:

Message msg;

while (true) { // execute "for ever"

idle();

msg = parse_message ();

if (msg==EXIT) break;

process_message (msg);

}

This is a stylised (simplified) message queue. Here, we execute the loop continuously. We start by entering idle mode which returns when there is a message on the queue. We then parse the message. If the msg is EXIT, we break out of the loop. Otherwise we dispatch the message and repeat from the beginning. Note that "infinite" loops are never truly infinite: you must provide some means of breaking out of the loop at some point. Here we used the EXIT message.

Another control statement is the continue statement. This only appears in loops and can be used to skip over any remaining statements in the loop without breaking out of the loop.

int x = 0;

do {

++x;

if (x==5) continue;

printf ("%d ", x);

} while (x<10);

Here we print the value of x until x is 10. However, when x is 5, we do not print x, we jump to the control expression where x<10 is re-evaluated.

Note the final example shows a do-while loop and is used whenever we wish the body of the loop to execute at least once, regardless of the control expression which is evaluated at the end of each iteration, rather than at the beginning. However, the type of loop does not matter; the continue statement always skips the remainder of the loop but does not exit the loop.

A goto statement is also a control statement. However, use goto sparingly and only when required. Code that jumps about too much is difficult to read; code that is hard to read is also hard to maintain. The same can also be said of break, continue and return statements.

Function calls are also considered control statements because they (usually) interrupt the flow of execution via the call and return mechanism. The exceptions are inline expanded functions which give you all the advantages of a function (i.e., localisation of common code) without the expense of a function call.

User Avatar

Wiki User

7y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is control statement give answer with its types?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering
Related questions

What is a control statement?

The controlling statement controls the writing you will do


Give you a prejudicial rhetorical statement?

Give you a prejudicial rhetorical statement?


What types of statement can be used to explain the steps of a proof?

The corollaries types of statement is what is used to explain the steps of a proof.


What types of the statement can be used to explain the steps of a proof?

The corollaries types of statement is what is used to explain the steps of a proof.


What is difference between control statements and looping statements in c language?

differance between control statement and looping statement?


What statement about control group is false?

the control group is recieving the medication


What is the purpose of break statement in c?

The break statement exits control of the innermost for, while or do-while loop, or switch statement.


What are the Types of income statement?

1. Single step income statement 2 &acirc;&euro;&ldquo; Multi-step income statement


African ethics is at crossroads the statement true.why.explain what you think is meant by the statement and give reasons for such a phenomenon?

African ethics is at crossroads the statement true.why.explain what you think is meant by the statement and give reasons for such a phenomenon?


What are control structures used in javascript?

The control structures used in java script are if-statement, for-loop, for-in loop, while loop,do-while loop, switch-statement, with-statement. try-catch-finally statements.


What is an audience response statement?

If someone is giving you a statement about something you're arguing you give him a response statement .


How is a fund flow statement prepared give answer with imaginary figures?

how is a fund flow statement prepared ?give your answer with imaginary figure.