Initial commit of lecture code
This commit is contained in:
35
LC8/complex_if.c
Normal file
35
LC8/complex_if.c
Normal file
@@ -0,0 +1,35 @@
|
||||
/* EEEE1001/H61CAE Chapter 8 */
|
||||
|
||||
/* Example of a simple if statements */
|
||||
|
||||
|
||||
#include <stdio.h> /* Standard include files */
|
||||
#include <stdlib.h>
|
||||
#include <conio.h>
|
||||
|
||||
|
||||
int main( void )
|
||||
{
|
||||
|
||||
int a = 1, b = 6; // Define and intialise variables
|
||||
|
||||
|
||||
if ( (a == 1 ) && ( b == 6 ) )
|
||||
{
|
||||
printf("a is 1 AND b is 6\n" );
|
||||
}
|
||||
|
||||
if ( (a == 1 ) || ( b == 99 ) )
|
||||
{
|
||||
printf("a is 1 OR b is 99\n" );
|
||||
}
|
||||
|
||||
if (( b < a ) || ( a == 7 ) )
|
||||
{
|
||||
printf ("The value of b is less than a OR a is 7\n");
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
35
LC8/if_else_if_else.c
Normal file
35
LC8/if_else_if_else.c
Normal file
@@ -0,0 +1,35 @@
|
||||
/* Chapter 8 */
|
||||
|
||||
/* Example of a simple if statements */
|
||||
|
||||
|
||||
#include <stdio.h> /* Standard include files */
|
||||
#include <stdlib.h>
|
||||
#include <conio.h>
|
||||
|
||||
|
||||
int main( void )
|
||||
{
|
||||
|
||||
int x = 55; // Define and intialise variable
|
||||
|
||||
|
||||
if ( x == 2 )
|
||||
{
|
||||
printf ("The value of x was 2\n");
|
||||
printf ("I will now do something\n");
|
||||
}
|
||||
else if ( x == 3 )
|
||||
{
|
||||
printf ("The value of x was 3\n");
|
||||
printf ("I will now do something else\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf ("x is neither 2 or 3; I will do this instead!\n");
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
37
LC8/if_equals_example.c
Normal file
37
LC8/if_equals_example.c
Normal file
@@ -0,0 +1,37 @@
|
||||
/* EEEE1001/H61CAE Chapter 8 */
|
||||
|
||||
/* Incorrect use of a single equals sign in an if statement */
|
||||
|
||||
|
||||
#include <stdio.h> /* Standard include files */
|
||||
#include <stdlib.h>
|
||||
#include <conio.h>
|
||||
|
||||
|
||||
int main( void )
|
||||
{
|
||||
|
||||
int c = 6; // Define variable as initialise with value
|
||||
|
||||
printf ("\nThe current value in c is %d\n",c); // Display value in c
|
||||
|
||||
// This is correct
|
||||
if ( c == 1 )
|
||||
{
|
||||
printf("\nThe value of c is 1\n" );
|
||||
}
|
||||
|
||||
printf ("\nThe current value in c is %d\n",c); // Display value in c
|
||||
|
||||
// This is WRONG - it is setting c to one, this value is then tested
|
||||
// and, as non-zero, is considered to be TRUE
|
||||
if ( c = 1 )
|
||||
{
|
||||
printf("\nThe value of c is 1\n" );
|
||||
}
|
||||
|
||||
printf ("\nThe current value in c is %d\n",c); // Display value in c
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
34
LC8/if_examples.c
Normal file
34
LC8/if_examples.c
Normal file
@@ -0,0 +1,34 @@
|
||||
/* Chapter 8 */
|
||||
|
||||
/* Example of a simple if statements */
|
||||
|
||||
|
||||
#include <stdio.h> /* Standard include files */
|
||||
#include <stdlib.h>
|
||||
#include <conio.h>
|
||||
|
||||
|
||||
int main( void )
|
||||
{
|
||||
|
||||
int a = 1, b = 6; // Define and intialise variables
|
||||
|
||||
|
||||
if ( a == 1 )
|
||||
{
|
||||
printf("\nThe value of a is 1\n" );
|
||||
}
|
||||
|
||||
if ( a < b )
|
||||
{
|
||||
printf ("The value of a is less than b\n");
|
||||
}
|
||||
|
||||
if ( b < a )
|
||||
{
|
||||
printf ("The value of b is less than a\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
31
LC8/switch_1.c
Normal file
31
LC8/switch_1.c
Normal file
@@ -0,0 +1,31 @@
|
||||
/* EEEE1001/H61CAE Chapter 8 */
|
||||
|
||||
/* Example of a simple if statements */
|
||||
|
||||
|
||||
#include <stdio.h> /* Standard include files */
|
||||
#include <stdlib.h>
|
||||
|
||||
int main( void )
|
||||
{
|
||||
|
||||
int a; /* Define an int */
|
||||
scanf ("%d",&a); /* Get value */
|
||||
switch (a) /* Start of switch */
|
||||
{
|
||||
case 1:
|
||||
printf("Hi"); /* Case 1 */
|
||||
break;
|
||||
|
||||
case 2:
|
||||
printf("Bye"); /* Case 2 */
|
||||
break;
|
||||
|
||||
default :
|
||||
printf("Err"); /* Default */
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
34
LC8/switch_2.c
Normal file
34
LC8/switch_2.c
Normal file
@@ -0,0 +1,34 @@
|
||||
/* Chapter 8 */
|
||||
|
||||
/* Example of a simple if statements */
|
||||
|
||||
|
||||
#include <stdio.h> /* Standard include files */
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <conio.h> /* Required for getch() */
|
||||
|
||||
int main( void )
|
||||
{
|
||||
|
||||
char c; /* Define an char */
|
||||
c = getch(); /* Get value - no return required*/
|
||||
|
||||
switch (c) /* Start of switch */
|
||||
{
|
||||
case '1':
|
||||
printf("Hi"); /* Case 1 */
|
||||
break;
|
||||
|
||||
case '2':
|
||||
printf("Bye"); /* Case 2 */
|
||||
break;
|
||||
|
||||
default :
|
||||
printf("Err"); /* Default */
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
33
LC8/switch_3.c
Normal file
33
LC8/switch_3.c
Normal file
@@ -0,0 +1,33 @@
|
||||
/* EEEE1001/H61CAE Chapter 8 */
|
||||
|
||||
/* Example of a simple if statements */
|
||||
|
||||
|
||||
#include <stdio.h> /* Standard include files */
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <conio.h> /* Required for getch() */
|
||||
|
||||
int main( void )
|
||||
{
|
||||
|
||||
char c; /* Define an char */
|
||||
c = getch(); /* Get value - no return required*/
|
||||
|
||||
switch (c) /* Start of switch */
|
||||
{
|
||||
case '1':
|
||||
printf("Hi"); /* Case 1 */
|
||||
|
||||
case '2':
|
||||
printf("Bye"); /* Case 2 */
|
||||
break;
|
||||
|
||||
default :
|
||||
printf("Err"); /* Default */
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
Reference in New Issue
Block a user