Monday 5 June 2023

Control statements in C

What are Control statements?

In C programs, statements are performing sequentially in the organization in which they look in the program. But occasionally we may want to use a state for perform only a part of program. Also many affairs arise where we may want to program some statements some times. Control statements allow us to specify the order in which the many order in the program are to be executed. This control the flow of control. Control statements visibility how the control is move to other parts of the program. C language bear four types of control statements, which are as follows:

If ….else, loop, while, goto, switch, do…. while, for.

C programs, Loop, while, goto, switch, do
Control Statements in C language image-1

1.1   compound statement or block

A compound statement or a block is a set of statements surrounded within a match of wavy braces {}.

The statements inside the slab are implementing sequentially.

A compound statement is syntax similar to a one statement and can look anywhere in the program where a one statement is permit.

1.2   if…..else

If…else is a bi-directional conditional control statement. This statement is used to check a condition and take one of the two viable actions. If the condition is correct then a one statement or a block of statements is (one part of the program) performed, otherwise another single statement or a block of statements is (other part of the program) performed. Remember that in c, any nonzero value is regarded as true while zero is look on as false.

1.3   Nesting of if….else

We can have another if….else statement in the if chunk or the else hunk. This is called nesting of if….else statements. Here is an example of nesting where we have if…..else inside both if block and else block.

1.4   else if Ladder

This is a kind of nesting in which there is an if….else statement in each else part excepting the last else part. This type of nesting is often used in plan and also known as else if ladder. Here each condition is checked, and when a order is found to be true, the statements corresponding to that are programmed, and the control comes out of the nested form without checking still existing conditions. If none of the conditions is correct then the last else part is programmed.

1.5   Loops

Loops are used when we want to programmed a part of the executed or a block of statements some times. For examples, assume we want to print “C is best” 10 times. One way to get the wished output is – we write 10 printf statements, which is not better. Other way out us – use loop. Using loop we can write one loop statement and only one printf statement, and this speak to is definitely better than the first one.