Control Statements in C - for loop | C Language Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome to knowledge technologies this is fin woz so today we are going to discuss about for loop in C language so this is one loop control statement how it executes so what is the syntax and execution flow some of the examples we'll see in this video right for loop so generally what is a loop user to execute a block of instructions repeatedly iterations write iteratively a set of instructions we are executing how many times as long as the given condition is true right so for loop is also used to execute a block of instructions for is a keyword right all are in a small case you have to write what is the syntax syntax of for loop here so we are writing initialization statement so what is initialization like I equals to 0 I equals to 5 right initialization followed by condition we need to write condition next one is modified initialization statement followed by the condition and modify statement and here it is we are writing the body so nothing but logic of a loop we are writing statements inside so this is simply for loop how it executes the flow so first initialization statement executes initialization after execution of initialization statement it will go and check the condition it will check the condition so after condition what will happen the control move inside if condition is a true the control move inside sir if condition is a false then it will terminate every loop terminates whenever the condition has failed so here it is if condition is a true the control move inside and it will execute the statements after execution of these statements now it will go and check the modify statement for so modify means what increment and decrement statements after modify once again it will check the condition so this is five sir if condition is true again then again control move inside a statements execute and then it will go and execute modify and then it will check the condition means the loop will repeat here only as long as the condition is a true it will execute initialization statement execute only once after execution of a condition the control move inside directly okay now we will see the flowchart flowchart off for loop execution flow of a for loop here it is flow start so first it will execute initialization statement statements in a parallelogram we are representing so this is initialization statement initialization after initialization so now here it is it will go and check the condition it will go and check condition sir if condition is a false then it will terminate directly the control directly come to end if condition is a true so first it will execute it will execute statements defined inside the block statements after execution of statements it will check it will go and modify modify statement executes after execution of modify statement again it will go and check the condition once again again if condition is a true same process here only the loop will repeat if condition has failed then the control come out of that and it will move to and okay so this is the flow right so one simple example we'll see right how write a program will execute right using a for loop so one example on for loop see main simple program just printing printing one to 10 numbers using for loop printing only just declare the variable declare the variable and all the three initialization statement one to ten numbers I value initializing with one initialization statement second one condition and incrementing statement all the three we are writing in a single statement only and inside we are printing the I value we are printing I value this much so very easy of using for loop here it is first I value starts with one initialization and next control command check the condition one is less than or equals to ten condition true come inside it will print the I value I value 1 it will printer after printing next it will go and modify I plus plus I will you become 2 after that here it is it will check right 2 is less than or equal to 10 condition true up to 10 it will repeat 10 times it will repeat whenever I value become 11 11 is less than or equals to 10 condition false it is a simple program now we will see how to find some of first n numbers using for loop so program is sum of first n numbers right so here so first we are reading n value we are reading in value so here I am declaring variable N and here it is we are asking printf enter n value enter n value next scanf we are reading the N value we are reading n value this consider n value is a 5 and value is five so now right I want to so print only just some of first file numbers only so we are repeating the loop we are repeating the loop to repeat the loop I variable is required I starts with 1 how many times it is not 5 it is n because n value varies that is depends on the end user input it is not a 5 always here it is I is less than or equal to n I plus place so here what we have to do sorry n value equals to 5 then we need to find the value of 1 plus 2 Plus 3 plus 4 plus 5 sum of first five numbers sum of first five numbers is our program 1 plus 2 Plus 3 plus 4 plus 5 so every time whenever we are adding the numbers right where we are storing so for that so declare one variable sum and initializes with 0 so wiser we are initializing with 0 it is the local variable in C language if you are not initializing any local variable that is implicitly initializes with a garbage value so whenever we are adding the values 1 2 3 4 5 so that will be added to garbage value finally it will give another garbage value so that creates a problem for you that is why so here it is sum equals to 0 right so now every time I value is varying from 1 to 5 so to the sum to the sum we are adding I value and finally the result we are storing into some result we are storing into some this is so how this statement executes very simple suppose if I value 1 I value varies from 1 to 5 so first I am writing I value 1 to 5 1 to 5 and initially some value is a zero sum value zero so here some equals to sum equals to sum plus I sum equals to sum plus I so here it is so what is the sum value every time just take some equals to every time we are storing into some only we are storing into some I value one two three four five five now observe initially what is the sum value 0 0 plus 1 will be stored into some so now what is the sum value 1 sum value 1 next this sum value will be stored here some value is a 1 so 1 plus 2 the value will go and stored into some what is it some value 3 next some value 3 3 plus 3 6 will go and store into some so here it is some value 6 next 6 plus 4 the value will go and store some value become 10 next 10 will be substituted 10 plus 5 15 so final value of sum is a 15 so like that the logic will execute so now so finally how we are printing very clearly printf sum of first n number is nothing but Phi numbers numbers is is percent is B here it is N and some we are printing how it executes sum of first n numbers n numbers means what Phi numbers because here user input is of 5 we considered fine umbers is some value it will print what is the sum value 15 so message will be very clear at the end sum of first five numbers is a 15 so this is simply how a for loop executes right how to find a sum of first n numbers using a for loop now we will see right how to check a number is a perfect number or not how to check a number is a perfect number this is using for loop once again now the concept is a perfect number perfect number sir what is the perfect number what is the perfect number means sum of factors of a given number except itself sum of factors of given number except itself is equals to the same number confusion just see the example you will get clarity suppose n value is 6 consider now we need to find the factors of 6 we need to find the factors of 6 here it is we are checking with the I value I value V as varies from 1 2 3 4 5 6 n is divisible by 1 or not n is divisible by 2 or not 3 4 5 actually no need to check with itself because except itself is there in the definition so here it is n is divisible by 1 or not right divisible means reminder should be 0 so reminder operator we are using n mod I equals to 0 or not yes next n mod 2 equals to 0 or not yes n mod 3 equals to 0 or not yes n mod 4 fail 5 fail so 1 is a factor 2 6 2 is a factor 2 6 3 is a factor 2 6 and of course 6 is a factor 2 6 but no need to check with itself so here it is just combined 1 plus 2 plus 3 again the result is a 6 it is equals to the N some of factors of a given number is equals to the same number is called a perfect number so how to write the logic very simple observe how to write the logic main I am declaring the variable n because we need to read I and as usual some we are taking zero sum now here we are reading the N value how to read using printf and scanf only enter n value enter n value and here it is we are scanning % is d address of en address of n and next one here it is we are checking for loop for loop from 1 to less than itself we are checking every number is a factor or not so I value starts with 1 2 less than in not less than or equals to because no need to check with itself important i plus plus i plus plus every time we are checking if the n value is divisible by I are not condition is a very clear if n value is divisible by I or not if it is divisible just add that I value to some add that I value to some sum equals to some plus I this is a program and here it is first I value 1 1 is less than or equals to 6 condition true come inside 6 mod 1 equals to 0 s 1 is a factor so 1 will be added to some some value initially 0 that will become 1 next I value become 2 2 is less than or equals to 6 come inside 6 mod 2 equals to 0 s condition true come inside that sum right 2 two is added to some so one plus two three next I value three six mod three equals to zero yes that will be added next I value 4 right 6 mod 4 equals zero fail next I value 5 6 mod Phi equals to zero fail next I value 6 6 is less than 6 condition false it will terminate so finally we are checking if the given number is equals to sum then we can call it as a perfect number perfect number else else printf it's not perfect number it is not perfect number this is example how to check a number is a perfect number or not means the perfect number means the number right the sum of factors of a given number is equals to same number of course except itself so how many factors we need to check using one for loop less than itself because no need to check with itself if it is a factor yes we are finding the sum all the factors sum we are finding finally we are checking if the finding some is equals to the N value then we can call it as a perfect number that is it now you people just try how to write the flow chart for perfect number how to write the flow chart for perfect number right in the next session we will see some more examples on for loop okay for more videos please subscribe to nourish id channel thank you
Info
Channel: Naresh i Technologies
Views: 653,314
Rating: 4.8901944 out of 5
Keywords: C Language, Loops in C, Srinivas, Naresh IT, Hands on C Language Training, C Language Demo, Online C Language Training, C Language Tutorial Videos, C Language Overview, C Language Interview Questions
Id: _NZZ9Y-j_mw
Channel Id: undefined
Length: 17min 30sec (1050 seconds)
Published: Tue Sep 20 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.