Control Statements in C - while loop | C Language Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome to nourish technologies this is Finn was so in this session so we are going to discuss about a while loop in C language so loop control statements so very very important in logical programming right here it is this is one of the control statements right loop control statements so that is a while loop so what is the definition of a while loop and what is the syntax a flowchart and some of the examples we will see in this session okay see so first one so what is a while loop so generally a loop is nothing but execute a block of instructions execute a block of instructions instructions repeatedly repeatedly as long as the condition is true how many times it will repeat means that is based on the condition that is based on the given condition we have given given condition okay so for example if condition is a true it will execute and then it will repeat once again the same block so when it will terminate only if the condition fails if you see the syntax and the flowchart parallely then you will get more clarity on while okay see what is the syntax what is the syntax of while loop syntax here while and here it is a condition and next inside the block we are writing statements just looking like a if block statements only write condition and statements but flow is different right flow is a differ from if condition how the flow will be observed so first as usual starts and here it is in a flowchart start so we are representing with oval symbol and next here it is checking the condition so condition checking and for every condition true and false two options will be present condition if it is a true what will happen and if it is a false what will happen that we need to check right here start it will check the condition suppose if condition is true if condition is true what all the statements defined inside the block execute this is just considered a parallelogram and here it is all the statements inside the block execute statements so after execution of a statements will it end no it will not end it will not end after execution of statements once again it will go and check the condition once again it will go and check the condition it will repeat here only how many times as long as the given condition is true first it will start next if condition is a true statements execute then it will continue here only it will repeat again it will check here only how many times as long as the given condition is true suppose if condition fails if condition Falls then the control come to and this is end we are representing we are representing with oval oval so now the control come to and come to end this is if condition has failed if condition has failed then the control come to end this is execution flow of a while loop simple okay suppose if it is a if block after execution of a block only once right the control come out of the block but in this case how many times it will repeat means that is simply based on the given condition simply a based on the given condition okay so this is a syntax and as well as a flowchart syntax and flow here how it executes some of the examples we'll see some of the examples here suppose I am writing while while and inside I am printing MS is printf a message is a loop message is a loop so what is the condition any condition we are giving for example here it is minus 11 we are giving so tell me - 11 is a true value or false value it is a true value we know that except 0 all the integers either positive or negative comes under positive values comes under true values so here so first it will check the condition condition is true so come inside come inside and it will print this message loop and next after printing whenever the control come to here again it will go and check the condition once again again it will check the condition sir condition is a true or false again condition is a true come inside loop will print again it will go on check right so this is infinite iteration loop because always the condition is true just run this program infinite iterations loop is executing continuously loop okay so this is infinite loop so now some more examples for example here I am declaring I value is 1 I value 1 suppose if it is a short integer so what is the short integer limits short integer limits that is minus 3 2 7 6 8 2 plus 3 2 7 6 7 so these are the limits - 3 - 7 6 8 2 + 3 - 7 6 7 so these are the limits of short integer short so why I am writing here means here inside I am writing simply a plus plus I or directly only one I am giving 1 and here we are printing the message I value what is the value I am printing I and after printing the I value I am increasing the I value so modify operators we discussed already so here it is plus plus I now how it executes observed condition is a true come inside it will print the I value what is the I value 1 next I value become 2 again it will repeat condition is a true write I value it will print 2 3 4 5 up to 3 - 7 6 7 it will print sir after 3 to 7 6 7 what will happen sir it will not terminate because again right the loop will repeat with the minus 3 - 7 6 8 value - 3 - 7 6 8 is a true value or false value it is a true value so again it will repeat right up to 0 it will repeat but sir whenever it reaches 0 will it stop no reason condition is always true because so directly hard-coding 1 I have given it is not I write whenever you are giving I here come in right whenever it reaches 0 it will stop but here it is not I it's 1 right so it will print 0 again it will start 1 2 3 again it is infinite loop but that infinite loop will fall within this range only - 3 - 7 6 8 2 plus 3 - 7 6 7 observe here here it is starting here it is 17 18 3 - 7 six seven again negative values whenever it reaches zero again it starts positive value again it reaches three to seven six seven again negative values right in that limit set only right it will repeat infinite times sir suppose here instead of giving one I am giving I what is that I value initially one so in this program how it will print means first it will starts with one two and so on three to seven six seven and next it will continue with the minus 3 to 7 6 8 comma minus three to seven six seven comma and so on minus 2 comma minus 1 whenever it reaches zero whenever I value is zero whenever I value is zero here it is while 0 condition false Here I am not giving one here I am giving I so it is last value is considered as a false value then it will pray it or it will stop so up to minus 1 only it will print so it is not infinite loop it is a finite loop only it is a finite loop see just compile and run - 3 2 7 6 7 and here it is reaches 1 whenever it is 0 after - whenever whenever it is 0 it will stop it will terminate the program so this is not infinite loop it is a finite loop only it is a finite loop so this is a program and one more example will see here one more example for example character we are taking signed character signed character CH equals 2 suppose 120 I am taking and here we are writing directly CH + + CH we are writing + + CH and we are printing that CH value CH percentage D a tab space I am giving tab space same story sir what is the range of a minus means what the sign the character means minus 128 2 plus 127 minus 128 2 plus 127 is the character range so within the range only the loop will repeat so first CH value is a 120 but whenever we are writing plus plus CH here only the value will increase here only the value increases plus plus CH the value become a 1 yes 121 is a true value the control come inside it will print 121 next 120 2 3 4 5 6 7 and whenever it reaches 127 it will not stop again it will start with the minus 128 minus 127 up to 0 whenever it reaches 0 it will stop see the output of this program starts with 121 122 3 4 5 and so on 127 next 120 minus 128 minus 127 it will continue up to minus 1 whenever the value is 0 it will terminate this is the case okay now using while loop I just want to print only one two ten numbers what is your requirement so based on the requirement only you should write the program so I just want to print only one two ten numbers that's it so input is very clear and output is also clear input and output both are clear here input so that is a I value is 1 up to limits lower bound and upper bound here it is a here it is upper bound is a I is less than or equals to 10 input and output is clear right here it is a upper bound lower bound upper bound is also clear I value starts with 1 up to 10 I want to print so 10 times it should repeat I is less than or equals to 10 and here it is we are printing the I value percentage D percentage D slashin I but more clearly a message also I am printing I equals to I equals to I value 1 but in the next iteration it should print it too so how to print simple plus plus I plus plus I next it will become 2 again condition is at root 2 is less than or equals to 10 next it will become 3 4 5 after printing 10 it will become 11 11 is less than or equals to 10 condition will fail so then it will stop so output is a 1 to 10 it will print output is 1 to 10 for example here instead of writing a two statements as soon as printing the I value if you want to increase the value simply post increment we are writing both are same post increment suppose if it is a pre increment it will start printing from two because i values a 1 so plus plus i means what it will increase first and then printer so that is why if you write a post increment i plus plus first it will print 1 and then the value become 2 next it will print a 2 and the value become 3 right it will print the same output it will print same output but here within one line only so we are writing the logic we are writing logic only in a one line this is so printing 1 to 10 sir I want to print I want to print only even numbers from 1 to 10 in this program we are printing all the I values all the numbers from 1 to 10 we are printing but now I want to print only even numbers so instead of printing I value directly instead of printing I value directly now here we are writing plus plus I separately if you print I value directly right all the values it will print 1 to 10 but now only even numbers so print the I value only if it is an even number simple so now printing this high value on condition sir what is the condition first check I value is even number or not even or odd already we know a simple condition then print now observe here it is I mod 2 equals to 0 first I value 1 1 mod 2 equals to 0 condition fail so 1 will not print this if block will skip directly a value increases plus plus I value become 2 2 is less than or equal student condition true come inside and 2 mod 2 this condition is also true so control move inside it will print the output value is 2 output is a 2 it will print next plus plus I value become 3 3 is less than or equals to 10 condition true and here it is control move inside 3 more 2 2 condition false again if block will not execute next I value become 4 4 is less than or equals to 10 condition true come inside for mod 2 equals to 0 this condition is also true so come inside it will print that I value 2 so like this only even numbers will print within the range of 1 to 10 right here it is simply we are adding the condition to the previous example to print all the even numbers in the given range okay so here it is run the program two four six eight ten two four six eight ten okay so this is the uses of while loop in C language okay so in the next session we will see some more examples on while loop and as well as nested while loop how a nested while loop works how a nested while loop will run in a c programming language so thank you thank you for watching for more videos please subscribe to nourish ID
Info
Channel: Naresh i Technologies
Views: 494,568
Rating: 4.8458295 out of 5
Keywords: C Language, While Loop 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: _2weEBxDTo8
Channel Id: undefined
Length: 18min 41sec (1121 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.