c-programming-class 2

Upload: jackharish

Post on 30-May-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 C-Programming-Class 2

    1/32

    1

    Session 02

    Control Statements & Storage

    Specifiers

  • 8/14/2019 C-Programming-Class 2

    2/32

    2

    Session Objectives

    To learn about different types of Control

    Statements

    To learn about different Storage ClassSpecifiers

  • 8/14/2019 C-Programming-Class 2

    3/32

    3

    Session Topics

    Decision Structures if statement, if-else statement,

    nested if statement

    The switch statement

    Repetition or Iteration structure - for statement,continue statement, nested loop, while loop

    Storage class

  • 8/14/2019 C-Programming-Class 2

    4/32

    4

    Decision making and Branching

    To change the order of execution based on certain conditionsor repeat a group a statements until certain specifiedconditions are met.

    Any expression can be used as a program statement by

    following the expression with a ; (semicolon).

    ANSI C has the following categories of statements Selection if, switch

    Iteration for, do, while

    Jump continue, break, goto, return Label case, default, (goto) label statement

    Expression valid expression

    Block { } (also called compound statements)

  • 8/14/2019 C-Programming-Class 2

    5/32

    5

    C Control Structure Decision

    { . . . }Compound Statements

    if - else switch - case

    break

    break

  • 8/14/2019 C-Programming-Class 2

    6/32

    6

    C Control Structure Looping

    The while loop keeps repeating an action until an associated test

    returns false. This is useful where the programmer does not know

    in advance how many times the loop will be traversed.

    The do while loops is similar, but the test occurs after the loop

    body is executed. This ensures that the loop body is run at least

    once.

    The for loop is frequently used, usually where the loop will be

    traversed a fixed number of times. It is very flexible, and novice

    programmers should take care not to abuse the power it offers.

  • 8/14/2019 C-Programming-Class 2

    7/32

    7

    C Control Structure Looping

    n - times

    while do-while for

  • 8/14/2019 C-Programming-Class 2

    8/32

    8

    The if else Statement

    Structure if (expression)

    statement_1

    else

    statement_2

    The elsepart is optional

    The expression is evaluated: ifexpression is TRUE(I.e. non zero)

    thenstatement_1. Ifexpression isFALSE(i.e. zero) then

    statement_1 is executed if present. For multiple ifs, the else

    goes with the closest ifwithout an else condition.

  • 8/14/2019 C-Programming-Class 2

    9/32

    9

    The if else Statement- Examples

    #include int

    main()

    {

    int b;

    printf("Enter a value:");

    scanf("%d", &b);

    if (b < 0)printf("The value is

    negative\n");

    else if (b == 0)

    printf("The value is

    zero\n");

    else

    printf("The value is

    positive\n");

    return 0;

    }

  • 8/14/2019 C-Programming-Class 2

    10/32

    10

    The switch-case Statement

    Multiple branch selection statement

    Tests the value of an expression against a list of integer or char

    constants

    When a match is found, then statement associated with that

    constant is executed.Structure switch (expression)

    {

    case I1: statements;

    case I2: statements;

    case I3: statements;

    case In: statements;

    default: statements; // optional

    }

  • 8/14/2019 C-Programming-Class 2

    11/32

    11

    The switch-case Statement contd

    Operation: the expression is evaluated; then execution continues

    at the statements following the case statement that matches the

    result or after the label default if there are no matches. If the

    defaultcase does not exist, then execution continues after the last

    case statement.

    Execution continues through remaining cases in the switch

    structure unless the breakinstruction is encountered. If a breais encountered, then execution continues after the presentswitch-

    case instance.

  • 8/14/2019 C-Programming-Class 2

    12/32

    12

    The while Statement

    Structure while(expression)statement;or

    while(expression){

    statement_1;

    statement_2;}

    The while StatementExample

    while (a < b)

    {

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

    a = a + 1;}

    * Operation: expression is evaluated and ifTRUEthenstatement(or

    statement_1 and statement_2) is executed. The evaluation andexecutions sequence is repeated until the expression evaluates to be

    FALSE. If the expression is initiallyFALSEthenstatementis not

    executed at all.

  • 8/14/2019 C-Programming-Class 2

    13/32

    13

    Understanding a while loop

    b=1;

    While(b

  • 8/14/2019 C-Programming-Class 2

    14/32

    14

    Understanding a while loop

    b=1;

    While(b

  • 8/14/2019 C-Programming-Class 2

    15/32

    15

    Understanding a while loop

    b=1;

    While(b

  • 8/14/2019 C-Programming-Class 2

    16/32

    16

    The do-while Statement

    Structure do

    {

    statement;

    } while(expression);

    Operation: Similar to the whilecontrol except that

    statement is executed before the expression is

    evaluated. This guarantees that statementis always

    executed at least one time even if expression is

    FALSE. do {printf("%d\n", a);

    a = a + 1;

    } while (a < b);

  • 8/14/2019 C-Programming-Class 2

    17/32

    17

    The for Statement

    Structure: for(expr1; expr2; expr3)

    {

    statement;}

    Operation: The for loop in C is simply a shorthand way of expressing a

    while statement:expr1;while(expr2){

    statement;

    expr3} The comma operator lets you separate several different statements in

    the initialization and increment sections of the for loop (but not in thetest section).

  • 8/14/2019 C-Programming-Class 2

    18/32

    18

    Auxiliary Control Statements Break Causes the innermost control structure to be exited

    immediately. Execution continues with the statement immediately

    following the control structure just exited.

    Continue Causes immediate execution of the next loop to begin.

    Goto - Rumour has it that the goto statement should be avoidedbecause the program flow will jump all over the place, generating

    what's known as SPAGHETTI CODE! No harm in using it in the

    most simplest of programs though, but don't make a habit of using

    it!! The goto keyword is followed by a label, which is basically some

    identifier placed elsewhere in the program - like a hyperlink the

    points to a place on the same web page.

  • 8/14/2019 C-Programming-Class 2

    19/32

    19

    Looping: A Real Example

    Let's say that you would like to create a program that prints a

    Fahrenheit-to-Celsius conversion table. This is easily

    accomplished with a for loop or a while loop:

    #include

    int main(){

    int a;a = 0;while (a

  • 8/14/2019 C-Programming-Class 2

    20/32

    20

    C Errors to Avoid

    Putting = when you mean == in an if or while statement

    Forgetting to increment the counter inside the while loop - If you

    forget to increment the counter, you get an infinite loop (the loop never

    ends). Accidentally putting a ; at the end of a for loop or if statement so that

    the statement has no effect - For example:

    for (x=1; x

  • 8/14/2019 C-Programming-Class 2

    21/32

    21

    Storage Class Specifiers

    C has four kinds of storage classes Automatic

    Register

    Static

    External

    Storage class is used to denote to the compiler where memory is to beallocated for variables

    Storage class tells, what will be the initial value of the variable, if the

    initial value is not specifically assigned.(i.e the default initial value).

    Storage class informs the compiler thescope of the variable.Scope refers

    to which functions the value of the variable would be available. Storage class informs the compiler the life of the variable. Life refers to

    how long would the variable exist.

  • 8/14/2019 C-Programming-Class 2

    22/32

    22

    Automatic Storage Class

    All variables declared within a function are called localvariables, by

    default belong to the automatic storage class.

    Till the control remains within

    the block in which it is

    defined

    Life

    Local to the block in

    which the variable is

    defined

    Scope

    Garbage ValueDefault Initial Value

    MemoryStorage

  • 8/14/2019 C-Programming-Class 2

    23/32

  • 8/14/2019 C-Programming-Class 2

    24/32

    24

    Example for Automatic Storage Class

    Main(){

    auto int j=1;

    {

    auto int j=2;

    {

    auto int j=3;

    printf(%d\n,j);

    }

    printf(%d\n,j);

    }

    printf(%d\n,j);

    }

    Output:

    3

    2

    1

  • 8/14/2019 C-Programming-Class 2

    25/32

  • 8/14/2019 C-Programming-Class 2

    26/32

    26

    Example for Register Storage Class

    main()

    {

    register int i;

    for(i=1;i

  • 8/14/2019 C-Programming-Class 2

    27/32

  • 8/14/2019 C-Programming-Class 2

    28/32

    28

    Example for Static Storage Class

    main()

    {

    function();

    function();

    function();

    }function()

    {

    static int i=1;

    printf(%d\n,i);

    i=i+1;

    }

    Output

    1

    2

    3

    main()

    {

    function();

    function();

    function();

    }

    function()

    {

    auto int i=1;

    printf(%d\n,i);

    i=i+1;

    }

    Output

    1

    1

    1

  • 8/14/2019 C-Programming-Class 2

    29/32

    29

    External Storage Class

    Variables declared outside functions have externalstorage class

    As long as the programs

    execution doesnt come to an

    end

    Life

    GlobalScope

    ZeroDefault Initial Value

    MemoryStorage

  • 8/14/2019 C-Programming-Class 2

    30/32

    30

    Example for External Storage Class

    int i;

    main()

    {

    printf(i=,i);

    increment();

    increment();

    decrement();

    decrement();}

    increment()

    {

    i=i+1;

    printf(On incrementingi = %d\n,i);

    }

    decrement()

    {

    i=i-1;

    printf(On incrementing

    i=%d\n,i);

    }

    Output:

    i=0On incrementing i=1

    On incrementing i=2

    On decrementing i=1

    On decrementing i=0

  • 8/14/2019 C-Programming-Class 2

    31/32

    31

    Summary The different decision structure are if statement, if else statement,

    multiple choice else if statement. The while loop keeps repeating an action until an associated test returns

    false.

    The do while loops is similar, but the test occurs after the loop body isexecuted.

    The for loop is frequently used, usually where the loop will be traverseda fixed number of times.

    Storage class is used to denote to the compiler where memory is to beallocated for variables

    C has four kinds of storage classes

    Automatic

    Register

    Static

    External

  • 8/14/2019 C-Programming-Class 2

    32/32