Initial commit of lecture code
This commit is contained in:
42
LC9/do_while_example.c
Normal file
42
LC9/do_while_example.c
Normal file
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
This program shows how to use a DO/WHILE statement to keep displaying
|
||||
the key pressed on the screen. It terminates when the 'q' key is
|
||||
pressed
|
||||
|
||||
James Bonnyman
|
||||
*/
|
||||
|
||||
#include <stdio.h> /* Standard include files */
|
||||
#include <stdlib.h>
|
||||
#include <conio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
char x; /* Define a variable of type char */
|
||||
|
||||
|
||||
/* We now use the while statement. The 'loop' continues to execute */
|
||||
/* until such time as the expression in the brackets becomes false */
|
||||
|
||||
/* we use != , meaning 'Not Equal to' as the test, as we wish the */
|
||||
/* the statements to be executed every time we press a key other */
|
||||
/* than 'q'. Once we press 'q' the code continues on to the next */
|
||||
/* statement */
|
||||
|
||||
/* Note: This time the getch is in the while expression */
|
||||
|
||||
|
||||
do
|
||||
{
|
||||
x = getch();
|
||||
/* Display a message on the screen */
|
||||
printf("\nThe key you pressed was the %c key",x );
|
||||
}
|
||||
while ( x != 'q' );
|
||||
|
||||
// All done :-)
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
42
LC9/for_loops.c
Normal file
42
LC9/for_loops.c
Normal file
@@ -0,0 +1,42 @@
|
||||
/* Example Program */
|
||||
|
||||
/*
|
||||
This program shows how to write a simple loop that counts up
|
||||
from zero to 9.
|
||||
|
||||
A second loop then counts from 10 down to 1
|
||||
|
||||
Remember
|
||||
|
||||
j++ means j = j + 1
|
||||
j-- means j = j - 1
|
||||
|
||||
*/
|
||||
|
||||
#include <stdio.h> /* Standard include files */
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
char j; /* Define a variable of type integer */
|
||||
|
||||
|
||||
/* The count up loop */
|
||||
printf("\nCounting Up ... ");
|
||||
|
||||
for ( j = 0 ; j < 10 ; j++ )
|
||||
printf("\nThe value of j is %d",j );
|
||||
|
||||
|
||||
/* The count down loop */
|
||||
|
||||
printf ("\nCounting Down .. ");
|
||||
for ( j = 10 ; j > 0 ; j-- )
|
||||
printf("\nThe value of j is %d",j );
|
||||
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
47
LC9/while_example_version_1.c
Normal file
47
LC9/while_example_version_1.c
Normal file
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
This program shows how to use a WHILE statment to keep displaying
|
||||
the key pressed on the screen. It terminates when the 'q' key is
|
||||
pressed
|
||||
|
||||
|
||||
*/
|
||||
|
||||
#include <stdio.h> /* Standard include files */
|
||||
#include <stdlib.h>
|
||||
#include <conio.h>
|
||||
|
||||
|
||||
int main( void )
|
||||
{
|
||||
|
||||
char x; /* Define a variable of type char */
|
||||
|
||||
printf("Press a key ");
|
||||
x = getch(); /* Use getch to read a keypress and store the */
|
||||
/* result in 'x'. We do this initially so that */
|
||||
/* there is a value in 'x' for the while statment */
|
||||
/* to consider the first time round */
|
||||
|
||||
/* We now use the while statement. The 'loop' continues to execute */
|
||||
/* until such time as the expression in the brackets becomes false */
|
||||
|
||||
/* we use != , meaning 'Not Equal to' as the test, as we wish the */
|
||||
/* the statements to be executed every time we press a key other */
|
||||
/* than 'q'. Once we press 'q' the code continues on to the next */
|
||||
/* statement */
|
||||
|
||||
while ( x != 'q' )
|
||||
{
|
||||
/* Display a message on the screen */
|
||||
printf("\nThe key you pressed was the %c key\n",x );
|
||||
|
||||
/* Get a new value for X, if we do not the 'expression' will never */
|
||||
/* change, and the loop will go for ever */
|
||||
|
||||
printf( "Press another key or q to quit \n");
|
||||
x = getch();
|
||||
|
||||
}
|
||||
return 0;
|
||||
|
||||
}
|
37
LC9/while_example_version_2.c
Normal file
37
LC9/while_example_version_2.c
Normal file
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
This program shows how to use a WHILE statment to keep displaying
|
||||
the key pressed on the screen. It terminates when the 'q' key is
|
||||
pressed
|
||||
|
||||
*/
|
||||
|
||||
#include <stdio.h> /* Standard include files */
|
||||
#include <stdlib.h>
|
||||
#include <conio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
char x; /* Define a variable of type char */
|
||||
|
||||
|
||||
/* We now use the while statement. The 'loop' continues to execute */
|
||||
/* until such time as the expression in the brackets becomes false */
|
||||
|
||||
/* we use != , meaning 'Not Equal to' as the test, as we wish the */
|
||||
/* the statements to be executed every time we press a key other */
|
||||
/* than 'q'. Once we press 'q' the code continues on to the next */
|
||||
/* statement */
|
||||
|
||||
/* Note: This time the getch is in the while expression */
|
||||
|
||||
|
||||
while ( ( x = getch() ) != 'q' )
|
||||
{
|
||||
/* Display a message on the screen */
|
||||
printf("\nThe key you pressed was the %c key",x );
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
Reference in New Issue
Block a user