×
 Introduction
Introduction to Newbie Programmer Series Introduction to Computer The Computer Programming Language Setting up the Computer for Programming
 Lets Get Started !
Beginning C Programming Variables a Handbag ! Basic Variable types in C Basic Formatted Output Basic Formatted Input
 Loops
Looping Repeating Concept Different Kinds of Loops Advanced concepts and trick with Loops
 Operators
Operators, An Introduction Arithmetic Operators Relational Operators Logical Operators Bitwise Operators Miscellaneous Operators and Operators Precedence
 Controlling the Flow
Flow Charts Conditional Statements Using Goto and Label Break and Continue the loops Switch the Cases Some Practical Applications of Control Flow
 The program structure
The program Structure Functions More Concepts of Functions Scope of the Variables across functions How the C language itself Works ? Scope of the Variables across files Static Variables Auto and Register Variables #define PreProcessor Some more PreProcessors
 Arrays and Structures
Introduction to Arrays Arrays, some more Concepts Arrays, Cool Examples Structures Introduction Structure, Some more Concepts Structure, Some Examples
 File Management
Basics of File management in C Steps Involved with File Handling in C
 Miscellaneous
Inside Logic Gates – The Electronic Logic Binary Number System Mathematical Reasoning
Newbie Programmer
full book coming soon
by Shubham Ramdeo

break and continue the loops, no need for goto !


 

break and continue

Welcome back to the Newbie Programmer Series. We are learning about controlling the flow. In the last part (click here), we have learned about the goto statement that how its useful for jumping in between code for error handling but also that using goto is a bad practice. In this part, we will learn about break and continue statements. These are the statements that can help you to manage the flow of your program without using goto. In some way, break and continue are the alternatives of goto. But they have many other advantages to and we will discuss them here. So if you are new to this series, please go to index (click here) and read out all the previous parts so that you can easily understand what is going down below. Break and continue statements are made to use with loops only. Lets discuss the coding stuffs.


Break

Using break is ultra easy

. . . . . . .
break;
. . . . . . .

And ultra sonic easy to understand !!! In our code, whenever the break statement is encountered, the program comes out of the current loop. That's simple. The loop will be stopped immediately when the break statement is encountered.

For example

#include<stdio.h>
main()
{
int x;
for(x = 1; x < 10; x++)
     {
     printf("hello \n");
     if(x==5)
          {
          break;
          }
     }

}
OUTPUT :
hello
hello
hello 
hello
hello

But why so ? Don't you see that we have made the for loops for 10 times ? And instead of 10 'hello" we are getting 5 only ! That is so because every time the loop runs, it is checked if x becomes 5. When x becomes 5,  the break statement inside the if block is encountered and the loop stops.

For such quitting, we can make a label below the loop and use the goto statement inside the loop to quit the loop. But break is better alternative.

Continue

Similar to use :
. . . . . . .
continue;
. . . . . . .
Just as break stops the loop, continue statement is used to come back to the loop. Break statement makes our program to come out of the loop and continue statement makes the program to get into the loop again and all the other statements are skipped.
For example :
#include<stdio.h>
main()
{
int x, num;
printf("I like 5 and greater ! \n");
for(x = 1; x < 10; x++)
     {
     scanf("%d", &num);
     if(num < 5)
          {
          continue;
          }
     printf("Thanks \n");
     }
printf("bye !");
}

OUTPUT

I like 5 and greater !
3
5
Thanks
3
2
1
8
Thanks
999
Thanks
0
3
4
Bye !

Its simple. The loop will go for 10 times. In these 10 loops, it will ask you for a number. If the number is less than 5 then due to the continue statement, the loop will start again and all the statements inside the loop ( like here for saying thanks ) are skipped. It will repeat 10 times in total. In these 10 times, for all the numbers you have entered which are greater than or equal to 5, you will get a thanks otherwise nothing.

We can use a label above the loop and then use the goto statement inside the loop to start it again. But continue is a better alternative for it.

So that's it for today. In the next post, we are doing another interesting stuff. So stay connected.

Please share this post as much as you can so that we can get connected to more geeks who wants to learn programming but have no clue. Thanks for reading :) (c)Shubham Ramdeo, All rights reserved.

Any doubts ? Meet me at twitter @ramdeoshubham

 

break and continue

Please share this as much as you can so that we can get connected with more and more people who want to learn programming.

© Shubham Ramdeo

Site Map
with
© Shubham Ramdeo