------------------------------------------------
            "Introduction To C:
 Chapter 2: Expressions, Operators & Statements"
------------------------------------------------
  C/O :: Dr4g of DynamicHell Development Team
------------------------------------------------
  http://dynamichell.org | irc.dynamichell.org
------------------------------------------------

------------
Expressions
------------

C Expressions, are anything that evaluates a numberic value.
They come in all kind of shapes and sizes.

Expression		Definition
----------              -----------
PI or MAX		A symbolic constant defined somewhere within the program.
Time or age		A Variable
50			A Literal constant

Complex Expressions.
--------------------
Complex expressions are built up of smaller, more simple expressions, as Follows.
5 + 3
or
5.2+ 4.8 / age + (vat * 10)

This is where the flexibility of C comes into place.
x = 5 + 2 - a
This expression means that 5+2-a and assigns the result to variable x.
The following assigns the result of 5+2-a to two variables, x and y.
y = x = 5 + 2 – a.


----------
Operators
----------
The following are C Operators.

== Equal To
!= Not Equal To
|| Or
&& And
< Less than
<= Less than or Equal To
> Greater Than
>= Greater Than or Equal To

The Following are C Mathematical Operators.

++ Increment
-- Deincrement
+= Add Something To Value
-= Deduct Something From Value


-----------
Statements
-----------

Input and Output Statements
----------------------------
Printing to the screen
-----------------------
In C there are many many ways to do things, but we will use the simplistic printing stataments here.
"printf()" is a function that outputs to the console(STDOUT).

From the function name you can guess what it is about and the f just means formatted.
Meaning formatted outputs, you can set it to output in certain formats and numerial formats like decimal points.

Syntax
--------
printf(“ Hello World ”);

This will print "Hello World" to the screen.

There are other text formatting expressions, named escape charcters, as follows.
\n - new line
\t -  insert tab
\a - nice little computer beep :o)
\r - carridge return

So the following would take a new line then beep :o)
printf("Hello\nWorld\a");

Output
------
Hello
World
*beep*


If we want to print a variable the synax would be.
myInt = 5;
printf("The Value of The Integer is %d", myInt);

Output
-------
The Value of The Integer is 5

The %d means that it is an integer(decimal value),
if we had ("Hello World Your Age is %d - Your Password is %d",myAge,myPassword);      

You can see that after the quotes are closed, you declare your variables,
which it outputs them in the order you set them, like above (myAge,myPassword).

There are ofcourse other formats as follows;
%i :: %d - Integer
%c - Char Type (character)
%f - Floating Point Value (decimal number (4.5))
%s - String
%x - Hex value
And so on.


Overview
---------
We now know typical expressions of C.
We now know all common operators of the C language.
We now know how to print a value to the screen in a formatted way,
using printf.


Example Program
-----------------
#include <stdio.h>

int intAge = 10;
int intPass = 123456;
char chFirst = "P";

int main() {

  printf(“Welcome To The Program \n\n\a”);
  printf(“Your Age is %d, Your Password is %i”,iAge,iPass);
  return 0;

}
-----
 EOF
-----
Explanation
------------
We include the main C library which manages input and output functions.


Taking in a Value
------------------
In C there are many ways to do things, but we will use scanf here to take in a value from the console(STDIN).

Scanf
-----
From the name you can see, that Scan means take in a value, and f still means formatted.

Syntax
-------
scanf(“%d”,&myAge);
%d means the same as printf - Integer.


The & Symbol
-------------
The Apersand Symbol "&" means "Address of" something.
It will store the Integer Value of myAge in the memory address that the value of myAge is held.

If we done the following
 printf("The Address of myAge is %d",&myAge);
This would display the "address" that the myAge value is stored in memory.
Example: 0x55BBFF 


------------
Statements
------------
The if Statement
The if statement can consist of IF,ELSE and ELSEIF.
The if statement is used to test program control statements, and a number of conditions,
and act accordingly to that condition.

The syntax
-----------
if (condition)
statement;

If an expression is enclosed in brackets with no condition, then it is set to default as true.
 
An if statement can control the execution of statements, or blocks.
A block is a group of two or more statements, enclosed in braces

