CPPM Unit-4
CPPM Unit-4
goto Statement
The goto statement is a jump statement which is sometimes also referredto as unconditional jump
statement. The goto statement can be used to jump from anywhere to anywhere within a
function.
1. Entrycontrolledloop
2. Exitcontrolledloop
In an entry control loop in C, a condition is checked before executing the body of a loop. It is
also called as a pre-checking loop.
In an exit controlled loop, a condition is checked after executing the body of a loop. It is also
called as a post-checking loop.
The control conditions must be well defined and specified otherwise the loop will execute an
infinite number of times. The loop that does not stop executing and processes the statements
number of times is called as an infinite loop. An infinite loop is also called as an “Endless loop.”
3 For Loop In a for loop, the initial value is performed only once, then
the condition tests and compares the counter to a fixed value
after each iteration, stopping the for loop when false is
returned.
While Loop in C
A while loop is the most straight forward looping structure. While loop syntax in C programming
language is as follows:
while(condition)
{
statements;
}
After exiting the loop, the control goes to the statements which are immediately after the loop.
The body of a loop can contain more than one statement. If it contains only one statement, then
the curly braces are not compulsory. It is a good practice though to use the curly braces even we
have a single statement in the body.
#include<stdio.h>
#include<conio.h>
void main()
{
int num=1; //initializing the variable while(num<=10) //while loop
while condition
{
printf("%d\n",num);
num++; //incrementing operation
}
return 0;
}
Do-While loop in C
A do…while loop in C is similar to the while loop except that the condition is always executed
after the body of a loop. It is also called an exit-controlled loop.
As we saw in a while loop, the body is executed if and only if the condition is true. In somecases,
we have to execute a body of the loop at least once even if the condition is false. This type of
operation can be achieved by using a do-while loop.
In the do-while loop, the body of a loop is always executed at least once. After the body is
executed, then it checks the condition. If the condition is
Similar to the while loop, once the control goes out of the loop the statements which are
immediately after the loop is executed.
The critical difference between the while and do-while loop is that in while loop the while is
written at the beginning. In do-while loop, the while condition is written at the end and
terminates with a semi-colon (;)
The following loop program in C illustrates the working of a do-while loop:
#include<stdio.h>
#include<conio.h>
void main()
{
int num=1; //initializing the variable do
//do-while loop
{
printf("%d\n",2*num);
num++; //incrementing operation
}while(num<=10);
return 0;
}
For loop in C
A for loop is a more efficient loop structure in „C‟ programming. The general structure of for loop
syntax in C is as follows:
The condition is a Boolean expression that tests and compares the counter to a fixed value after
each iteration, stopping the for loop when false is returned.
Nested Loop in C
C supports nesting of loops in C. Nesting of loops is the feature in C that allows the looping of
statements inside another loop. Let's observe an example of nesting loops in C.
Any number of loops can be defined inside another loop, i.e., there is no restriction for defining
any number of loops. The nesting level can be defined at n times. You can define any type of
loop inside another loop; for example, you can define 'while' loop inside a 'for' loop.
for(initialization;condition;inc/dec)
{
for(initialization;condition; inc/dec)
{
void main()
{
int rows = 5;
while(condition)
{
while(condition)
{
//inner loop statements.
}
//outer loop statements.
}
Example:
// C program to print right half pyramid pattern of star
#include <stdio.h>
void main()
{
int rows = 5;
// first loop for printing rows
i=0;
CONTINUE
BREAK
Break statement
In all the C loops we have a way to break out of a loop at any point in time, immediately, regardless of the
conditions set for the loop.
In simple words, The break statement is a loop control statement which is used to terminate
the loop immediately.
Basically break statements are used in the situations when we are not sure about the actual number of
iterations for the loop or we want to terminate the loop based on some condition.
Syntax :
break;
In the above example, a for loop is used to print the value of i in each iteration. In which
a if statement is used with break statement in it.
The condition of if statment is i == 5 i.e. when i is equal to 5 and break statement is executed to
terminate the loop. Hence, the output doesn't include values greater than or equal to 5.
In C programming, break is also used with the switch statement. This will be discussed in the next
tutorial.
continue
This is one of the easiest and the most basic keywords in C/C++, which gives programmers control
over loops.
The continue exactly as the name suggests. Since we use this in loops, it will skip over the remaining
body of the current loop, and continue to the next iteration.
We can use this inside any loops like for , while , or do-while loops.
In Simple words,
1. The continue statement is used to skip the remaining portion of a for/while loop.
2. we continue onto the next iteration, and move to the loop condition check.
3. This is quite useful for programmers to be flexible with their approach in control loops.
Syntax :
continue;
1 2 3 5 6 7 8 9 10
In the above program, we have used the the for loop to print the value of i in each iteration. Here, notice the
code,
if (i == 4) {
continue;
}
When i is equal to 4, the continue statement skips the current iteration and starts the next
iteration
Then, i becomes 5, and the condition is evaluated again.
Hence, 5 and 6 are printed in the next two iterations.
Loop prints the value of i until it became 11 and condition becomes false. Then, the loop
terminates.
Break vs Continue
Break Continue
Break statement stops the entire Continue statement only stops the
process of the loop. current iteration of the loop.
Break also terminates the remaining iterations. Continue doesn‟t terminate the next
iterations; it resumes with the
successive iterations.
Break statement can be used with switch Continue statement can be used with loops but not
statements and with loops. switch statements.
In the break statement, the control exits from the In the continue statement, the control remains within
loop. the loop.
It is used to stop the execution of the loop at a It is used to skip a particular iteration of the loop.
specific condition.