OldSchoolCode

.com



This tutorial has to do with while and do while loops.
This tutorial is about "while" loops and "do while" loops. Loops are a lot like "if" statements, except they keep running the same code until the conditional statement is false.

For example:
int c = 0;

while ( c < 5 )
{
    c++;  //c++ is the same as doing c = c + 1;
}
This is saying: while c is less than 5 keep executing the code in the while loop.The code in the while loop then continues to add 1 to c, so as soon as c == 5 the while loop will break and any code below it will run.

Here is another example:
while ( c < 5 )
{
    printf( "\nhello my friend!" );
}
In this loop, we are never changing c! This means the while conditional will always be true and it will print "hello my friend" forever.That is called an infinite loop.

There are some cases when you want it to execute code first, and then check the conditional.This is called a "do while" loop.  It means do "this" then if the conditional is still false, do "this" again.

Example:
do
{
    printf( "\nhow old are you?" );

    scanf( "%d", &age );
}
while ( age < 21 );
The above program will ask how old we are. Then it checks the user's input, if the user put a number less than 21 it would ask again.

You can also use multiple conditionals for the loops, just like with "if" statements.
//this will loop while c is less than 5 or a is less than 5
 
while ( c < 5 || a < 5 )  

//this will loop as long as c AND a are less than 5.
 
while ( c < 5 && a < 5 )

#define _CRT_SECURE_NO_DEPRECATE

#include <stdio.h> 
#include <conio.h>
   
void main( void )
{
   float a, c, d;

   char b, play;

   int code, trys;

   //the access code
   code = 300;

   //starting point!! not number of tries
   trys = 0;   

   //print this until conditions are met
   //in the "while" statement
   do 
   {
      //this will increment by 1 until it 
      //reaches 5 in the "while" statement, 
      //without this it would loop forever 
      trys++;	 
                
      printf("enter access code :" ); 

      scanf( "%d", &code );
   }
   while ( code != 300 && trys < 5 );
   
   // if access code = "300" print "enter a number"
   if (code == 300 )
   {   
      do
      {  
         printf( "Enter a number:" );
         scanf( "%f", &a );

         //clears input buffer to accept next input
         fflush( stdin );

         //enter symbol +-*/
         printf( "Enter a symbol:" );
         //shows symbol entered and holds symbol in 
         //var &b note the %c here for use with symbols
         scanf( "%c", &b );
         fflush( stdin );

         printf( "Enter a second number:" );
         scanf( "%f", &c );
         fflush( stdin );
        
         if ( b == '+' )
         {
            d = a + c;
            printf( "%.2f + %.2f = %.2f\n", a, c, d );
         }

         if ( b == '*' )
         {
            d = a * c;
            printf( "%.2f * %.2f = %.2f\n", a, c, d );
         }

         if ( b == '-' )
         {
            d = a - c;
            printf( "%.2f - %.2f = %.2f\n", a, c, d );
         }

         if ( b == '/' )
         {
            d = a / c;
            printf( "%.2f / %.2f = %.2f\n", a, c, d );
         }
         
         //notice we use %c here because we're
         //reading character, not a number
         printf( "Do you want to play again, y or n: " );
         scanf( "%c", &play );
      }
      while ( play == 'y' );
   }

   else 
   {
      printf( " bye " ); 
   }
   
   getch( ); 

   return;
}