Friday, June 15, 2012

Handling Multiple Choices

    In many occasions we come across situations where we are required to choose one thing out of many. This can be handled by using nested if - else if - else if ... else structure.

C also provides a special control structure that allows us to handle such cases effectively, rather than using a series of if statements.

Case Control Structure

The control structure in C, which allows as to make a selection out of a number of choices is called a switch. It consists of three keywords switch - case - default.

Syntax : switch (<expression>) <statement>
                         case <constant expression>
                         default;


switch <expression>
{
case <constant expression>:
               do this;
case <constant expression>:
               do this;
default :
               do this;
}


switch Causes control to branch to one of a list of possible statements in the block defined by <statement>
           
case The list of possible branch points within <statement> is determined by preceding sub statements with     case <constant expression>; where <constant expression> must be an int and must be unique.


Once a value is computed for <expression>, the list of possible <constant expression> values determined from all case statements is searched for a match. If a match is found, execution continuous until a break statementis encountered or the end of <statement> is reached.

default If a switch is not found and "default : " statement prefix is found within <statement>, execution     continues at this point. Otherwise<statement> is skipped entirely.


The loop control structure

The programs so far we have discussed followed either a sequential or a decision control structure. In the sequential structure the execution of statements was carried out in a fixed order while in the selection structure the execution of statements was carried out depending on the outcome of a condition being over, with variations in the details each time. The structure that meets this need is the Repetitive Control Structure and its action is termed 'looping'.

Repetition

The versatility of a computer lies in its ability to perform a set of instructions repeatedly. This involves repeating some parts of a program, a specified number of times or until a particular condition is full-filled. This is achieved by using the 'Repititive' or loop control structure.


/* Day of week */
#include <stdio.h>
#include <conio.h>
int main()
{
            int day;
            printf(“Enter day of the week : ”);
            scanf(“%d”, &day);
            switch (day)
                 {
                        case 1:
                                printf(“Today is Sunday \n”);
                                break;
                        case 2:
                                printf(“Today is Monday \n”);
                                break;
                        case 3:
                                printf(“Today is Tuesday \n”);
                                break;
                        case 4:
                                printf(“Today is Wednesday \n”);
                                break;
                        case 5:
                                printf(“Today is Thursday \n”);
                                break;
                        case 6:
                                printf(“Today is Friday \n”);
                                break;
                        case 7:
                                printf(“Today is Saturday \n”);
                                break;
                        default :
                                printf(“Wrong entry! \n”);
                 }
}




Transferring Control of a Program Unconditionally


Syntax : goto <identifier>;
 keyword transfers the control of a program unconditionally, to the location of a local label specified by <identifier>

Wednesday, June 13, 2012

Controlling the FLOW of a Program

Controlling the FLOW of a Program

     The execution of statements in our programs discussed so far followed an order or sequence. The first statement at the top of the program is executed, then the next statement, then the statement after that and so on, until the last is executed. This is called sequential

Tuesday, June 12, 2012

Expressions, Operators and Punctuators-2

Punctuators

[] Brackets                 () Parentheses
{} Braces                    , Comma
; Semi-colon                : Colon

Expressions, Operators and Punctuators

    Variable names, function names, array names, constants, function calls, array references, structure references are all considered expressions. Applying a unary operator to one of these appropriate expressions or combining two or more expressions also leads to an expression. An expression enclosed within parentheses is also an expression.

Monday, June 11, 2012

Standard Inputs

scanf() Function

    scanf() function reads data from a standard input device into arguments specified by the format string.

Friday, June 8, 2012

Assignments

Once we decide the data types and variables to be used in a program we can assign values to them.

Thursday, June 7, 2012

Constants and Variables

Constants

        The C language is composed of seven basic types of code: Constants, Variables, Operators, Punctuations,Keywords, Functions and Labels – collectively known as Tokens.

Wednesday, June 6, 2012

The Formatted Output of a C Program

printf() function

   The printf() function is one of the most widely used functions in C. The purpose is to output its argument

Tuesday, June 5, 2012

Introduction to C

     C is a general purpose high-level language developed by Dennis Ritchie and Brain Kernighan at AT&T Bell Laboratories of USA in 1972. Originally it was designed to run on a PDP-11 under UNIX operating system. C was influenced by two related languages BCPL and B. Although it was originally intended to run under UNIX, there has been a great interest in running it under MS-DOS on IBM-PC.

What is programming


What is Programming?

      The roots of man-machine relationship runs back to thousands of years. Today, we manipulate various kinds of machines. Out of myriads of machines available for the human, the computer has become the foremost, due to the fact that it is programmable and versatile.