The syntax
-----------
if (condition == 5)
   {
      statement;
      statement2;
   }
else
      statement3;


Pros and Cons
-------------
Pros: Remember to indent your code, within braces, that goes for groups too.
Cons: Remember, there is no semi-colon at the end of the if statement
Good: if (condition)
Bad: if(condition);

 

The Switch Statement
---------------------
The switch statement is a function related to the "if" statement.
The switch statement is used for testing many conditions.
However there is a time and a place for both.

Scenario 1) If there are not a lot of conditions to be tested, the formal IF statement would seem a logical choice.
Scenario 2) If there are many conditions being tested, then the switch statement would seem the mode logical.


Standard Switch Syntax is as Follows
------------------------------------
switch (variable) // variable being conditioned.
{
case 5:
	/* do your Code Here
	   do some more here */
	break;
case 'a':
	// Do some other code here 
	break;
case <=10:
	/* do some further code here. */
	break;
default:
	/* do the default code here */
	break;
}


Now you can see the simplicity in the switch statement,
compared to the ELSEIF statement.
It works like follows.

We're working with the variable, saying if this is the case 'do this' or if this is the case 'do something else'.
The case default, is like the else statementin the if,
it just means if none of the above conditions have been met, do this.

The break is a function, you can see this by the semi-color at then end of the line.
This means break out of function, or loop.
In this case it means, break out of the Switch statement, once this condition has been executed,
then continues on with the rest of the program.(after the switch).

Example Program
----------------

/* This program will give the user a menu screen
   And will act accordingly, depending on their choice.
*/

#include <stdio.h>

int intMyChoice;
int intValue1,intValue2;
int result;

int main() {

   // Print the Menu
   printf("Welcome To The Control Panel\n");
   printf("You Have one of the Following Options\n\n");
   printf("1. Add 2 values\n2. Subtract 2 values\n");
   printf("3. Multiple 2 values\n\nEnter Your Choice: ");

   // Take in the Menu Choice
   scanf("%d",&intMyChoice);

   /*
   IF the inputted number is 1 or 2 or 3
   print "good" and continue with
   the rest of the program.
   ELSE: print error message and exit the program.
   */

   if (intMyChoice == 1 || 2 || 3) { 
        printf("\nGood\n");
   }
   else {
        printf("\nPlease Enter Values 1 to 3");
        exit(1);
   }

   // Stating the variable we wish to condition.
   switch(intMyChoice) {

   // If the case is "user entered value 1"
   case 1:
	// Take in value 1
        printf("\nEnter Value 1: ");
        scanf("%d",&intValue1);

	// Take in value 2
        printf("\nEnter Value 2: ");
        scanf("%i", &intValue2);

	// Calulate total & print it
        result = intValue1 + intValue2;
        printf("\n\nThe Answer is: %d\n",result);
        break;

   // If the case is "user entered value 2"
   case 2:
	// Take in value 1
        printf("\nEnter Value 1: ");
        scanf("%d",&intValue1);

	// Take in value 2
        printf("\nEnter Value 2: ");
        scanf("%i", &intValue2);

	// Calulate total & print it
        result = intValue1 - intValue2;
        printf("\n\nThe Answer is: %d\n",result);
        break;

   // If the case is "user entered value 3"
   case 3:
	// Take in value 1
        printf("\nEnter Value 1: ");
        scanf("%d",&intValue1);

	// Take in value 2
        printf("\nEnter Value 2: ");
        scanf("%i", &intValue2);

	// Calulate total & print it
        result = intValue1 * intValue2;
        printf("\n\nThe Answer is: %d\n",result);
        break;

   // If the case does not meet any of these conditions, do this
   default:
        printf("There was an error");
        break;
   }
/* If the program manages to get as far as here.
We've successfully ran the program. */

return 0;
}

Explanation
------------
Follow the comments in the code.


Recap
-----
You should know the following
1) Typical expressions of C. From simple to complex.
2) All common operators of the C language.
3) How to print a value to STDOUT in a formatted way, using printf.
4) How to take in a value from STDIN in a formatted way, using scanf.
5) We now know some condition statements, the IF and the Switch.