Wednesday, August 26, 2009

Loops

Loops
  • Loop is to repeat the action over and over again
  • This condition to terminate or exit a loop is referred as loop control expression
  • C provide two looping statements namely: for, while and do...while

The while loop
  • pretest loop
  • It used an expression to control the loop
  • It test the expression before every iteration of the loop
       while (expression){
             Statements
       }
  • As long as the expression is evaluated to true the statements in the loop body will be repeated.

The for loop
  • a pretest loop
  • uses three expression
  • first expression contains initialization statements
  • the second contains the limit-test expression
  • the third contains the updating expression
  • A for loop is normally used when the number of times of the loop to be executed are knowns
       for(initialization; limit-test; updating){
             Statements;
       }
  • As long as the limit-test is evaluated to true the statements in the loop body will be repeated.

The do...while loop
  • a post-test loop
  • It also uses an expression to control the loop
  • It tests the expression after the execution of the body
  • The do…while loop end with a semicolon
  • The do…while loop only test the expression at the end of the loop, the body of the loop will be at least execute for once
       do{
              Statements;
       }while (expression);
  • As long as the expression is evaluated to true the statements in the loop body will be repeated.

Tuesday, August 25, 2009

Selection Statement

Selection Statement
  • A selection statement is a control statement that allows choosing between two or more execution paths in a program.
  • C provide one-way, two-way and multiple conditional branching execution.
  • C provides this branching through two methods namely if statement and switch statement
If
  • There are three ways of writing if statements.
if
       if(expression){
                Statements;
        }
  • The expression is evaluated to either true or false.
  • True is nonzero value and false is zero value.
  • The Statements inside the if will only be executed if the expression is evaluated to true
if…else
        if(expression){
                Statements1;
        }else{
                Statements2;
        }
  • If the expression is evaluated to true then Statements1 will be executed.
  • If the expression is evaluated to false then Statements2 will be executed.
if…elseif
        if(expression1){
                Statements1;
        }elseif (expression2{
                Statements2;
        }elseif (expression3){
                Statements3;
        }else{
                Statements4;
        }

Switch Statements

  • Switch is a composite statement used to make a decision between many alternatives
  • The expression of switch statements must be evaluated to either integer type or character constant.
  • The selection condition must be one of the C integral types

        switch (expression){
               case constant-1: statement;
                                               …
                                              statement;
                                              break;
               case constant-2: statement;
                                               …
                                              statement;
                                              break;
               case constant-n: statement;
                                               …
                                              statement;
                                              break;
               default                : statement;
                                               …
                                              statement;
                                              break;
         }
  • Each case label must be a constant expression either integer or character
  • The case label simply provides an entry point to start executing the code
  • Default is executed whenever none of the other case values matches the value in the switch expression
  • The break statement causes the program to jump out of the switch statement; continue with the code after the switch statement

Saturday, August 22, 2009

Expressions

Expressions
An expression is a sequence of operands and operators that reduces to a single value
An operator is a language-specific syntactical token that requires an action to be taken.
An operand receives an operator’s action.

Precedence rule used to determine the order of execution between different operators in an expression. The higher the number, the earlier the execution.
Associativity rule used to determine the order of execution in which the operators with the same precedence level.

Assignment Expression
Assignment Expression evaluates the operand on the right side of the operator (=) and places its value in the variable on the left.

Postfix Increment/Decrement
The value of the postfix increments (decrements) expressions is determined before the variable is increase (decrease).

Example

int a = 7;

printf("%d\n", a)
printf("%d\n", a++)
printf("%d\n", a)

Output:
7
7
8

Prefix Increment/Decrement
The value of the prefix increments (decrements) expressions is increase then the value is determined.

Example

int a = 7;

printf("%d\n", a);
printf("%d\n", a++);
printf("%d\n", a);

Output:

7
8
8

Friday, August 21, 2009

Printf and Scanf

Printf
Used for formatted output in C.
Writes formatted data to the monitor
Printf needs two things
  • Instruction for formatting the data and
  • The actual data to be printed

Example:
          int a;
          char b;
          float c;
          double d;

          printf("%d %c %f %lf", a, b, c, d);

Conversion Code:

int           %d
char        %c
float        %f
double    %lf

Scanf
Used for formatted input in C.
Reads formatted data from the keyboard
It consists of two parts
  • A format string that describes the data and
  • an address list that identifies where data are to be placed in memory
scanf requires that variables in the address list be represented by their address.
To specify an address, you prefix the variable name with the address operator, the ampersand (&)

Example
          int a;
          char b;
          float c;
          double d;

          scanf("%d %c %f %lf", &a, &b, &c, &d);

Friday, August 14, 2009

Data Type, Variables and Constant

Data Type
The are four basic data type for C namely:
  • char (character)
  • int (integer)
  • float (floating-point number)
  • void
Variables

Variables are named memory locations that have a type.

Must declare and define in order to used it.

Example

          int a;
          float b;
          char c;

Can not declare a variable of type void!!!
Can initialize a value to it during the declarations refers as initialization.

Example

          int a = 1;
          float b = 1.0;
          char c = 'a';

Constant

Data values that cannot b changed during the execution of the program.
Character constants are enclosed between two single quotes.
Example:
          'a'
          '\n'
String constant is a sequence of zero or more characters enclosed in double quotes
Examples:
          "a"
          "ums"

C Program Structure

The C program structure serve as a important basic for understanding how to write a C program. This structure describe the important component for writing a C Program.

           Preprocessor Directives

           Global Declarations

           int main (void){

           Local Definitions

           Statements

           return 0;
           }