Category
š
LearningTranscript
00:00Now the same program which I have explained with the use of the while loop to display
00:09the first three odd numbers, I am going to make the same program by use of for loop.
00:15How to use the for loop?
00:18I am going to explain with this simple program.
00:22First of all I will save this new file, something like this I have given the name, here it should
00:39show what C and C++ files, then save and I will start making the program, that is include
00:51then I will use main, inside it I will use first OD then I, I will give the data type
01:04also with it integer then OD equals to 1, I will use the for loop for I equals to 1,
01:14I less than equals to 3 and then what I++ and here I will use what printf and inside
01:23it I will write down slash n odd number is that is what percent d, where it is inside
01:36the OD, at the last of the statement printf statement what semicolon then what OD equals
01:45to OD plus 2 to get the next odd number, here I will write down what return 0 like this.
01:57I will save it and let us first see if there is any error, so there is not any error,
02:06let us run and see it's going to collect correct result, yes 1, 3 and 5.
02:12How the things work let us understand, inside the for loop I have initialized the value of I that
02:19is 1, it will come to the decision part, 1 less than 3 the condition is true, so it's going to
02:25display the first odd number that is what 1, then 1 plus 2 that is 3, so 3 is going to store inside
02:31OD and the value of I is going to increment, so either you write down I equal to I plus 1
02:39or you write down I++, both the things are same, it's only going to increment by 1,
02:46so the value of I is going to increment from 1 to what 2, so now the value of I is what 2,
02:53it will come to the decision part, 2 less than 3 the condition is true, so it's going to display
02:58what 3 as the last value inside OD it was 3, then 3 plus 2, 5 again the loop will carry on,
03:07the value of I is going to increment from 2 to what 3, it will come to the decision part,
03:133 equal to 3, here is the condition is true, so next odd number it was 5, so it's going to display
03:195 and 5 plus 2, 7 again it will come here I++, now the value of I is what 4, 4 less than 3,
03:30this loop will terminate and we will get what 1, 3 and 5, so we have seen the result,
03:39we have run it and we have seen that it has displayed 1, 3 and 5.
03:44Okay, the next video I will explain something else, thanks.