 Hello friends, I am Sanjay Gupta. Welcome back on Sanjay Gupta Tech School. तो आप देख हैं C-Language video series. तो इस video series का ये जो lecture है, ये है lecture number 10. और इस lecture में जो topic हम discuss करनेवाले है, उसकर आम है, loops. तो पिच्ले lecture में आपने देखा था की हमने switch statement discuss की, और मैंने उसका एक example आपको बताया था. प्रज्ट्ट्ट्ट्ट्ट में हम loop के बारे में discuss करेंवें, तो पहले loop के types जानेंगे कों कों से होते हैं, और फिर इस video में एक loop के बारे मैं अपको बतावागा जोगी for loop है, और से related कुछ programs implement करेंवें. और जिन students के पास इस video series का link नहीं है, तो वो इस video के description में जाएं, और वहापको play list का link में लेगा जिस में, जिनने भी videos इस video series के part हैं, वो आपको एक साथ एकी sequence में arranged में जाएंगे. तो आब हम start करते है, loop discuss करना. तो basically loop होता क्या पहले वो देखते है, तो इस में देखो पहला स्थेद में जो लिखावा है, loops are used to repeat one or more than one set of instructions and time. तो आब एक छोटा से एकजामपल लिट हैं, जैसे c-program के अंदर हमें लिखना है, अपना नाम, screen पर. तो आब क्या यूस करते हैं, printf. प्रिंटेफ एक बार लिख होगे, अपना अब परCrave अपना नाम प्रिंट अब अगर असी प्रिंटव, कोपि कर के बह�特別 repeats is that even if we start printing too much of those printed, अपना अपना अपना अपना अपना अपना अपना अपना अपना अपना आं ॥, वह Anna Prave to print ten times of you and print your name so an तो आप आप नद़ ता की आप को लगेगा बड जैसी बोला थाउजन, प्फ्ती थाउजन, बन लग, तो उस केस में क्या होगा, की आप कोपी पेस्ट करने में भी बहुत बडी प्रव्रुम पेस्ट करेगें तो इस प्रव्रुम का जो सोलिशन है वो है लुप कि जब हमें एकी ताइप के स्टेट में को एक से जाड़ा बार एकजिकूट करने है तो उस को बार बर क्यो लिकें? कोई एसी चीज हो नहीं चीज हो लिए कि जो उस को अप्दमेड़क लिए रेपीट कर दें तो आपने देखा होगा चोटी ख्लासस के अंडर हम को तीचर से लाईन लिए कर देते थे और उस को बोलते थे की पुरे पेच पर लिए कर लाना तो उस ताइं तो लुप ठानी ना प्रोग्रामिग हम जानते थे तो हम उस को बार बार बार लिए के थे बड़ अब लूप पडने के बार आपको असा लिए काश उस ताइं मुझे लूप आता तो मैं उस स्टेटमें को एक बार लूप की अंडर लिखना और पीटर को पुड़ता याब ये लूप अपने हबस को रेपीट कर गे और उपनी भार प्रेइंट कर देखा सोग आब जीस ब आब आब ची भरवेंग के थू कर सतें बआसेको लूप लिए न्प़्िमट करने के लिए टीन खबनट सक्झाई के लिएट हो साँ य लो आपके सामने लिखने लिखने लेक्रे लेएट फोसथ is initialisation termination condition and third one is increment or decrement so initialization means beginning of the loop, termination condition is ending of the loop and increment decrement which will take you from the beginning and termination condition so now how it is implemented, how it is written in the loop, we will discuss it later so now I have told you that when we have to execute a common set of instructions then we will use the loop and to use the loop these three parts will be required so now you are thinking that which loop is available to repeat the statements so you have three types of loop, for, while and do while and in this lecture we will only focus on one loop, for loop so in the next lecture 11, 12, 13 you will learn how to work on while loop and do while loop so we will tell you about the related program that we can make so this is the syntax of the for loop so the syntax is very important in programming because the syntax tells you that any feature can be used in any way because the way to write it is already decided so this is the syntax of the for loop for a keyword which has to be written in the small letter so I have told you three parts, the first part is initialization after that we have used semi-colon, then termination condition again semi-colon and then increment or decrement so these three have to be written in the same sequence if you are using for loop and if you want to repeat one or more statements then they will come in for loop so if there is one statement then these curly bases are optional and if there is one or more statements then you have to use them so as you can see in the example below that the printf is one statement so I have not used the curly bases but if there is one or more statements then I have to use the curly bases so first I will explain it and then I will tell you about this example so first let's see the flow so as I told you in the slide if we will read the for loop so first I will tell you about the syntax and looping process through printing name so that is what we are seeing so now let's discuss the syntax of for loop in more detail when this loop is executed then the initialization will be the first step after that we will go to increment, sorry after that we will go to condition so condition check will be if condition is true if condition is true then you will execute this statement so this is our third step after executing the statement we will come to increment and decrement this is our fourth step after increment and decrement you will not go to initialization again instead you will come to termination condition so initialization will be done only once because if you do not initialize again then your loop will not terminate so I have told you the process of this syntax so let's apply this example so first I will initialize from i1 then condition check will be i less than equals to 5 condition is true so condition is also true 1 is smaller than 5 condition is true it means this printf will be executed so what will printf do? my name will be displayed on the screen then we will go to i++ so now this increment decrement operator which I have explained in the operator chapter so those who have not seen it then I have already told you the playlist link that this video description is available on the first number so as soon as you click that link then all the videos will be available in the series so if you see operator video then you will understand increment decrement so basically what will i++ do? it will increase its value from 1 so what is the current value of i? i++ will be i2 so when i2 will happen then you yourself think that should we go back to initialization or should we do i1? no because we will do 1 then it will be 2 and then it will be 1 again so all this will be wrong so i++ means after increment decrement when the value of i2 will be 2 then it will go back to condition checking so now this is the loop so condition check will be then name print will be after name print there will be increment decrement so this is a loop cycle so when i2 was so condition is true printf will print its name then i++ will be 3 then condition check will be condition is still true then i4 will increase 4 will be smaller than 5 then i++ will be 5 5 is equal to 5 5th time name print then i6 will be then condition falls so how many times loop is repeated? so printf automatically 5 times my name is printed on screen now you think to implement it practically so that we understand it better so let's go to our DAVC++ compiler or any compiler you are using you can open it and implement it so now I am writing a comment process of follow so what we are understanding that follow basically implement so now in this I will tell you many more things so watch it carefully so one variable declared i then I wrote follow i equals to 1 i less than equals to 5 i++ and I wrote printf backslash and my name you must have understood because we have used printf many times so if we remove backslash so all the time the name will be printed all will be printed in the same line we want the name to be printed in different lines so before printf we will use backslash so now I will save it so I have given the name test.c let's compile and run it so you will see on the screen my name was printed 5 times okay now see how easy process is you will say print your name 15 times so I have traded it 15 times and when I will compile and run it you can count my name has been printed 15 times so how easy is it so now if you are a teacher you have written one line and if you want to write so many times then simply write a loop so this loop will print it but you are already in big classes so you won't be able to do such things but in future when you will grow up your children will be there and they will give you assignments so you can write a loop there because till then programming will reach on advanced level so these days children of 5th to 6th class have started programming so all these things are important programming automatically we have to implement it once and it will automatically execute so now you have to run it 100 times 100 times 1000 times so this termination condition is very important the whole loop will depend on it till it is true the loop will continue so let's turn it now let's talk about curly brace so I have put this now I have only one statement so I have told you that it is optional if we will use it then there is no problem but if there is more than one statement then I will write one more line below it now there are two statements so first let's use curly braces and see what happens see my name is printed and then C programming video series both things are printed after 10 times now if we remove the curly brace then what will happen now if we remove the curly brace then what will happen along with the for loop the first line will be repeated the second line is not a part of for loop because we have not put the curly braces so my name will be printed 10 times and then C programming video series will be printed once see my name was printed 10 times and C programming video series was printed once so this way you will understand the use of curly braces if there is more than one statement then it is beneficial to put the curly braces if there is one then you avoid it so I hope now how does the for loop work or if there is more than one statement then how can we execute it more than one then how can we do that now you are thinking of telling some real life example some mathematical calculation which we will solve through loop so we will discuss that so as you can see there are two types of titles here first we have to print natural numbers then even odd numbers then table of a number then we will print these series so first we have to print natural number from 1 to n now we will have to input from the user so now we will pass two variables we will change the title in the comment so the title will be print natural numbers upto n so the value of n we have to run the loop and we have to print the natural numbers so first we have to print f enter value of n we have to scan it we have to scan it through scanf so in scanf you are an expert so one number is so the value will be in n now we have to decide where the beginning of loop where the ending of loop will be increment decrement so where the beginning of loop because natural number starts from 1 condition will be i less than equals to n so in the last example there was 10, 50, 5000, 1000 so many times we have to run the loop but now the loop will depend on the input of the user so the value of n will be printed by natural number so in the condition i less than equals to n and then i plus plus and now we have to print natural number so i cannot print the name but i will use the variable i so the first time value of i will be 1 so i plus plus will be 2 so 2 will be printed so in the loop the variable you used for the condition checking you can print it so this program is completed so now let's run the compiler from which you can see as i have written 10 so you can see 10 natural numbers 1,2,3,4,5 up to 10 if we run again and you enter any other value let's suppose 20 so 20 natural numbers will be 10 so in this way we input from the user and control the loop the condition of the loop how many times the loop should run and the natural numbers automatically so it is a very interesting topic if you want to do any work automatically then you don't need to write again and again you can simply implement the loop okay now let's move on now we will talk about print even or odd numbers so now we have printed the natural number 1,2,3,4,5 now the user wants to see all the odd numbers even numbers or even numbers so first let's print even numbers print odd numbers up to n now in this program we will change a little bit so odd numbers will be printed so now we can make this program in two ways first we do i so now i1 starts initialization so the condition will check if it is true then i is printed then when we will go to this increment part then it is not necessary to always write i++ or i- we have written i equals to i plus 2 so what is the current value of i 1 plus 2 is 3 so what will be the new value of i so 3 will be the part of the condition checking if it is true then 3 will be printed then next time 3 plus 2 will be 5 so 5 will be the condition check so all the odd numbers will be printed so this is the first method and as i have written 20 so you can see all the odd numbers are being printed 1,3,5,7,9,11,13,15,17 or 19 okay now you can tell me some other method through which we can print odd numbers so for that here i will write i plus now i am writing the base okay and i am writing if i modulus 2 double equals to sorry not equals to 0 and here i am writing print f i so watch this carefully now loop i plus plus is being controlled i1,2,3,4 and upto and upto we have written if now you will understand for example if else we have seen in previous videos how to use it now we will use it in loop so you can use if else in for loop so value of i is 1 modulus 2 not equals to 0 so if i 1,2,3,4,5 whatever happens if remainder is not equal to 0 it means the odd number will be there if even number is 2 completely divisible so whenever this condition is true then the value of i will be printed otherwise not so save it compile and run so you can see 1,3,5,7,9 print 2 means odd number so this was the second method but the last example and the difference in this example loop n time is running loop n by 2 time is running because we skipped loop by 2,2 so you have to keep in mind the less rotation you execute the more efficient your program is so if we talk about odd number printing then the previous example for loop in which i was written equals to i plus 2 it is more efficient as compared to this but output will come through both so whatever you write in the exam it will be correct now let's talk about even number so let's modify this first if it is divisible from i2 then it means even number so save it and run it if it is 20 then you can see 2,4,6,8,10,12,14,16,18,20 all the even numbers are printed and if we remove this line and write here i equals to i plus 2 and start from loop 2 you can start from 0 so we have written 20 so you can see all the even numbers are printed so in this way we have printed odd numbers and even numbers and to print both of them I have told you two different methods which you can use so one more thing we can do increment decrement it is not necessary to write i plus you can do increment decrement and second we have seen we can use if else in for loop okay now let's move ahead so next example is print table of an number so this also will remind you of old classes when you were younger or we should talk because when we were in first and second class we were taught to write table 5,6,10 so we had to write table of every digit separately now programming AC power will provide that you will get number input and its table will automatically display on your screen so first of all let's print title print table of a number so this is comment so you are only visible there will be no park of execution so enter value of n enter the number 2stable you want to print so the message can be anything it will depend on you okay now let's remove these things now we have to make for loop so first of all think how many times for loop it will repeat if you have to print table of any number then you will say there are 10 digits in any number 2nd table 10 digits 5th table 10 digits so there are 10 digits in every number table so this is clear that loop will go 10 times okay so first let's write that loop will go 10 times okay loop is going 10 times we have to print our table so now for example look below if n is 5 if n is 5 then what is table of 5 so whenever the number we have drew we ought to multiply these numbers 1 2 3 4 5 how we get 1 2 3 4 5 i won't get it because i will have 1 then i will have 3 so if i write a printf here n into i I have written this printf so what is printf for scanners this will get this number if you have a single number of voE,it is going to take a loop from the user. for example number is 5 now loop is going to run,it is going to have an i value so what will happen here? 5-1 when i++ comes,It will be i in this loop,we are not changing the n anywhere we are increasing the i value to 1 2 equals to 10, अगर इस वोर्वेड में परिंट कराओ तो मज़े है ते के आप बगगग इस वोर्वेड में परिंट कर के बताओ थो जाने भई दिक्कत नहीं है, इस वोर्वेड को बनाने के लिए आप को या करना है तो 5 है, वो तो n से मिलेगा, 1, 2, 3, 4, i से मिलेगा ल्च़ हो कों आँ आप स़ीवाएटीग worried लिक य 절대 ज़वार आप क्योनerk नो मुर्ती का आ�绍़塔 n वrésad ab अप ज़ोग effect of particular अपिक यए ही just अप आप विसक्र येभी पन लिन्गी प करागेता, आंटर्त हगे यब आभा लिए व्या ईन, तुईके खम थारे नहीं कि अंडर बलिए ले आगी कि याप लिए आन को षाः शबत का खेन ये तुब फर्सें, विसक्र अगे रेई!! तो देख सबते हो 36 के बात के कोमाम ने हताते है। तो ये एक और एसकेप सीक्वेंस होता है, बैख शलच भी, योंकी बैख स्पेस का खाम करते है। और इसको मैंने लूप के बहार लिख हैं, तो अब दियान रख हैगा। तो बी एक बेस को लिए खॉस करने की आदेड डालो था कि अप को पता चलेगी काँन्सा वो लिए रख हो लिएगा। तो उमनेद करता हों एक तो अपको फार लुक का बैसिक समझन आगया। अध थी आप ले बैनफिष्यल होगा तो आप वोडञ्खिन बिघिू सेरी of using this video series और जरग से आद आद अपने फ्रिंष के साथ न को शेए सेर करेंप या सेर करेंफार्खी मुजे भी पोपलरंटी मिले और आप lack of good education अपके प्रैंट्स पुस्कों लुज कर। तो ख़ेंग्यो फाँचिन दिस वीड्यो