+ All Categories
Home > Documents > Decision MakingII

Decision MakingII

Date post: 03-Apr-2018
Category:
Upload: parth-r-shah
View: 213 times
Download: 0 times
Share this document with a friend

of 27

Transcript
  • 7/29/2019 Decision MakingII

    1/27

    3/22/2013 CS&E Dept

    Decision making & Looping

    A sequence of statements are executed until sometermination conditions satisfied.

    Program loop: body of loop.

    control statement -> tests certain conditions & then

    directs repeated execution of statements within the bodyof loop.

    Two types: Based on position of control statement.

    1) Entry controlled loop: control are tested

    before the start of the loop. If false body will not beexecuted.

    2) Exit controlled loop: test is performed at theend of the body. i.e body of loop executed at least once.

  • 7/29/2019 Decision MakingII

    2/27

    3/22/2013 CS&E Dept

    Entry Controlled & Exit controlled loop

    True

    Entry

    TestCondition

    Body ofThe loop

    False

    TestCondition

    True

    Body of

    The loop

    False

    Exit

  • 7/29/2019 Decision MakingII

    3/27

    3/22/2013 CS&E Dept

    While statement

    Basic format:

    while (test condition)

    {

    body of the loop

    }

    * Entry controlled loop statement.Test condition evaluated & if it is true, then body of the loop

    is executed. After execution, the test condition is again

    evaluated & if it is true, the body is executed again. This

    Procedure is repeated until test condition becomes false, & control

    transferred out of the loop. i.e body of loop may notexecuted if the condition is false at the very first attempt.

  • 7/29/2019 Decision MakingII

    4/27

    3/22/2013 CS&E Dept

    Example

    #include

    void main()

    {

    int counter;counter =0;

    while (counter < 5)

    {cout

  • 7/29/2019 Decision MakingII

    5/27

    3/22/2013 CS&E Dept

    Example

    #include

    void main()

    {

    int counter;

    int sum;

    sum=0; //initialize sumcounter=0;

    while (counter

  • 7/29/2019 Decision MakingII

    6/27

    3/22/2013 CS&E Dept

    The do statementGeneral form:

    do

    {body of the loop

    }

    while (test condition);

    Exit controlled loop.After do statement, program executes the body of the

    Loop.

    At the end of the loop, the test condition in the while is

    evaluated. If it is true, body of the loop is executed once

    again & this process continues as long as the condition istrue. When condition becomes false, the loop will be

    terminated.

    Body of the loop executed at least once.

    do while loop can be nested.

  • 7/29/2019 Decision MakingII

    7/27

    3/22/2013 CS&E Dept

    Example#include

    void main(){

    int counter;

    int sum;

    sum=0; //initialize sum

    counter=0;do

    {

    sum=sum+counter;

    counter = counter+1;

    } while (counter

  • 7/29/2019 Decision MakingII

    8/27

    3/22/2013 CS&E Dept

    The for statement

    The general form:

    for (intilization; test condition; increment)

    {Body of the loop

    }

    Entry controlled loop.Next statement;

  • 7/29/2019 Decision MakingII

    9/27

    3/22/2013 CS&E Dept

    Explanation

    1. The expression initial_value1 is evaluated, usually anassignment statement that sets a variable to aparticular value.

    2.The expression condition is evaluated. It is typically a

    relational expression.3. If condition evaluates as false (zero), the for statement

    terminates and execution passes to the first statementfollowing the for statement that is the next_statement.

    4. If condition evaluates as true (non zero), the subsequentC++ statements are executed. (i.e body of the loop)

    5. The expression increment is executed, and executionreturns to step no. 2.

  • 7/29/2019 Decision MakingII

    10/27

    3/22/2013 CS&E Dept

    Example

    #include

    void main()

    {

    int counter;

    int sum;

    sum=0; //initialize sum

    for(counter=0;counter

  • 7/29/2019 Decision MakingII

    11/27

    3/22/2013 CS&E Dept

    Additional features of for loop

    1. More than one variable can be initialized.

    Example: for (p=1,n=0;n

  • 7/29/2019 Decision MakingII

    12/27

    3/22/2013 CS&E Dept

    Continued4. It is also permissible to use expressions in the

    initialization & increment sections.

    Eg: for (x=(m+n)/2; x>0; x=x/2)5. In for loop one or more sections can be omitted.

    Eg: i) m=5;

    for(;m!=50;)

    { cout

  • 7/29/2019 Decision MakingII

    13/27

    3/22/2013 CS&E Dept

    Nesting of for loop

    One for statement within another for statement.

    for (i=1;i

  • 7/29/2019 Decision MakingII

    14/27

    3/22/2013 CS&E Dept

    Jumping out of a loop

    An early exit from a loop can be accomplished

    by using the break statement.

    When the break statement is encountered inside

    a loop, the loop is immediately exited & theprogram continues with the statement

    immediately following the loop.

    When the loops are nested , the break would

    only exit from the loop containing it.

    i.E , the break will exit only a single loop.

  • 7/29/2019 Decision MakingII

    15/27

    3/22/2013 CS&E Dept

    Exiting a loop with break

    statement

    while (.)

    {.

    If(condition)

    break;

    .

    }

    ..

    Exit

    From

    loop

    do (.)

    {.

    If(condition)

    break;

    .

    } while()

    ..

    Exit

    From

    loop

  • 7/29/2019 Decision MakingII

    16/27

    3/22/2013 CS&E Dept

    for

    {.

    If(condition)

    break;

    .

    }

    ..

    ExitFrom

    loop

    for (.)

    {.

    for(..)

    {

    If(condition)

    break;

    }

    ..

    }

    ..

    ExitFrom

    inner

    loop

  • 7/29/2019 Decision MakingII

    17/27

    3/22/2013 CS&E Dept

    Skipping a part of loop

    while (.)

    {.

    If(condition)

    continue;

    .

    }

    ..

    do (.)

    {.

    If(condition)

    continue;

    .

    } while()

    ..

    Skip a part of the body of the loop under certain conditions.

    Using continue statement. As the name implies, causes the loop to be

    continued with next iteration, after skipping rest of the body of the

    loop.

  • 7/29/2019 Decision MakingII

    18/27

    3/22/2013 CS&E Dept

    for (.)

    {.

    If(condition)

    continue;

    .

    }

    ..

  • 7/29/2019 Decision MakingII

    19/27

    3/22/2013 CS&E Dept

    #include

    #include

    #include

    //Sine of an angle upto given accuracy i.e |term|>accr;

    angle=angle*pi/180;term=angle;sum=angle;

    while(fabs(term)>accr)

    {term=-term*angle*angle/((2*i)*(2*i+1));

    sum=sum+term;

    i++;

    }cout

  • 7/29/2019 Decision MakingII

    20/27

    3/22/2013 CS&E Dept

    #include

    #include

    #include

    //Cosine of a number upto given accuracy

    void main(){

    int i=0;const double pi=22.0/7;

    float sum,angle,term,accr;clrscr();

    coutangle>>accr;

    angle=angle*pi/180; term=1;sum=1;

    while(fabs(term)>accr)

    {

    term=-term*angle*angle/((2*i+1)*(2*i+2));

    sum=sum+term;

    i++;}

    cout

  • 7/29/2019 Decision MakingII

    21/27

    3/22/2013 CS&E Dept

    #include

    #include

    #define true 1

    #define false 0

    //To check whetherthe number is prime or not

    void main(){

    int n,i,prime;clrscr();

    coutn;

    if(n==1)

    cout

  • 7/29/2019 Decision MakingII

    22/27

    3/22/2013 CS&E Dept

    #include

    #include

    //Generating prime numbers below the given limit

    void main()

    {

    int n,i,prime,j;clrscr();coutn;

    for(i=1;i

  • 7/29/2019 Decision MakingII

    23/27

    3/22/2013 CS&E Dept

    #include

    #include

    #define true 1

    #define false 0

    // Generate prime Fibonacci numbersvoid main()

    {

    int j,prime,num,i,f1,f2,fib;

    clrscr();

    coutnum;

    cout

  • 7/29/2019 Decision MakingII

    24/27

    3/22/2013 CS&E Dept

    {

    prime = true;

    if(f1%j==0)

    {

    prime = false;

    break;}

    }

    if(prime==true)

    cout

  • 7/29/2019 Decision MakingII

    25/27

    3/22/2013 CS&E Dept

    #include

    #include

    #define true 1#define false 0

    //Generating prime numbers below the given limit

    void main(){

    int n,i,prime,j;

    clrscr();coutn;

  • 7/29/2019 Decision MakingII

    26/27

    3/22/2013 CS&E Dept

    for(i=1;i

  • 7/29/2019 Decision MakingII

    27/27

    3/22/2013 CS&E Dept

    #include

    #include

    //Find average of m numbers until negative number is typed using

    type castingvoid main()

    {

    int m;

    float sum,x,avg;

    clrscr();cout


Recommended