P6 Control Structures

download P6 Control Structures

of 30

Transcript of P6 Control Structures

  • 8/3/2019 P6 Control Structures

    1/30

    2/15/20122/15/2012 11

  • 8/3/2019 P6 Control Structures

    2/30

    2/15/20122/15/2012 22

    Control StructuresControl Structures

    Used to control the execution of various

    statements in the program.

    By default, instructions in the program areexecuted sequentially.

    It is classified into three categories.Decision Making

    Loops

    Case

  • 8/3/2019 P6 Control Structures

    3/30

    2/15/20122/15/2012 33

    Decision MakingDecision Making

    When we want a set of instruction to be

    executed at one situation, and entirely

    different set of instruction to be executedin another situation.

    Implemented using the following:The ifstatement

    The if-else statement

    The conditional operators

  • 8/3/2019 P6 Control Structures

    4/30

    2/15/20122/15/2012 44

    ififstatementstatement

    General form ofifstatement is:

    if ( this condition is true )

    {

    statement 1;

    statement 2;

    .

    .statement n;

    }

  • 8/3/2019 P6 Control Structures

    5/30

    2/15/20122/15/2012 55

    IfIfstatementstatement--exampleexample

    main( )

    {

    int num ;

    printf ( "Enter a number less than 10 " ) ;

    scanf ( "%d", &num ) ;

    if ( num

  • 8/3/2019 P6 Control Structures

    6/30

    2/15/20122/15/2012 66

    IfIf--elseelse statementstatement

    Ifstatement does nothing when the expressionevaluates to false.

    General form ofif-else statement:

    if(expression)

    {

    statements;

    }

    else

    {

    statements;

    }

  • 8/3/2019 P6 Control Structures

    7/30

    2/15/20122/15/2012 77

    IfIf--elseelse statement examplestatement example

    if(A>B)

    {

    printf(A is big);

    }else

    {

    printf(B is big)

    }

    If-else statements can also be nested

  • 8/3/2019 P6 Control Structures

    8/30

    2/15/20122/15/2012 88

    Logical operatorsLogical operators

    C allows use of logical operators.

    AND &&

    OR ||NOT !

  • 8/3/2019 P6 Control Structures

    9/30

    2/15/20122/15/2012 99

    Logical OperatorsLogical Operatorsmain( )

    {

    int m1, m2, m3, m4, m5, per ;

    printf ( "Enter marks in five subjects " ) ;

    scanf ( "%d %d %d %d %d", &m1, &m2, &m3, &m4, &m5 ) ;

    per = ( m1 + m2 + m3 + m4 + m5 ) / 5 ;

    if ( per >= 60 )

    printf ( "First division" ) ;

    if ( ( per >= 50 ) && ( per < 60 ) )

    printf ( "Second division" ) ;

    if ( ( per >= 40 ) && ( per < 50 ) )

    printf ( "Third division" ) ;if ( per < 40 )

    printf ( "Fail" ) ;

    }

  • 8/3/2019 P6 Control Structures

    10/30

    2/15/20122/15/2012 1010

    Logical OperatorsLogical Operators

    ! Operator Reverses the result of the

    operator it operates on.

    For example, if the expression evaluatesto non-zero, then by applying ! Operator

    we can make it it zero and vice-versa.

    Along with the hierarchy of other

    operators, logical operators also has a

    priority which is mentioned in the table.

  • 8/3/2019 P6 Control Structures

    11/30

    2/15/20122/15/2012 1111

    Hierarchy of logical operatorsHierarchy of logical operators

    Note: In C assignment operator = is, different from relational operator ==.

  • 8/3/2019 P6 Control Structures

    12/30

    2/15/20122/15/2012 1212

    Conditional or ternary operatorConditional or ternary operator

    Conditional operators ? And : are called

    ternary operators,since they take three

    arguments.

    General form of ternary operator is:

    Expression1?expression2:expression3

    If expression 1 is true,value returned isexpression2,else expression 3 will be

    returned.

  • 8/3/2019 P6 Control Structures

    13/30

    2/15/20122/15/2012 1313

    Ternary operatorsTernary operators--ExampleExample

    int x, y ;

    scanf ( "%d", &x ) ;

    y = ( x > 5 ? 3 : 4 ) ;

    Equivalent if statement will beif ( x > 5 )

    y = 3 ;

    else

    y=4;

  • 8/3/2019 P6 Control Structures

    14/30

    2/15/20122/15/2012 1414

    LoopsLoops

    Used to perform set of operations

    repeatedly.

    Three statements are used to achievelooping

    While statement

    For statement

    Do-while statement

  • 8/3/2019 P6 Control Structures

    15/30

    2/15/20122/15/2012 1515

    While statementWhile statement

    General form of while statement

    initialize loop counter

    while(test loop counter for a condition)

    {statement 1;

    statement 2;

    ..

    ..

    statement n;

    increment or decrement loop counter;

    }

  • 8/3/2019 P6 Control Structures

    16/30

    2/15/20122/15/2012 1616

    While statement exampleWhile statement example

    main()

    {

    int i=1;

    while(i

  • 8/3/2019 P6 Control Structures

    17/30

    2/15/20122/15/2012 1717

    Increment and Decrement operatorIncrement and Decrement operator

    ++ operator is used to increment the value by 1.

    i=i+1 => i++;

    -- operator is used to decrement the value by 1.

    i=i-1 => i--;

    Similarly, compound assignment += operator can

    be used.

    j+=10 => j=j+10

  • 8/3/2019 P6 Control Structures

    18/30

    2/15/20122/15/2012 1818

    Increment operatorIncrement operator

    Consider the statement:While(i++

  • 8/3/2019 P6 Control Structures

    19/30

    2/15/20122/15/2012 1919

    forforlooploop

    This is one of the commonly used looping

    statement in C.

    General form of for statement:for(initialize counter; test counter; increment counter)

    {

    Statements;

    }

  • 8/3/2019 P6 Control Structures

    20/30

    2/15/20122/15/2012 2020

    forforlooploop

    It allows us to specify three things in a

    single line.Setting a loop counter to an initial value

    Testing a loop counter to determine whether it has

    reached number of repetitions

    Increase the loop counter each time when the

    program segment within the loop has been

    executed

  • 8/3/2019 P6 Control Structures

    21/30

    2/15/20122/15/2012 2121

    forforlooploop

    main( )

    {

    int i ;

    for ( i = 1 ; i

  • 8/3/2019 P6 Control Structures

    22/30

    2/15/20122/15/2012 2222

    dodo--whilewhile looploop

    When you are not sure about how many times

    the loops has to be executed.

    General form of do-while statement is:

    do

    {

    statements;

    }while(condition)Here statements inside the loop is getting

    executed at least once.

  • 8/3/2019 P6 Control Structures

    23/30

    2/15/20122/15/2012 2323

    dodo--whilewhile looploop--exampleexample

    main( )

    {

    char another ;

    int num ; do

    {

    printf ( "Enter a number " ) ;

    scanf ( "%d", &num ) ;

    printf ( "square of %d is %d", num, num * num ) ;

    printf ( "\n Want to enter another number y /n " ) ;scanf ( " %c", &another ) ;

    } while ( another == 'y' ) ;

    }

  • 8/3/2019 P6 Control Structures

    24/30

    2/15/20122/15/2012 2424

    Break statementBreak statement

    Used to jump out of the loop instantly.

    Control is passed to the first statement

    after the loop. This statement is used with for and while

    loopswhile ( j++

  • 8/3/2019 P6 Control Structures

    25/30

    2/15/20122/15/2012 2525

    Continue statementContinue statement

    To take the control to the beginning of the

    loop and bypassing the statements inside

    the loop.for ( i = 1 ; i

  • 8/3/2019 P6 Control Structures

    26/30

    2/15/20122/15/2012 2626

    goto statementgoto statement

    Used to transfer the control to anywhere in

    the program.

    General form of goto isgoto label;

    ..

    ..label:

  • 8/3/2019 P6 Control Structures

    27/30

    2/15/20122/15/2012 2727

    Switch statementSwitch statement

    This allows us to make a decision from thenumber of choices.

    It is also called switch-case default.

    General form of switch statement isswitch ( integer expression )

    {

    case constant 1 :

    do this ;

    case constant 2 :

    do this ;case constant 3 :

    do this ;

    default :

    do this ;

    }

  • 8/3/2019 P6 Control Structures

    28/30

    2/15/20122/15/2012 2828

    Switch statementSwitch statement--exampleexample

    main( )

    {

    int i = 2 ;

    switch ( i )

    {

    case 1 :

    printf ( "I am in case 1 \n" ) ;case 2 :

    printf ( "I am in case 2 \n" ) ;

    case 3 :

    printf ( "I am in case 3 \n" ) ;

    default :

    printf ( "I am in default \n" ) ;

    }}

  • 8/3/2019 P6 Control Structures

    29/30

    I request Electronics and communication

    ENGINEERING students to visit my blogfor

    more

    abhishek1ek.blogspot.com

    awhengineering.blogspot.com2/15/20122/15/2012 2929

  • 8/3/2019 P6 Control Structures

    30/30

    2/15/20122/15/2012 3030