Control Statements in C programming

Embed Size (px)

Citation preview

  • 8/11/2019 Control Statements in C programming

    1/34

    CONTROL STATEMENTS

    Lakhbir Singh(Lect.IT)

    S.R.S.G.P.C.G. Ludhiana

  • 8/11/2019 Control Statements in C programming

    2/34

    Flow of Control

    Unless specified otherwise, the order ofstatement execution through a function is

    linear: one statement after another in

    sequence

    Some programming statements allow us to:

    decide whether or not to execute a particular

    statement execute a statement over and over, repetitively

    These decisions are based on boolean

    expressions(or conditions) that evaluate to

  • 8/11/2019 Control Statements in C programming

    3/34

    Conditional Statements A conditional statementlets us choose which statement

    will be executed next

    Therefore they are sometimes called selection statements

    Conditional statements give us the power to make basicdecisions

    The C conditional statements are the:

    if statement if-else statement

    switch statement

  • 8/11/2019 Control Statements in C programming

    4/34

    Logic of an if statement

    condition

    evaluated

    statement

    true

    false

  • 8/11/2019 Control Statements in C programming

    5/34

    The if Statement

    The if statementhas the following syntax:

    if ( condition)statement;

    ifis a Creserved word

    The conditionmust be a

    boolean expression. It must

    evaluate to either true or false.

    If the conditionis true the statementis executed.

    If it is false the statementis skipped.

  • 8/11/2019 Control Statements in C programming

    6/34

    Logic of an if-else statement

    condition

    evaluated

    statement1

    true false

    statement2

  • 8/11/2019 Control Statements in C programming

    7/34

    The if-else Statement

    An else clausecan be added to an ifstatement to make an if-else statement

    if ( condition)statement1;

    elsestatement2;

    If the conditionis true, statement1is executed;

    if the condition is false, statement2is executed

    One or the other will be executed, but not both

  • 8/11/2019 Control Statements in C programming

    8/34

    Boolean Expressions

    A condition often uses one of C's equality operators or relational

    operators, which all return boolean results:

    == equal to

    != not equal to greater than

    = greater than or equal to

    Note the difference between the equality operator (==) and the

    assignment operator (=)

  • 8/11/2019 Control Statements in C programming

    9/34

    Boolean Expressions in C

    C does not have a boolean data type.

    Therefore, C compares the values of variables and expressionsagainst 0 (zero) to determine if they are true or false.

    If the value is 0 then the result is implicitly assumed to befalse.

    If the value is different from 0 then the result is implicitlyassumed to be true.

    C++ and Java have boolean data types.

  • 8/11/2019 Control Statements in C programming

    10/34

    Block Statements

    Several statements can be grouped together

    into a block statement delimited by braces

    A block statement can be used wherever astatement is called for in the C syntax rulesif (total > MAX)

    {printf ("Error!!\n");errorCount++;

    }

  • 8/11/2019 Control Statements in C programming

    11/34

    Block Statements

    In an if-elsestatement, the ifportion,or the elseportion, or both, could be block

    statementsif (total > MAX){

    printf("Error!!");errorCount++;}else{

    printf ("Total: %d, total);

    current = total*2;}

  • 8/11/2019 Control Statements in C programming

    12/34

    Nested if Statements

    The statement executed as a result of an ifstatement or

    elseclause could be another ifstatement

    These are called nested if statements

    An else clause is matched to the last unmatched if(no

    matter what the indentation implies)

    Braces can be used to specify the ifstatement to which an

    else clause belongs

  • 8/11/2019 Control Statements in C programming

    13/34

    The switch Statement

    The switch statementprovides another way to

    decide which statement to execute next

    The switch statement evaluates an expression,then attempts to match the result to one of

    several possible cases

    Each case contains a value and a list of

    statements

    The flow of control transfers to statement 2004 Pearson Addison-Wesley. All rights reserved

  • 8/11/2019 Control Statements in C programming

    14/34

    The switch Statement

    Often a break statementis used as the last

    statement in each case's statement list

    A break statement causes control to transferto the end of the switch statement

    If a break statement is not used, the flow of

    control will continue into the next case

    Sometimes this may be appropriate, but often

    we want to execute only the statements

  • 8/11/2019 Control Statements in C programming

    15/34

    The switch Statement

    switch (option){

    case 'A':aCount++;

    break;case 'B':bCount++;break;

    case 'C':

    cCount++;break;default:

    otherCount++;break;

    }

    An example of a switch statement:

  • 8/11/2019 Control Statements in C programming

    16/34

  • 8/11/2019 Control Statements in C programming

    17/34

    The switch Statement

    The expression of a switchstatement must

    result in an integral type, meaning an integer

    (byte, short, int,) or a char

    It cannot be a floating point value (floator

    double)

    The implicit test condition in a switch

    statement is equality

    You cannot perform relational checks with a

  • 8/11/2019 Control Statements in C programming

    18/34

    The switch Statement

    The general syntax of a switchstatementis:

    switch ( expression){

    case value1:

    statement-list1case value2:statement-list2

    case value3:statement-list3

    case ...

    }

    switch

    and

    case

    are

    reserved

    words

    If expressionmatches value2

    control jumps

    to here

  • 8/11/2019 Control Statements in C programming

    19/34

    Repetition in Programs

    In most software, the statements in theprogram may need to repeat for many times.

    e.g., calculate the value of n!.

    If n= 10000, its not elegant to write the code as1*2*3**10000.

    Loopis a control structure that repeats a group

    of steps in a program. Loop bodystands for the repeated statements.

    There are three C loop control statements:

    while, for, and do-while. 5-19

  • 8/11/2019 Control Statements in C programming

    20/34

    Flow Diagram of Loop Choice Process

    Copyright 2004 Pearson Addison-Wesley.All rights reserved.

    5-20

    e.g., calculate the value of n!

    e.g., read the content in a file

  • 8/11/2019 Control Statements in C programming

    21/34

    Comparison of Loop Choices (1/2)

    Kind When to Use C Structure

    Counting loop We know how many loop

    repetitions will be needed

    in advance.

    while, for

    Sentinel-

    controlled loop

    Input of a list of data ended

    by a special value

    while, for

    Endfile-controlled loop

    Input of a list of data froma data file

    while, for

    5-21

  • 8/11/2019 Control Statements in C programming

    22/34

    Comparison of Loop Choices (2/2)

    Kind When to Use C Structure

    Input validation

    loop

    Repeated interactive input

    of a value until a desired

    value is entered.

    do-while

    General

    conditional

    loop

    Repeated processing of

    data until a desired

    condition is met.

    while, for

    5-22

  • 8/11/2019 Control Statements in C programming

    23/34

    The while Statement in C

    The syntax ofwhilestatement in C:while(loop repetition condition)

    statement

    Loop repetition conditionis the condition

    which controls the loop.

    The statementis repeated as long as the loop

    repetition condition is true.

    A loop is called an infinite loopif the loop

    repetition condition is always true.5-23

  • 8/11/2019 Control Statements in C programming

    24/34

    An Example of a whileLoop

    5-24

    Statement

    Loop repetition condition

    Loop control variableis the variable whose value controls

    loop repetition.

    In this example, count_empis the loop control variable.

  • 8/11/2019 Control Statements in C programming

    25/34

    Flowchart for a whileLoop

    5-25

    Loop repetition condition

    Statement

  • 8/11/2019 Control Statements in C programming

    26/34

    Compound Assignment Operators (1/2)

    The loop body usually consists of statements

    of the form: variable = variable op expression.

    e.g., count_emp= count_emp+ 1;

    C provides compound assignment operators

    which enable a more concise notation for this

    kind of statements.

    variable op = expressionis the same to

    variable = variable op expression.

  • 8/11/2019 Control Statements in C programming

    27/34

    Compound Assignment Operators (2/2)

    Simple AssignmentOperators

    Compound AssignmentOperators

    count_emp =

    count_emp + 1;

    count_emp += 1;

    time = time -1; time -= 1;

    product = product *

    item;

    product *= item;

    total = total /

    number;

    total /= number;

    n = n % (x+1); n %= x+1;

    5-27

  • 8/11/2019 Control Statements in C programming

    28/34

    The forStatement in C The syntax of

    forstatement in C:

    for(initialization expression;

    loop repetition condition;

    update expression)

    statement

    The initialization expressionset the initial value of the

    loop control variable.

    The loop repetition conditiontest the value of theloop control variable.

    The update expressionupdate the loop control

    variable. 5-28

  • 8/11/2019 Control Statements in C programming

    29/34

    An Example of the forLoop

    5-29

    Loop repetition condition

    Initialization Expression

    Update Expression

    count_empis set to 0 initially.

    count_empshould not exceed the value of number_emp.

    count_empis increased by one after each iteration.

  • 8/11/2019 Control Statements in C programming

    30/34

  • 8/11/2019 Control Statements in C programming

    31/34

    Comparison of Prefix and Postfix

    Increments

    Copyright 2004 Pearson Addison-Wesley.All rights reserved.

    5-31

    The value of the expression (that uses the ++/-- operators)

    depends on the position of the operator.

    The valueof j is

    increased

    The value

    of j is not

    increased

  • 8/11/2019 Control Statements in C programming

    32/34

    Nested Loops

    Nested loops consist of an outer loopwith oneor more inner loops.

    e.g.,

    for (i=1;i

  • 8/11/2019 Control Statements in C programming

    33/34

  • 8/11/2019 Control Statements in C programming

    34/34

    An Example of the do-whileLoop

    /* Find even number input */

    do{printf(Enter a value: );

    scanf(%d, &num);

    }while(num % 2 !=0)

    5-34

    This loop will repeat if the user

    inputs odd number.