Sunday, February 2, 2014

C programming - Loop for



Lesson 7

( Loop for )

Hi and welcome back on learn for free programming!

In this lesson we are going to talk about loop for. For loop is very interesting and it's very easy to learn. With for loop you can make moving objects on screen just like screen saver, witch i will show you on the end of this lesson. But first let's learn the basics.

If the body is a big command of while loop, it can become difficult to understand and control the information loop with it to establish a connection.

The reader directly visible can only be control loop expression. Control variables are initialized somewhere before the command while, but modification of these variables are typically hidden inside the loop. Command for solves this problem by making the loop control information stored in one place at the top of the command loop.



( Picture 1 )
Loop for in algorithm 




In code it look's like this:

- example:

for( i=1; i<=10; i++) {                                                                     
         commands;
}



( Picture 2 )
Loop for in code


Code above shows command for with 3 control expressions. Expression for testing the condition is in the middle ( in this case i <= 10 ).


- simple example : Let's say you want to print numbers 1-100 on screen. Like this 

1
2
3
....
99
100

 Sure you wouldn't type printf 100 times, with for loop you can make it so much easier. All you need to type is this :

for ( i=1; i<=100 ; i++){
    printf("%d \n ", i);
}

Upon entering the for loop, the value of initialization expression is determined, then execution moves to conditional expression. If conditional expression is false for loop is done, but if it's true execution moves to update expression ( see picture 2 ).


Now as i said on the beginning of this lesson i will show you how to make moving text on screen. You will see some new functions but i will explain them.

- Code :

#include <stdio.h>
#include <conio.h>      //-----> we are including this header file for functions delay and gotoxy

int main()
{
         int x, y;
         clrscr();
         _setcursortype(_NOCURSOR);

         y=5;  

         for(x=3; x<30; x++){
              gotoxy(x,y);
              printf(" C Language ");    //------------> you can put whatever text you want to display
              delay(200);         // -------> if delay doesn't work try sleep(time) 
              clrscr();
         }
    
        return 0;
}




 _setcursortype(_NOCURSOR);  - is used to remove cursor from screen

 gotoxy(x,y);  - is used to position somewhere on the screen ( for example if you want to write                             something on the center of the screen you will use this function ). Coordinate                                 system in ci looks like this:



Coordinate system in C

delay( time );  -  is used to hold the screen ( "pause" the program ) for ( in this case ) 200 ms.



Okay that's it for this lesson come back to learn more about C.

I will be posting more lessons about C programming, so follow me, comment if there is something you would change :) .

If something doesn't work or you need help just email me on: sashans89@gmail.com and i will help you.

See you next time, Bye.


No comments:

Post a Comment