Added book examples
This commit is contained in:
22
C8/if_example.c
Normal file
22
C8/if_example.c
Normal file
@@ -0,0 +1,22 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Declare some variables
|
||||
int a = 7, b=6;
|
||||
|
||||
// A single line of code conditional on the value of a
|
||||
if ( a == 7 )
|
||||
printf ("The value of a is 7 - so I will do this\n");
|
||||
|
||||
// Multiple lines of code conditional on b not equalling 4
|
||||
// so then need to be placed inside { and }
|
||||
if ( b != 4 )
|
||||
{
|
||||
printf ("The value of b is not 4\n");
|
||||
printf ("So I will do multiple tasks\n");
|
||||
}
|
||||
|
||||
return 0; // Exit from main
|
||||
}
|
29
C8/switch_example_1.c
Normal file
29
C8/switch_example_1.c
Normal file
@@ -0,0 +1,29 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Declare some variables
|
||||
int a = 1;
|
||||
switch ( a )
|
||||
{
|
||||
case 0 :
|
||||
printf ("Sunday\n");
|
||||
break ; // end of the lines of code to execute
|
||||
|
||||
case 1:
|
||||
printf ("Monday\n");
|
||||
break ; // end of the lines of code to execute
|
||||
|
||||
case 2:
|
||||
printf ("Tuesday\n");
|
||||
break ; // end of the lines of code to execute
|
||||
|
||||
// etc...
|
||||
|
||||
default: // If no case is met (OPTIONAL)
|
||||
printf ("\nThe value supplied is out of range\n");
|
||||
|
||||
}
|
||||
return 0;
|
||||
}
|
25
C8/switch_example_2.c
Normal file
25
C8/switch_example_2.c
Normal file
@@ -0,0 +1,25 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Declare some variables
|
||||
int a = 1;
|
||||
switch ( a )
|
||||
{
|
||||
case 1 :
|
||||
case 2 :
|
||||
case 3 :
|
||||
// Code to do if a is 1, 2 or 3
|
||||
break ; // end of the lines of code to execute
|
||||
|
||||
case 4:
|
||||
case 5:
|
||||
// Code to do if a is 4 or 5
|
||||
break ; // end of the lines of code to execute
|
||||
|
||||
default: // If no case is met (OPTIONAL)
|
||||
// Code to do if no case is met
|
||||
}
|
||||
return 0;
|
||||
}
|
23
C8/switch_example_3.c
Normal file
23
C8/switch_example_3.c
Normal file
@@ -0,0 +1,23 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Declare some variables
|
||||
int a = 1;
|
||||
switch ( a )
|
||||
{
|
||||
case 1 :
|
||||
printf ("This is case 1\n");
|
||||
case 2 :
|
||||
printf ("This is case 2\n");
|
||||
case 3 :
|
||||
printf ("This is case 3\n");
|
||||
break ; // end of the lines of code to execute
|
||||
|
||||
default: // If no case is met
|
||||
printf ("This default case\n");
|
||||
// Code to do if no case is met
|
||||
}
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user