C PROGRAMMING #3 :LOOPS

Let’s suppose you want the computer to display “www.teachfullstack.com” 10 times.

printf(“www.teachfullstack.com”);

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.

  • #include<stdio.h>
  • #include<conio.h>
  • void main()
  • {
  • int i=1
  • while(i<=10){
  • printf(“www.teachfullstack.com”);
  • i++;
  • }
  • }

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()

For loop

  • #include<conio.h>
  • #include<stdio.h>
  • void main()
  • {
  • int i=1;
  • for(i;i<=10;i++)
  • {
  • printf(“www.teachfullstack.com”);
  • }
  • }
  • 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

$2.00

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.