Let’s suppose you want the computer to display “www.teachfullstack.com” 10 times.
Will you write this line 10 times?
If the sentence is lengthier writing it could be an exhausting task.But programmers look for shorter ways.The shorter the code the better.
Hence instead of writing it 10 times we create a loop inside the compiler after it reaches the specified limit we make it to stop or break out of the loop.
Lets write a programme to do the above task through while loop which is the one of the loops in C.
- void main()
- int i=1
The above code first 2 lines are preprocessor commands.3rd line is the starting of our function.4th line constitutes the starting of function it tells the programmer this is the starting point of this function, if you don’t know what is function scroll down and you will see a post telling about functions.5th line declares a variable I to the value 1. 6th line is a loop which tells the compiler that until i is less than or equal to 10 keep executing the value of I is the number of lines the printf will execute.i++ tells the compiler to increment value by 1 after which the compiler checks if the incremented I is less than or equal to 10.Please see the last bracket is necessary to close the main()
- void main()
- int i=1;
- This code gives the same outputJust the syntax is a little bit different.We will learn more about differences later.While doing practical both of these loops will give the same output it depends on you whichever you want to use.
GIVE ME A CUP OF COFFEE
GET ME A CUP OF COFFEE SO I CAN ADD MORE POSTS IN LESS TIME