Saturday, February 1, 2014

C programming - While loop




Lesson 6

( While loop )


Hi and welcome back on learn for free programming!

In this lesson we are going to talk about while loop. While loops are very important for programming, almost 99% program that are made contain while loop. In lesson 5 we created a simple calculator but you can use it only once. Putting while loop in that code you can use it to calculate numbers as many as you like ( for example : until you press the ESC key ).


Okay i will put that code on the end of this lesson but first let's get back on while loop.





While loop



- in code :

  while (condition ){
            command;
   }

- As you can see in algorithm and code, first the condition must be true for while loop to work. If condition is false program will skip every thing inside while loop block ( in this case command ).


While command checks the condition at the top of the loop. This means that the body of the command will not executed if the determination of the calculation result to zero.


for example : 


   i=5;

   while ( i>0 ){
           printf( " %d ", i );
           i--;
   }

In this case while loop will work, but if i put i=0 then condition is going to be false and while loop isn't going to work it will just skip all commands inside while loop block.



          Empty command



Empty command is only semicolon. Although it may not seem like a particularly useful structures, it's used sometimes in repeat orders and elections.


- example of empty command :


    char c;

        ....
    while( (c=getch()) != 'a' ) ;

This more like forever while loop, but it might end by pressing "a" key and this isn't useful command. There are some specific situations where this is useful.



            Command continue 


The continue statement inside the loop forces the loop to begin its next repetition. All commands behind continue are being skipped.


Example of continue


Continue command relates only for the command while, for or do which directly includes.
Commands for and do we are going to do in next lessons.

As i said on the beginning, now we are going to make calculator that end's when you want.


follow my steps :





  1. So first thing you need to do is to include header files.
  2. Open the starting "block" of program ( int main() { and close it } ).
  3. We are going to need two variables for our numbers. I will name them num1 and num2, because they are integer numbers we have to type int num1, num2 ;
  4. Next thing we need is operation ( + , - , / , * ). Type int op.
  5. Before we enter numbers and operation we need while loop to make it repeat.
  6. Now user has to enter numbers and operation, so we have to type :

         printf("Number 1 = ");
         scanf("%d", &num1);
         printf("Number 2 = ");
         scanf("%d", &num2);
         printf("operation =  ");
         op=getch();
  7. Now we need switch.

    switch(op){

    case"+": printf(" num1 + num2 = %d", num1+num2);
                    break;
    case"-": printf(" num1 - num2 = %d", num1-num2);
                    break;
    case"*": printf(" num1 * num2 = %d", num1*num2);
                    break;
    case"/": printf(" num1 / num2 = %l", num1/num2);
         
    }



-Code :


#include <stdio.h>
#define ESC 27  //------------> ESC key in ASCII code is 27 and i'm defining it here so when
                              //------------> ever i type ESC it will know what is it
int main()   
{
        int num1,num2,op;
        float result;
        clrscr();

        while( op != ESC ){        //------------------> != means different 


        printf("Number 1 = ");
        scanf("%d", &num1);
        printf("Number 2 = ");
        scanf("%d", &num2);

        printf("operation =  ");
       op=getch();

       switch(op){
                case"+": printf(" num1 + num2 = %d", num1+num2);
                               break;
                case"-": printf(" num1 - num2 = %d", num1-num2);
                               break;
                case"*": printf(" num1 * num2 = %d", num1*num2);
                             break;
                case"/": printf(" num1 / num2 = %l", num1/num2);
        }
        getch();
       clrscr();
       }
}


Putting while we are making program to work until pressing ESC key.



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 and i will see you next time :) .

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

Byee.






No comments:

Post a Comment