Transcript for:
Control Statements and Looping in C

in this easy add video lecture we are going to learn water iterative and loop type of control statements in C the various types of loops namely while do-while for and special or control statements namely go to break continue hey it's time to concentrate now let's start with the I'd rate of statements first repetative execution of certain statements is termed as iteration or looping a loop has simply a statement or collection of statements that is executed repeatedly until a certain condition is met after which the execution moves on to the next statement for example if you write a program to convert temperature from Fahrenheit to degree Celsius we're going to convert ten different values the user will have to run the program ten times we can use the loops through which the user will have to run the program only once and repeatedly enter ten values to get the output in one go loop's can either be a pretest loop or a post-test loop si has three loop constructs namely four while do-while are ovitch while and four are pretest loop and dowhile is a post-test loop let's take an example of products being packed on a conveyor belt if it is a pre decided that only ten products are supposed to be packed then a count on the number of products can be kept at entry level before packaging or at exit after packaging if the count is determind before packaging then it is called as pre testing or else if determined at exit level then it is termed as post testing let's take a look at pre test loops first in a pretest loop the condition is checked before the start of each iteration if the test condition is evaluated to be true then the statements associated with the pretest loops construct are executed repeatedly till the condition becomes false if the test expression evaluates to be false then the statements associated with the construct are skipped and the statements next to the loop are executed what are post s loops then in a post-test loop the code is executed once and after the completion of the loop the test condition is tested if the condition evaluates true then the loop repeats itself and if the expression is false then the loop stops and execution moves on to the next statement after the loop let's learn the basic working of the loop the processes associated with all the loops are step one is initialization of the counter variable the initialization process assigns a value to the counter variable step two is evaluating the test condition the destination is evaluated and based on its result decision is taken whether to continue execution of the loop statements or to jump outside the loop and execute the statements outside it when the result is true the statements within the loop are executed and when false the statements inside the loop are ignored and the control jumps to the statement outside the loop and the last step is incrementing or decrementing the counter variable the counter variable is updated every time the statement in the loop are executed the loop is executed to the time the counter variable reaches a certain value loop's can be programmed as event control loop or counter control loop in event control loop the occurrence of an event changes the test condition from true to false this is used when the number of iterations is not known when the number of iterations is known then the counter control loop is used seriously pay attention this is important let's learn the three types of loops starting with while loop this is the general form while is a pretest loop it uses the test condition to control the loop it also evaluates the test condition and if the result of the evaluation is true then the loop is executed once again thus the eye trations are dependent on the test conditions result the statements would be repeatedly executed till the value of the test condition becomes false to use wild statement the test expression should have a loop control variable the initialization of the loop control variable has to be done before the loop starts and updating the loop variable should be done within the body of the loop here in this example the loop is initialized by assigning D a value then the test condition is evaluated as the result of evaluation is nonzero the statements within the loop are executed and the counter variable is updated by decrementing it by one again the test expression is evaluated this process will continue till the result of evaluation is 0 that is when D is equal to 0 as soon as a result of evaluation becomes 0 the statement exactly after the loop is executed a while loop is called as indefinite loop as a number of times it will iterate is undetermined let's take the career example to understand the while loop suppose it is pre decided that the number of products to be packed is ten the sensor keeps a track on the counters value whether it has reached ten or not when the first product is placed on the conveyor the counter is set to one the sensor tests the condition by checking if counter value is less than ten as counter value is one the product is allowed to move on the conveyor for packaging the product is packed then the counter is incremented to allow the next product again the test condition is evaluated to check if counter is not equal to ten if it's true then the product is allowed to be packed this process continues till the counter reaches ten the sensor won't allow the eleventh product to move on the conveyor as the condition has become false now let's write a program illustrating the use of while loop this program finds the average of five numbers let's declare the main function the variables n which stores the numbers sum is used to maintain the summation and count is used as the counter variable counter count is initialized to 0 ask the user to enter the numbers and store each value in the variable n we are needed to use while loop here to scan five consecutive values and the number to the value stored in the sum increment the counter find the average and finally print the average seriously pay attention this is important let's move on to studying the next loop do-while loop do-while is just like a while loop except the test condition is checked at the end of the loop rather than the start this has the effect that the content of the loop are always executed at least once so this turns out to be the basic syntax of do-while loop thus the loop is executed at least once and then the test condition is evaluated if it is true then the block of statements are executed again then the result of evaluation turns out to be false all the statements are executed as soon as the result is false the statements exactly after the loop block are executed thus it will iterate at least one time do-while loop is considered as indefinite loop it is considered to be a post-test loop since the test expression is tested at the end of the loop let's take the same conveyor example to understand do-while loop as it is pre decided that the number of products to be packed is 10 the sensor keeps a track on the counters value whether it has reached 10 or not and displaced at the exit of the conveyor as the first product is placed on the conveyor the counter is set to 1 and the product is allowed to move on the conveyor for packaging when the product is packed the counter is incremented then the sensor tests the condition by checking if counter value is less than 10 as counter value is 2 the next product is allowed to be placed on the conveyor again the test condition is evaluated check if counter is not equal to 10 if it's true then the process continues till the counter reaches 10 the sensor won't allow the 11 product to move on the conveyor as the condition has become false let's write a program illustrating the use of do-while loop this program finds the average of five numbers let's declare the main function and the variables n stores the numbers sum is used to maintain the summation and count is used as the counter variable counter count is initialized to 0 ask the user to enter the numbers we are needed to use do-while loop here to scan 5 consecutive values this will make the statements in the braces to be executed at least once now store each value in the variable n then add number to the value stored in some increment the counter at the exit of this block the test condition is placed to check if numbers are not added lastly find the average and finally print the average seriously pay attention this is important let's now look at the last loop namely for loop the for loop is similar to while it's just written differently basic syntax of the loop is this in this expression one initializes the counter variable expression 2 is a conditional expression as long as this condition is true loop will iterate an expression 3 is a modifier which may be the simple increment of a variable a for loop is a pretest loop it is called as a definite loop as the programmer knows exactly how many times it will be repeated a simple example is I is initialized to 0 the test expression is tested if I is less than or equal to 5 if true then the printf statement is executed as the next instruction after the loop is executed the counter variable I is incremented control jumps to the loop test condition it should be noted that all the three parts of the previous loops are included in one statement here let's take the conveyor example to understand for loop for example again here it is pre decided that the number of products to be processed is 10 the sensor keeps a track on the counters value whether it has reached 10 or not when the first product is placed on the conveyor the counter is set to 1 the sensor tests the condition by checking if counter value is less than 10 as counter value is 1 the product is allowed to move on the conveyor for packaging then the counter is incremented to allow the next product again the test condition is evaluated to check if the counter is not equal to 10 if it's true then the product is allowed to be processed this process continues till the counter reaches 10 the sensor won't allow the eleventh product to move on the conveyor as the condition has become false let's write a program illustrating the use of the for loop this program finds the average of five numbers let's declare the main function the variables n which stores the numbers sum is used to maintain the summation and count is used as the counter variable ask the user to enter the numbers and store each value in the variable n we need to use for loop here in the for statement the count account is initialized to 0 the DES condition is placed here along with which counter is incremented inside the loops can five consecutive values and the number two the value stored in some find the average and finally print the average let's learn the special control statements namely break continue go to let's focus on break statement first what are brakes used in vehicles for when brakes are applied to the vehicle the vehicle immediately stops at that very place in situ break keyword is used to stop the current execution whenever it is used the execution from the current statement is terminated and the control is transferred to the statement outside the loop the break statement is used in loop constructs such as for while do-while and switch to terminate their execution with a nested statements the break statement terminates only the do-while for switch or while statement that immediately encloses it let's see how brick works and how it is implemented in programs we started by declaring a main function define its scope by giving it braces then we declare the variable name T for loop is used to loop infinitely till user enters 10 if the user enters 10 then the loop breaks and the control is transferred to the next statement after the loop and the last statement which is a printf statement to display a text continue is another keyword which is used for unconditional branching while crossing a signal a driver will continue to cross the signal on seeing the green light similarly in C the continue keyword is used to stop the current iteration and jump on to the next iteration the syntax is just the keyword continue followed by a semicolon continue statement does not terminate the loop but it goes on to the test condition to check the test condition in do-while and while loops and it updates the expression in the for loop let's write a program to understand the use of continued keyword the program prints all the numbers from 1 to 5 except for 3 start by declaring the main function declare a count of variable C and assign it a value 1 using the while program we will loop till C is less than or equal to 5 if checks whether C is equal to 3 if true then we place continue here in the if block continue we'll make the control jump onto the test condition if C is not equal to 3 then we will print the number C is a loop counter so on each iteration it is incremented let's learn the last and conditional branching statement go to the go-to statement is a jump statement which jumps from one point another point within a function the syntax is just the keyword goto followed by the label a label tags any statement within the program a label statement can be used anywhere in the function above or below the go-to statement the control is unconditionally transferred to location label by the label name let's write a single program to understand the use of go-to statement the code here simply asks the user to enter the character Y to continue an end to stop we declare the main function firstly ask the user to input a character we label this statement as i1 store the value in a using if we check whether the enter value is y then we print hi and transfer the control to the statement label I 1 else we print by and the program ends let's take a quick review of what we've learned here we started with iteration and repetitive statements where in repetitive execution of certain statements is termed as I creation or looping then there are two types of loops namely pretest loops and post-test loops in a pretest loop the condition is checked before beginning of each iteration in a post-test loop the code is executed once after the completion of the loop the destination is tested the looping process has three steps step 1 initialization of the counter step 2 is testing the condition step 3 is updating the counter variable next we learned that loops can be programmed as event control loops and counter control loops after which we learn the while loop which is a pretest loop as the test condition is placed at the beginning of the loop whereas do-while loop which is a post-test loop as the test condition is placed at the end of the loop this loop is executed at least once lastly the for loop which is again a pretest loop as a test condition is placed at the beginning of the loop it is very similar to the while loop in working then we moved on to learning the unconditional branching statements which are break continue and go to here break is used to terminate the loop and control moves on to the statement after the loop and continue keyword is used to stop the current iteration cycle in the loop and start from the new iteration cycle lastly go-to is used to include jumps within the program this makes the program unstructured and so it should not be used often in program