Tuesday, September 23, 2008

PROGRAMMING-(paraiso)

This week we learned about:
  • The Structure of a Simple C Program
  • The Input or Output Statements
  • The Conditional Statements

THE STRUCTURE OF A SIMPLE C PROGRAM


#include
#define directive
main()
{
variable declaration section;
______________________
______________________
}

  • #include directive – contains information needed by the program to ensure the correct operation of C’s Standard library functions.
  • n#define directive – used to shorten the keywords in the program.
  • Variable declaration section – it is the place where you declare your variables.
  • Body of the program – start by typing main() and the { and }. All statements should be written inside the braces.


**C is a case sensitive program, therefore use lowercase letters only.


Commonly used include files in C language

  1. alloc.h – declares memory management functions.
  2. conio.h – declares various functions used in calling IBM-PC ROM BIOS.
  3. ctype.h – contains information used by the calssification and character convertion macros.
  4. nmath.h – declares prototype for the math functions.
  5. nstdio.h – defines types and macros needed for standard I/O.
  6. string.h – declares several string manipulation and memory manipulation routines.

Important Symbols:

a.) \n – is a line char used to move the cursor to the next line

b.) ‘ ‘ – single quote is used for single character / letter.

c.) “ “ – double quote is used for two or more character

d.) { - open curly brace signifies begin

e.) } – close curly brace signifies end

f.) & - address of operator

g.) * - indirection operator / pointer

INPUT AND OUTPUT STATEMENTS

Input Statement- A statement used to input a single character or a sequence of characters from the keyboard.

Types:

  • getch- A function used to input a single character from the keyboard without echoing the character on the monitor.

* Syntax: getch();
Example: ch = getch();

  • Getche- A function used to input a single character from the keyboard, the character pressed echoed on the monitor, line the READLN in PASCAL

* Syntax: getche();
Example: ch = getche();

  • getchar - A function used to input a single character from the keyboard, the character pressed echoed on the monitor terminated by pressing Enter key.

* Syntax: getchar();
Example: ch = getchar();

  • scanf - A function used to input a single character or sequence of characters from the keyboard, it needs the control string codes in able to recognized. Spaces are not accepted upon inputting. Terminated by pressing spacebar.

*Syntax: gets();
Example: gets(ch);

Output Statement- A statement used to display the argument list or string on the monitor.

Types:

  • printf - A function used to display the argument list on the monitor.
    It sometimes needs the control string codes to help display the remaining argument on the screen.

    *Syntax: printf(“control string codes”, argument list)
    Example: printf(“Hello, %d, you are % years old.”, name, age);
  • putchar - A function used to display the argument list or string on the monitor. It is like overwriting a character.

* Syntax: putchar();
Example:putchar(tolower(ch));

  • puts - A function used to display the argument list or string on the monitor. It does not need the help of the control string codes.

* Syntax: puts();
Example: puts(“hello”);


Format String and Escape Sequence

** All format specifiers start with a percent sign (%) and are followed by a single letter indicating the type of data and how data are to be formatted.


List of Commonly Used format specifiers

  • %c – used for single char in C
  • scanf(“%c”, &ch); printf(“%c”, ch);
  • %d – decimal number (whole number)
  • scanf(“%d”,&num); printf(“%d”,num);
  • %e – scientific notation / exponential form
  • scanf(“%e”, &result); printf(“%e”, result);
  • %f – number with floating or decimal point
  • scanf(“%f”,&pesos); printf(“%f”,pesos);
  • %o – octal number
  • scanf(“%o”, &valuet); printf(“%o”, value);
  • %s– string of characters
  • scanf(“%s”,&str); printf(“%s”,str);
  • %u – unsigned number
  • scanf(“%u”, &value); printf(“%u”, value);
  • %x– hexadecimal numbers
  • scanf(“%x”,&value); printf(“%x”,value);
  • %X – capital number for hexadecimal number
  • scanf(“%X”, &nos); printf(“%X”, nos);
  • %%– print a percent sign
  • scanf(“%%”,&value); printf(“%%”,value);


List of Commonly used escape sequence

  • \\ - prints backslash
  • \’ – prints single quotes
  • \” – prints double quotes
  • \? – prints question mark
  • \n - newline

GOTOXY- A function gotoxy is used to send the cursor to the specified location.


*Syntax: gotoxy(x,y);
Example: gotoxy(5,10);

NOTE:

** A format specifier %.2f can be used to limit the output being displayed into two decimal places only.

CONDITIONAL STATEMENTS

> Are statements that check an expression then may or may not execute a statement or group of statement depending on the result of the condition.

