 Dear learner, today I am going to discuss iterative statements in C language and this is from the course computer programming using C. I will discuss mainly what is iterative statements, what are the different types of iterative statements and how they can be used in C programming. We have so far learned to write simple C programs using input output functions and their utilities. In programming, it is sometimes necessary to perform repeated actions or skip some statements. For these actions, certain control statements are available in C language. Now let me discuss what is iterative statements. Iterative statements are used to repeat the execution of a list of statements depending on the value of an integer expression. Iterative statements are commonly known as loops. These statements cause a section of the program to be executed repeatedly while an expression is true. When the expression becomes false, it terminates and the control passes on to the statements following the loop. In C language, there are three types of iterative statements and they are while loop, do while loop and for loop. Now let me discuss what is while loop. In case of while loop, the condition is tested before any of the statement block is executed. Here are the syntax of while loop is given. Here we can see that after executing statement X, the loop starts. First, the condition of the while loop is tested. If this condition is true, then only the statement block inside the while loop will be executed. If the condition is false, then the control will jump to statement Y. If the condition is true, only then the statement block will be executed, otherwise if the condition is false, the control will jump outside the while loop. Here the flow start of while loop is drawn. Here we can see that after executing statement X, the condition is tested. If the condition is true, then the statement block will be executed. After executing the statement block, it will increment or decrement the loop control variable as per our requirements. After incrementing or decrementing the loop control variable, it will again test the condition. If the condition is true, then only the statement block will be executed. And if the condition is false, then the control comes outside the while loop and it will execute the statement following the while loop. Here the statement Y will be executed. From the diagram, it is clear that while loop will execute as long as the condition is true. And updating of loop control variable is very necessary. Here I have taken a simple example to describe while loop. The program is to find the sum of first 10 numbers using while loop. In this program, inside the main function, I have declared two variables, I and sum. I is initialized to 1 and sum is initialized to 0. After that, clearSkin function is used to clear the screen. So that while loop is written with the condition I less than equal to 10. The while loop will continue until I become 11. Inside the while loop, there are two statements, sum equal to sum plus I, then I equal to I plus 1. Here in this side, I have written the iteration of this while loop. For the first iteration, that is when I equal to 1, 1 less than equal to 10, this is true. Then the control comes inside the while loop. And new value of sum will be 0 plus 1 equal to 1. Now that I has incremented by 1, the new value of I becomes 2. When I becomes 2, 2 less than equal to 10, this condition is true. Then again, the statements inside the while loop will be executed and the new value of sum will be 1 plus 2 equal to 3 and new value of I will be 2 plus 1 equal to 3. Then again, for I equal to 3, the condition is tested, 3 less than equal to 10. This is also true. The new value of sum will be 6 and the new value of I will be 4. This process continues until I become 11. When I become 11, 11 less than equal to 10, this condition is false and the control comes out of the while loop. As the total sum is stored in the variable sum, so sum is displayed with the help of this printed statement. The output of this program will be 55 because if we add 1 to 10, then the result will be 55. Now do while loop. Unlike while loop, the do while loop evaluates the condition after the execution of the statements in its construct. Here, the test condition is tested at the nmda loop. In the syntax, you can see that first the statement blocks will be executed after that condition is tested. If this condition is true, then loop will continue and if the condition is false, the control comes out of the loop and the statement while will be executed. The statement should in the do while loop gets executed at least once, even if the condition is false. This is the main difference between while and do while loop and this is the flow sort of the do while loop. Here I have taken one example to show the use of do while loop. The program will count the total number of positive numbers, total number of negative numbers and total numbers of 0s until the user press minus 1. When the user press minus 1, the program will terminate. Here in this program, in the main part, I have declared four variables, num, num is for storing the number, ng is used for storing the negative numbers, pos is used for storing the positive numbers and 0s will store the total number of 0s. In the do while loop, first the number is entered and it is stored in the variable num. I have written true statement printf for displaying the message entering the number and scanf for entering the number and storing the number in the variable num. After that, the number is compared with 0 with the help of this if else statement. If the number is greater than 0, that means it is a positive number, then the variable pos is incremented by 1. If the number is less than 0, that means the number is a negative number, then ng is incremented by 1. And if the number is equal to 0, then the variable 0 is incremented by 1. After that, I have written the condition number not equal to minus 1. That means the loop will continue until the user press minus 1. With the help of these three printf statements, total numbers of positive numbers, total numbers of negative numbers and total numbers of 0s will be displayed. The output of this program is written here. Suppose the user entered the numbers like this. Here the total numbers of positive numbers is 3. Total numbers of negative numbers is 2. Here minus 1 is also counted. And total numbers of 0s is 4. This is one simple example of do-while loop. I have already discussed while in do-while loop. Now I am coming to for loop. Here the syntax of for loop is written. For writing the for loop, for keyword is used. After that, within the first bracket, three components are written. First one is initialization, then test condition. Then update of loop variable. And these three components are separated by semicolon. The statements inside a for loop are written within second bracket. When a for loop is used, the loop variable is initialized only once. After that, the condition is tested. If the condition is true, then the statements inside the for loop will be executed. And the update of loop variable takes place. Then again, condition is tested. If the condition is true, then the statement block will again execute it. And the update of loop variable takes place. Like this, the for loop will continue until the test condition becomes false. When the test condition becomes false, the control comes out of the for loop and it will execute the statements written outside the for loop. This is the flow search for for loop. Here, first initialization of loop variable takes place after that condition is tested. If the condition is true, then the statements inside the for loop will be executed. After executing the statements, update of loop variable takes place. After updating the loop variable, it will again face the condition. If the condition is true, then again the statement block inside the for loop will be executed and like this the for loop will continue until the condition becomes false. When the condition becomes false, it will execute the statements outside the for loop. While writing for loops, there are some points which are required to be remembered. First one is every section of for loop is separated from each other with a semicolon. As we have already explained there are three sections and those sections must be separated by semicolon. Next one is it is possible that one of the sections may be empty. Do the semicolon still have to be there. Third one is in case all the expressions are omitted in a for loop, then there must be two semicolons in the for loop. In case of for loop, the condition is tested before the statements contained in the body are executed. So if the condition does not hold true, then the body of the loop may never get executed. Multiple conditions in the test expression may be tested by using the logical AND and logical OR operators. Multiple initializations must be separated with a comma operator. Here I have taken the same example of adding first 10 numbers 1 to 10 for explaining the for loop. In the main program, I have declared two variables i and sum. Then I have written the for loop. Inside the for loop, I have written i equal to 1, sum equal to 0. That means i is initialized to 1, sum is initialized to 0. After that condition is written i less than equal to 10. Then I have written i plus plus, that means increment of the loop variable. Inside the for loop, I have written one single statement sum equal to sum plus i. Now for the first iteration for i equal to 1, sum equal to 0, 1 less than equal to 10. That means the condition is true. As the condition is true, so the control comes inside the for loop. Inside the for loop, the new value of sum will be 0 plus 1, that means 1. After that, i is incremented by 1. The new value of i is 2. For the second iteration for i equal to 2, 2 less than equal to 10, which is also true. So the control comes inside the for loop and new value of sum will be 1 plus true. That means 3. Then again i is incremented by 1. New value of i is 3. For the third iteration, 3 less than equal to 10, which is also true. So the control comes inside the for loop. Like this, the for loop will continue until i becomes 11. 11 less than equal to 10, which is false. So the control comes outside the for loop. As the sum of all the 10 numbers are stored in the variable sum, so sum is displayed with the help of a print statement. The output of this program will be 55. The same for loop can be written in another way, which is shown in the right hand side of this slide. In the for loop, I have written only one condition i less than equal to 10. The initialization is written outside the for loop. And the updation of the loop variable is written inside the for loop. Here I have taken another example to show the use of for loop. This program can also be written with the help of while loop and do while loop. Here in this program, I have used for loop to display the multiplication table of a number. The number is entire through the keyboard and the multiplication table of that number will be displayed. In the main program, I have declared two variables and an i. Then the number is entered through the keyboard and stored in the variable. After that, I have used the for loop for i equal to 1 i less than equal to 10 i plus plus. Inside the for loop, I have written one single statement printf. Inside the printf statement, I have calculated the multiplication of the numbers with the expression and into i. As the loop will continue from 1 to 10, then it will display the multiplication table of the number from 1 to 10. Now I am coming to nested loop. C language allows its users to have nested loops. That means when we write loops inside another loop, then it is known as nested loop. In C language, loops can be nested to any desired level. Although these features will work with any loops such as while, do while and for, but it is most commonly used with the for loop. Because this is easiest to control. A for loop can be used to control the number of times that a particular set of statements will be executed. In this slide, the syntax for writing nested loop for three different kinds of loops are shown. In this slide, an example of nested for loop is shown. The program will display the following pattern. In the main program, there are two for loops. One for loop is inside another for loop. For the first loop, i is 1, i less than equal to 5, i plus plus. And for the second for loop, the loop variable is j from j equal to 1, j less than equal to i, j plus plus. Inside the second for loop, a printed statement is written to print the fellow of j. For the first iteration, for i equal to 1, 1 less than equal to 5, the condition is true. It will come inside the for loop and it will print one new line. Then the second for loop will be executed. For j equal to 1, j less than equal to i, that means 1 less than equal to 1, that is true, then it will display the fellow of j, that means 1. 1 is displayed. Then j is incremented by 1. The new value of j is 2 less than equal to i, that means i is 1 here. 2 less than equal to 1, which is false. It will come out of the inner for loop. After that, i is incremented by 1. Then new value of i is 2. For the second iteration of i, 2 less than equal to 5, which is true. Then the control comes inside the for loop and it will display one new line. After that, the inner for loop will be executed. For j equal to 1, 1 less than equal to i, that means 1 less than equal to 2, which is true. Then again it will print the value of j, that means 1 will be printed in the new line. After that j is incremented by 1. Then again condition is tested. 2 less than equal to 2, which is true. As the condition is true, so the again the fellow of j will be displayed. The fellow of j is now 2, then 2 is displayed. After that j is incremented by 1, that means new value of j is 3. As 3 less than equal to i, that means 3 less than equal to 2, which is false. So the control comes out of the inner for loop. Then again i is incremented. For the third iteration of i, that means i is now 3. 3 less than equal to 5, which is true. So again the control comes inside the for loop and it will display one new line. Then again the inner for loop will be executed for j equal to 1, j less than equal to 3. Like this the inner and the outer for loop will be executed until i becomes 5. When i becomes 5, the loop will discontinue and control comes outside the outermost for loop. In this way this pattern will be displayed and the output is shown here. In programming sometimes this is required to terminate the execution of loop suddenly. For such cases break statements are used. Now I am going to discuss break statement. A break statement terminates the execution of the loop and the control is transferred to the statement immediately following the loop. For writing the syntax of break statement only the keyword break is used followed by a semicolon. In general the break statement causes the control to pass to the statement following the inner most and closing while, do, while, for or should statement. Break statement is used to exit a loop from any point within its body bypassing its normal termination expression. In this program I have written one while loop and the condition is i less than equal to 10. Initially the value of i is initialized to 0. Inside the follow I have written one if statement. If i becomes 5 then I have written one break statement. This program will display 0 to 4. Then when i becomes 5 the program the loop will terminate. Although the condition is i less than equal to 10 that means from 0 to 10. But when a break statement is here inside the if statement then the execution of our loop will terminate suddenly and it will display only the numbers from 0 to 4. Here in this slide the break statement is used in three different loops while do, while and follow. Now I am going to discuss the continuous statement. The continuous statement is used to bypass the reminder of the current pass through a loop. When a continuous statement is encountered the remaining loop statements are skipped and the computation process directly to the next pass through the loop. For using the continuous statement we have to use the keyword continue and after that we have to give one semicolon. Here in this program I have written one for loop for displaying the numbers 1 to 10 except the number 5. Inside the for loop there is one if statement and inside the if statement there is one continuous statement. First the for loop will execute from 1 to 4 and when i becomes 5 the continuous statement will execute and it will skip the remaining part of the loop and as it skip the number 5 and then i is incremented by 1 and it becomes 6. The loop will continue until i becomes 10 and it will display the numbers 1 to 10 except the number 5.