Transcript for:
Understanding Break and Continue in C

hey guys this is me padma from pragyamis and welcome back to this series on c programming in this video we'll learn about break and continue statement to alter the normal slow of loops in c programming so let's get started the break statement immediately terminates the loop when it is encountered before we move forward let's see the working of loop first here i have used for loop the loop starts from i is equals to 1 to i is equals to 5 and it prints the value of i so let me run this code as you can see we get the output from 1 to 5. now let me use break statement inside the loop after the print statement and i'll run this code this time i only get one as output let's see what happened here during the first iteration the value of i is equals to 1 so when the loop runs the value 1 is printed now after the print statement the break statement is encountered and this break statement immediately terminates the loop for more clarity let me add another print statement after this break statement so printf and i'll enter the message after the break so let's run this as you can see we get the same output one this is because when the break statement is encountered the loop exit and no other code of the for loop is executed we generally use the break statement with the decision making statement like if else statement this is why we can break a loop under a certain condition let's see an example i'll use the same code from earlier here i'll remove this break statement and the print statement after that now suppose i want to end the loop when the value of i is equals to 3 so i'll add if statement so if statement with the condition i is equals to 3 and inside this if statement i'll use break now i'll run this code as you can see we get the output 1 and 2. here during the first iteration the value of i is equals to 1 so this if statement is false so we'll go to this print statement and 1 is printed on the screen now the value of i is increased to 2 and this statement is again false because the condition does not meet and again this print statement is executed and 2 is printed on the screen when the value of i is equals to 3 the if condition is true and this break statement is executed and the loop terminates hence we get the output one and two by the way if you are watching this there is a good chance you want to improve your skills in c programming lucky for you we have a mobile app that provides a well-structured c programming course with certification at the end and you can use the app alongside the video to practice on the built-in compiler our course is free so download now by scanning this qr code or click the link in the video description let's now use the brick statement with while loop i'll create a program that will ask input values from the user if the user inputs a positive value it will be printed however if the user input a negative value the while loop is terminated i'll use the same code from earlier i'll remove this for loop i'll create a while loop that is always true and i'll use one as a condition of while loop because we know that one represents true in c programming so this while loop is always true now inside while loop i'll declare a variable number so int number and i'll ask the input from the user and i'll ask the user to enter a number and i'll store it using scanf function since i want to break the loop only if the input value is negative i'll add if statement with the condition number is less than 0 then we'll break the loop and if it is not negative we'll print the value of the variable so i'll use printf statement so percent d and name of the variable that is number now let me run this code and i'll enter 5 you can see 5 is printed and i'll enter 9 and it is also printed so now let's enter some negative value you can see the loop terminated here you can see as long as we entered positive value the while loop executed repeatedly however when the input is negative then the if statement becomes true and the break statement is executed and it terminates the loop now let's move to the continue statement unlike break the continue statement skips the current iteration of the loop and starts the loop with the next iteration let's see an example in this code we have used break statement to terminate the loop when the value of i is equals to 3 now let me replace this break statement with continue statement so continue and i'll run this code now if you look into the output you can see the value 3 is not printed let's see why this happens we know that the loop runs for 5 times from i is equals to 1 to i is equals to 5 and during the first and second iteration the value of i r 1 and 2 respectively so if condition is false for both iteration hence the loop runs normally and 1 and 2 are printed during the third iteration the value of i is equals to 3 this time if condition is true and continuous statement is executed now the continue statement skips the current iteration of the loop and start the next iteration so the program jumps to this update expression and increase the value of i to 4 and again for the value 4 and 5 the if condition is false and loop runs normally and 4 and 5 is printed on the screen in this way for value 3 the continuous statement skips the print statement okay guys we need your support to keep this type of content free for all users youtube really likes engagement on the video so leave a comment below press that like button and hit subscribe if you haven't already let's keep the engagement score high up so that more people can discover and enjoy these courses we now know the working of break and continue let's use them together in a single loop i'll create a program that takes the input values from the user however i want the user to input positive number only so if the user enters 0 or negative number i'll use the break statement to terminate the loop also to make the program more interesting i'll only print the even numbers so if the user inputs an odd number i'll use the continue statement to skip the loop so that odd numbers are not printed let's start now here the break statement is already implemented that ends the loop if the number is a negative number since we want to exit the loop if the number is either negative or 0 i'll change this less than operator to less than or equal to operator now i want to run the continue statement if the number is odd so i'll add if statement with the condition number modulo 2 is not equal to 0. so inside if we'll add continuous statement here the modulo operator returns a remainder so for a number to be odd it shouldn't have a remainder 0 when it is divided by 2. so let's run this code i'll enter 4 you can see 4 is printed because it is an even number now i'll add 7 and i'll enter i'll enter even number 32 and you can see this 32 is also printed now let's enter a negative value so minus 3 you can see the loop is terminated so as you can see when the input value is positive and odd number this condition becomes true so the continue statement skips the print statement for the odd input value however when the input values are positive even numbers both the condition becomes false so it is printed when it is an even number when the input value is negative this condition number less than or equal to 0 becomes true so the break statement terminates the loop now to revise what we have learned it is a programming tax for you can you write a program that takes an input from the user and print it if the value is a negative odd number however if the input value is positive in the loop with the message positive value and if the input value is negative even skip the value with the message negative even so our output will look like this go ahead and try the problem and share your code in the comment below you can find the answers to this question in the github repository and if you want to revise the concept all the program used in this video are also present in there now that we have reached the end of this video it's time for programming squeeze which of the following keyword is used to skip the current iteration of all loop comment your answer below see you in the next video happy programming [Music]