Types:

  • The If Statement
  • The If-Else Statement
  • The Nested-If Statement
  • The If-Else-If Ladder
  • The Switch Statement
  • The Nested Switch Statement

The If Statement


**The general form of the If statement with block statement is:


if ( expression)
{
statement_sequence;
}


**In an if statement, if the expression evaluates to TRUE (1), the statement or the block of statements that forms the target of the if statement will be executed. Otherwise, the program will ignore the statement or the block of statements.


The If-Else statement

**The general form of the if-else statement is:


If (expression)
statement_1;
else
statement_2;

  • Where:
    –If and else are reserved words
    –Expression is relational or Boolean expression that evaluates to a TRUE (1) or False (0) value.
    –Statement_1 and statement_2 may either be a single C statement or a block of c statements.


** The general form of the if-else statement with block of statement is:


If (expression)
{ statement_sequence;
}
else
{
statement_sequence;
}

  • If an if-else statement, if the expression is TRUE (1), the statement or block of statement after the if statement will be executed; otherwise, the statement or block of statement in the else statement will be executed.


Note: Only the code associated with the if or the code that is associated with the else executes, never both.


Nested-If statement

** One of the most confusing aspects of the if statement in any programming language is nested ifs. A nested if is an if statement that is the object of either an if or else. This is sometimes referred to as “an if within an if.” The reason that nested ifs are so confusing is that it can be difficult to know what else associates with what if.

** Fortunately, C provides a very simple rule for resolving this type of situation

** In C, the else is linked to the closest preceding if that does not already have an else statement associated with it.

  • Consider the following situations:
    –Situations 1.
    The else at number 3 is paired with the if in number 2 since it is the nearest if statement with the else statement.
    1. if…..
    2. if ……

    3. else
  • Consider the following situations:
    –Situations 2
    . The else in number 5 is paired with the if in number 1.
    1. if ….
    2. {
    3. if ….
    4. }
    5. else

** Note that there is a pair of braces found in number 2 and number 4.

** The pair of braces defined the scope of the if statement in number 1 starting from the { in number 2 and ends with } in number 4.

** Therefore, the else statement in number 5 cannot paired with the if statement in number 3 because the else statement is outside the scope of the first if statement.

** This makes the if statement in number 1 the nearest if statement to the else statement in number 5.


The if-else-if Ladder

** A common programming construct in C is the if-else- if ladder.


**The general form of the if-else-if ladder statement is:


if ( expression_1)
statement_1;
else if (expression_2)
statement_2;
else if (expression_3;
statement_3;
:
:
else
statement_else;

  • Where:
    –If and else are reserve words in C
    –Expression_1, expression_2 up to expression_n in relational or boolean expression that evaluates to a TRUE (1) or False (0) value.
    –Statement_1, statement_2 up to statement_else may either be a single C statement or a block of C statement.
  • In an if-else-if ladder statement, the expression are evaluated from the top downward.
  • As soon as a true condition is found, the statement associated with it is executed and the rest of the ladder will not be executed. If none of the condition is true, the final else is executed.
  • The final else acts as a defaults condition. If all other conditions are false, the last else statement is performed.
  • If the final else is not present, then no action takes place.


Note: The final else is optional, you may include this part if needed in the program or you may not include if not needed.


The switch statement

** The switch statement is a multiple-branch decision statement.

** The general form of the switch statement is:


switch (variable)
{
case constant1:
statement sequence_1;
break;
case constant2:
statement_sequence_2;
break;
:
:
default:
statement_sequence_default;
}

** In a switch statement, a variable is successively tested against a list or integer or character constants.

** If a match is found, a statement or block of statement is executed.

** The default part of the switch is executed if no matches are found.

** According to Herbert Schildt (1992), there are three important things to know about switch statements:
–1. The switch differs from if statements in such a way that switch can only test fro equality whereas if can evaluate a relational or logical expression.

–2. No two case constants in the same switch can have identical values. Of course, a switch statement enclosed by an outer switch may have case constant that are the same.


–3. If character constants are used in the switch, they are automatically converted to their integer values.


Note: The break statement is used to terminate the statement associated with each case constant. It is a C keyword which means that at that point of execution, you should jump to the end of the switch statement by the symbol }.


The Nested Switch Statement


** The general form of the nested switch statement is:


switch (variable)
{
case constant:{
switch (variable)
{
case constant1:
statement sequence_1;
break;
case constant2:
statement_sequence_2;
break;
}
break;
}
case constant2:
statement sequence;
break;
default:
statement sequence;
}











































No comments: