 Hello and welcome to this video lecture. In this video lecture, I shall be demonstrating the use of loops and if condition to solve a particular problem. And this entire video lectures are being done with the objectives of problem solving. So through these video lectures, we are helping students to solve problems and thereby preparing them for competitive programming. So today we shall use loops and try to solve the problem that I am going to discuss in this particular video lecture. So after the end of this video lecture, you will be able to solve a problem by writing a C program and in the C program use loops and if condition. So these are the prerequisites that you would require. If you are knowing these concepts that have been shared here, you can go ahead with the problem. If you still want to have a brief introduction and explanation regarding the topics that have been discussed, I encourage you to go through these links so that you will be well versed with the concepts and will be able to solve the problem more efficiently. So what exactly is the problem statement that we are going to discuss in today's video lecture? Consider there are X number of friends and all these friends are playing a game in which a dice is involved. Now we know that a dice is a cube and on every surface of the dice, there is a particular number starting from 1 through 6. So these X number of friends rolled a dice in turn one after the other. Now if the number on the dice is 6 as we all know and have played it sometime or the other, then the dice will be rolled again by the same person until a number other than 6 is obtained. So if the 6 number occurs, the person who is rolling the dice, he gets another chance. Consider that you know the sequence of numbers while the dice shows when rolled each time if you are noting down the sequence, the problem is to find what is the total number of friends playing the dice. So depending on the sequence of the roll of dice, we have to find out the number of friends playing the dice if the sequence is invalid, we return minus 1. So say for example, the sequence is 3, 6, 2, 1, 2, 3, 3, 6, 2, 1, 2, 3. So from this sequence we come to understand that the first friend when rolled the dice, the number 3 was on the top surface, then the second friend rolled the dice for 3 times. The first time he or she rolled it was a 6, the second time again it was a 6 and the third time the friend got 2 on the surface, so that makes it 2. The third friend rolled and it was 1, the fourth friend rolled and it was a 2 and the last friend that is the fifth friend rolled and it was 3. So we come to know from this particular sequence that the number of friends is 5, so our program. So here is the algorithm to solve the problem that we have been discussing so far. First initialize your program and then you need to take an input string from the user and store it in some variable, in this instance we call our input string as str, so the sequence of the dice played by the friends will be stored in str and we also need to store the length of the string that we are using, so the sequence that is the length of the sequence will be stored in say for example l and then the third step will be to scan the string from left to right using a loop, so we can use all the loops that are available in C language, it can be a for loop, it can be a do while loop and then we will scan the string, the sequences in the string from left to right and in the string, in the string we shall look for all the sequence, all the occurrences from 1 through 5, that is 1 or 2 or 3 or 4 or 5, so if any of these occur then we increment count by 1, then we will increment our count counter by 1 and if any occurrence is a 6, that means if 6 occurs anywhere except in the last position what we will do is we will continue the loop, what we will do is we will use the continue keyword and continue with the iteration so that the iteration jumps from the current execution to the next execution, if the last occurrence in the sequence is equal to 6 or if the count of the sequence is equal to 0 then we return 1 stating that the last occurrence cannot be a 6 or the length of the sequence cannot be 0, if this is the case either of 1 is the case then we return a minus 1 and if we do not return a minus 1 that means we are having a valid count and we return the count, now we shall look at how to execute this, so starting with our program I have declared the header files that I am going to require here, then I have declared integer l to store the length, integer i to run the loop and count 0, count 0 has been initialized, count is initialized to 0 so that we can increment it later, now I take the input as a string, as a string and store it in a character array a, then I calculate the length of the string by using the strln function and the length of the string is stored in l, now starting from 0 to l that is 0 to l minus 1, if the current occurrence is 6 so the user, the friend will get another chance, so we continue with the loop, we do not count it as a count, we continue it, we continue counting, we continue the loop excuse me and do not count the occurrence and if the current occurrence is equal to 1 or greater than 1 and if it is less than or equal to 5 then we increment the count, now notice that this is not 1 but it is the character 1 and this is not 5 but it is the character 5, so all the ASCII values of 1, 2, 3, 4, 5 are going to be compared here because for certain reasons that we are comparing the characters and character a or the sequence that we have taken as an input is a character input so the ASCII values are going to be compared, so anything from 1 through 5 occurs the count will be incremented, else what I have done is else say for example during the input of the program we know that the dice runs from 1 through 6 but by mistake any other number is entered a message as invalid input will be returned and the function will exit, so I have returned it you can just return minus 1 here and end the function, after this loop runs from 0 to L minus 1 if the count is 0 that means there is no sequence or if the last occurrence of the last digit in the sequence if it is 6 then it will print minus 1, now 6 cannot be the last occurrence in the sequence because after 6 after every 6 there should be another turn, so 6 cannot be the last number the last sequence the last occurrence in the sequence if it is then we return minus 1 if it is not the case that means that we have had a successful sequence and we return count, now I will quickly execute this code and see the output, I am going to enter the sequence now say for example I enter 1, 2, 3, 4, 5, 6 and hit enter this is going to be an invalid sequence because the last occurrence is a 6 which is invalid in our case again I run the same code 1, 2, 3, this time I take 6 not as the last but somewhere in between 6, 5, 4 and it returns 5 as the number of friends playing, so we will count that the first friend played and got a 1, the second one got a 2, the third one got a 3, the fourth one in the first chance got a 6, in the second chance got a 5, so this is 4 and the last friend that is the fifth friend got a 4, so number of friends that are playing dice is 5, I have shared the link for the downloadable code of this program you can download it, it is available in the description section, now pause the video for a moment and implement the same program using a do while loop pause the video and write the code, so the question was to implement the same program using a do while loop, so if you have used a do while loop your program might be similar to the one that I am going to show you now, so I have made the required changes in my code and instead of a for loop I am going to use a do while loop for using the do while loop I will require to initialize my i variable to 0 before the loop, so I initialize i to 0 and I start with the do while loop, so inside the do while loop I check for each and every number in the sequence and after the checking is done and incrementing of count is done I increment the i variable by plus 1, I increment it by 1 and I continue with this loop till i is less than l, so these are the required number of changes that you need to do if you are using a do while loop instead of a for loop, now we will execute the program and see for the output, now I take this simple sequence of 1, 2, 3, 6, 5, 4, the last time also it gave us that output that five friends are playing dice and right now also we can see that the programming is a program is returning 5 which is the correct in this case, so this is how you use a do while loop instead of for loop to implement the same program, so these are the references that I have referred to you can refer to these references this is the reference to the problem and few notes regarding loops, thank you