Learn Programming Technique C to Master Skills - First Program of Loop (While Loop)

  • 22 hours ago
Transcript
00:00Hello everyone, in my last video I have explained the flowchart of display first three odd numbers.
00:12I have explained with a dry run that how we will get the first three odd numbers.
00:18Now I'm going to implement this program in C, so I'm going to open a new file.
00:29One empty file, I will save it also, then hash include what strdio.h, I will use what
00:56main, first of all I'm going to make the program in while loop and then I'm going to make the
01:04same program in for loop. I need the variables for odd number od to count the number of odd
01:14numbers that is i, I will define the value odd number that is 1 and I will initialize the value
01:22of i also that is 1. Now I will apply the loop while i less than equal to 3 as the loop is going
01:33to carry on for three times. First of all I will use the printf and what slash n odd a number is
01:44%d where it is, it is inside od, so it's going to display and od equals to od plus 2, so we are
01:56going to get the next odd number and i equals to i plus 1, so we are going to get the next
02:05incrementation in the counter variable.
02:08So this is the program friends, let me save it by using ctrl s and click on the build and see
02:18if there is any error or not, so it's showing zero error and I'm going to run it, so it displayed 1,
02:273 and 5. Now let us quickly understand how the things work, I've taken two variables,
02:33the first odd number is 1 as going to count with the counter variable i, so the value of i is also
02:391, while the value of i is less than 3, so yes here 1 is less than 3, the condition is true,
02:49so it's going to print the first odd number that is odd number is 1, then 1 plus 2 that is what 3,
02:57here 1 plus 1 that is what 2, it will come to the while loop, 2 less than 3 condition is true,
03:05so the next value which is there inside od that is what 3 that is going to print,
03:10then 3 plus 2 that is what 5, so remember the value of od it is what 5,
03:172 plus 1 what is now another value of i, it's 3, again it will go to the while loop,
03:223 equals to 3 the condition is true, what was the last value inside od it was 5,
03:28so it's going to display and 5 plus 2, 7 here it will be what 3 plus 1, 4
03:36come to the while loop, 4 less than 3 the condition is false, so the loop will terminate
03:43and we will get the results that is 1, 3 and 5. So I hope you understand, in the next video
03:50we will see some another program of loop. Bye.

Recommended