Unlike break statement, which is used to jump the control out of the loop, it is sometimes required to skip some part of the loop and to continue the execution with next loop iteration. Continue statement used inside the loop helps to bypass the section of a loop and passes the control to the beginning of the loop to continue the execution with the next loop iteration. The syntax is as follows:
continue;
In a C program, a decision causes a one-time jump to a different part of the program, depending on the value of an expression. Decisions in C can be made in several ways. The most important is with the if...else statement, which chooses between two alternatives. This statement can be used without the else, as a simple if statement. Another decision control statement, switch, creates branches for multiple alternative sections of code, depending on the value of a single variable.
The if Statement
It is used to execute an instruction or sequence/block of instructions only if a condition is fulfilled. In if statements, expression is evaluated first and then, depending on whether the value of the expression (relation or condition) is “true” or “false”, it transfers the control to a particular statement or a group of statements.
Different forms of implementation if-statement are:
>> Simple if statement
It is used to execute an instruction or block of instructions only if a condition is fulfilled. The syntax is as follows:
if (condition)
statement;
where condition is the expression that is to be evaluated. If this condition is true,
statement is executed. If it is false, statement is ignored (not executed) and the
program continues on the next instruction after the conditional statement.
>> If … else statement
If…else statement is used when a different sequence of instructions is to be executed depending on the logical value (True / False) of the condition evaluated. Its form used in conjunction with if and the syntax is as follows:
if (condition)
Statement _1;
else
Statement_ 2;
statement_3;
If the condition is true, then the sequence of statements (Statements_1_Block) executes; otherwise the Statements_2_Block following the else part of if-else statement will get executed. In both the cases, the control is then transferred to Statements_3 to follow sequential execution of the program.
>> Nested if…else statement
In nested if… else statement, an entire if…else construct is written within either the body of the if statement or the body of an else statement. The syntax is as follows:
if (condition_1) {
if (condition_2) {
Statements_1_Block;
} else {
Statements_2_Block;
}
} else {
Statements_3_Block;
}
Statement_4_Block;
Here, condition_1 is evaluated. If it is false then Statements_3_Block is executed and is followed by the execution of Statements_4_Block, otherwise if condition_1 is true, then condition_2 is evaluated. Statements_1_Block is executed when condition_2 is true otherwise Statements_2_Block is executed and then the control is transferred to Statements_4_Block.
>> Else if statement
To show a multi-way decision based on several conditions, we use the else if
statement. This works by cascading of several comparisons. As soon as one of the
conditions is true, the statement or block of statements following them is executed and
no further comparisons are performed. The syntax is as follows:
if (condition_1) {
Here, the conditions are evaluated in order from top to bottom. As soon as any condition evaluates to true, then the statement associated with the given condition is executed and control is transferred to Statements_x skipping the rest of the conditions following it. But if all conditions evaluate false, then the statement following final else is executed followed by the execution of Statements_x.
The Switch Statement
Its objective is to check several possible constant values for an expression, something similar to what we had studied in the earlier sections, with the linking of several if and else if statements. When the actions to be taken depending on the value of control variable, are large in number, then the use of control structure Nested if…else makes the program complex. There switch statement can be used. Its form is the following:
switch (expression){
case expression 1:
block of instructions 1
break;
case expression 2:
block of instructions 2
break;
.
.
default:
default block of instructions
}
It works in the following way: switch evaluates expression and checks if it is equivalent to expression1. If it is, it executes block of instructions 1 until it finds the break keyword, moment at finds the control will go to the end of the switch. If expression was not equal to expression 1 it will check whether expression is equivalent to expression 2. If it is, it will execute block of instructions 2 until it finds the break keyword.
Finally, if the value of expression has not matched any of the previously specified
constants (you may specify as many case statements as values you want to check), the
program will execute the instructions included in the default: section, if it exists, as it
is an optional statement.
Loop control statements are used when a section of code may either be executed a fixed number of times, or while some condition is true. C gives you a choice of three types of loop statements, while, do- while and for.
The While Loop
When in a program a single statement or a certain group of statements are to be executed repeatedly depending upon certain test condition, then while statement is used. The syntax is as follows:
while (test condition)
{
body_of_the_loop;
}
Here, test condition is an expression that controls how long the loop keeps running. Body of the loop is a statement or group of statements enclosed in braces and are repeatedly executed till the value of test condition evaluates to true. As soon as the condition evaluates to false, the control jumps to the first statement following the while statement. If condition initially itself is false, the body of the loop will never be executed. While loop is sometimes called as entry-control loop, as it controls the execution of the body of the loop depending upon the value of the test condition
The do...while Loop
There is another loop control structure which is very similar to the while statement – called as the do.. while statement. The only difference is that the expression which determines whether to carry on looping is evaluated at the end of each loop. The syntax is as follows:
do
{
statement(s);
} while(test condition);
In do-while loop, the body of loop is executed at least once before the condition is evaluated. Then the loop repeats body as long as condition is true. However, in while loop, the statement doesn’t execute the body of the loop even once, if condition is false. That is why do-while loop is also called exit-control loop.
The for Loop
for statement makes it more convenient to count iterations of a loop and works well where the number of iterations of the loop is known before the loop is entered. The syntax is as follows:
for (initialization; test condition; increment or decrement)
{
Statement(s);
}
The main purpose is to repeat statement while condition remains true, like the while
loop. But in addition, for provides places to specify an initialization instruction and an
increment or decrement of the control variable instruction. So this loop is specially
designed to perform a repetitive action with a counter.
The Nested Loops
C allows loops to be nested, that is, one loop may be inside another. The program given below illustrates the nesting of loops.
Write a program to generate the following pattern given below:
1
1 2
1 2 3
1 2 3 4
/* Program to print the pattern */
int main() {
int i, j;
for (i = 1; i <= 4; ++i) {
printf("%d\n", i);
for (j = 1; j <= i; ++j)
printf("%d\t", j);
}
return 0;
}
Here, an inner for loop is written inside the outer for loop. For every value of i, j
takes the value from 1 to i and then value of i is incremented and next iteration of
outer loop starts ranging j value from 1 to i.
The goto statement is used to alter the normal sequence of program instructions by transferring the control to some other portion of the program. The syntax is as follows:
goto label;
Here, label is an identifier that is used to label the statement to which control will be transferred. The targeted statement must be preceded by the unique label followed by colon.
label : statement;
Although goto statement is used to alter the normal sequence of program execution but its usage in the program should be avoided. The most common applications are:
goto can never be used to jump into the loop from outside and it should be preferably used for forward jump.
Situations may arise, however, in which the goto statement can be useful. To the
possible extent, the use of the goto statement should generally be avoided.
Sometimes, it is required to jump out of a loop irrespective of the conditional test value. Break statement is used inside any loop to allow the control jump to the immediate statement following the loop. The syntax is as follows:
break;
When nested loops are used, then break jumps the control from the loop where it has
been used. Break statement can be used inside any loop i.e., while, do-while, for and
also in switch statement
Unlike break statement, which is used to jump the control out of the loop, it is sometimes required to skip some part of the loop and to continue the execution with next loop iteration. Continue statement used inside the loop helps to bypass the section of a loop and passes the control to the beginning of the loop to continue the execution with the next loop iteration. The syntax is as follows:
continue;
John Doe
5 min agoLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
ReplyJohn Doe
5 min agoLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Reply