Initial commit of lecture code

This commit is contained in:
Louise Brown
2022-04-07 16:58:01 +01:00
commit 5b205092a7
60 changed files with 2413 additions and 0 deletions

42
LC9/do_while_example.c Normal file
View 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
View 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;
}

View 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;
}

View 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;
}