loops in c ++

Upload: mrsanaullahkhan

Post on 04-Jun-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 Loops in C ++

    1/26

  • 8/13/2019 Loops in C ++

    2/26

    do...while loopLike a while statement, except that it tests the condition at the end of the

    loop body

    nested loopsYou can use one or more loop inside any another while, for or do..while

    loop.

    Loop Control Statements:

    Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic

    objects that were created in that scope are destroyed.

    C++ supports the following control statements. Click the following links to check their detail.

    ControlStatement Description

    break statementTerminates the loopor switchstatement and transfers execution to the

    statement immediately following the loop or switch.

    continue statementCauses the loop to skip the remainder of its body and immediately retest

    its condition prior to reiterating.

    goto statementTransfers control to the labeled statement. Though it is not advised to use

    goto statement in your program.

    The Infinite Loop:A loop becomes infinite loop if a condition never becomes false. The for loop is traditionally used for this purpose.

    Since none of the three expressions that form the for loop are required, you can make an endless loop by leaving the

    conditional expression empty.

    #include

    usingnamespacestd;

    intmain ()

    {

    for(;;)

    {

    printf("This loop will run forever.\n");

    return!;

    When the conditional expression is absent, it is assumed to be true. You may have an initialization and increment

    expression, but C++ programmers more commonly use the for(;;) construct to signify an infinite loop.

    NOTE:You can terminate an infinite loop by pressing Ctrl + C keys.

    http://www.tutorialspoint.com/cplusplus/cpp_do_while_loop.htmhttp://www.tutorialspoint.com/cplusplus/cpp_nested_loops.htmhttp://www.tutorialspoint.com/cplusplus/cpp_break_statement.htmhttp://www.tutorialspoint.com/cplusplus/cpp_continue_statement.htmhttp://www.tutorialspoint.com/cplusplus/cpp_goto_statement.htmhttp://www.tutorialspoint.com/cplusplus/cpp_do_while_loop.htmhttp://www.tutorialspoint.com/cplusplus/cpp_nested_loops.htmhttp://www.tutorialspoint.com/cplusplus/cpp_break_statement.htmhttp://www.tutorialspoint.com/cplusplus/cpp_continue_statement.htmhttp://www.tutorialspoint.com/cplusplus/cpp_goto_statement.htm
  • 8/13/2019 Loops in C ++

    3/26

    C++ while loopAdvertisements

    Previous Page

    Next Page

    A whileloop statement repeatedly executes a target statement as long as a given condition is true.

    Syntax:

    The syntax of a while loop in C++ is:

    while(condition)

    {

    statement(s);

    Here, statement(s)may be a single statement or a block of statements. The conditionmay be any expression, and true

    is any non-zero value. The loop iterates while the condition is true.

    When the condition becomes false, program control passes to the line immediately following the loop.

    Flow Diagram:

    Here, key point of the whileloop is that the loop might not ever run. When the condition is tested and the result is

    false, the loop body will be skipped and the first statement after the while loop will be executed.

    http://www.tutorialspoint.com/cplusplus/cpp_loop_types.htmhttp://www.tutorialspoint.com/cplusplus/cpp_loop_types.htmhttp://www.tutorialspoint.com/cplusplus/cpp_loop_types.htmhttp://www.tutorialspoint.com/cplusplus/cpp_loop_types.htm
  • 8/13/2019 Loops in C ++

    4/26

    Example:#include

    usingnamespacestd;

    intmain ()

    {

    ocal varia$le declaration%

    inta &'!;

    while loop eecution

    while(a

  • 8/13/2019 Loops in C ++

    5/26

    Next, the conditionis evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop

    does not execute and flow of control jumps to the next statement just after the for loop.

    After the body of the for loop executes, the flow of control jumps back up to the incrementstatement. This

    statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon

    appears after the condition.

    The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body ofloop, then increment step, and then again condition). After the condition becomes false, the for loop terminates.

    Flow Diagram:

    Example:#include

    usingnamespacestd;

    intmain ()

    {

    for loop eecution

    for(inta &'!;a

  • 8/13/2019 Loops in C ++

    6/26

    return!;

    When the above code is compiled and executed, it produces the following result:

    value of a% '!

    value of a% ''

    value of a% '

    value of a% '+

    value of a% ',

    value of a% '-

    value of a% '

    value of a% '/

    value of a% '0

    value of a% '1

    C++ do...while loopAdvertisements

    Previous Page

    Next Page

    Unlike forand whileloops, which test the loop condition at the top of the loop, the do...whileloop checks its condition at

    the bottom of the loop.

    A do...whileloop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time.

    Syntax:

    The syntax of a do...while loop in C++ is:

    do

    {

    statement(s);

    while(condition );

    Notice that the conditional expression appears at the end of the loop, so the statement(s) in the loop execute once

    before the condition is tested.

    If the condition is true, the flow of control jumps back up to do, and the statement(s) in the loop execute again. This

    process repeats until the given condition becomes false.

    Flow Diagram:

    http://www.tutorialspoint.com/cplusplus/cpp_loop_types.htmhttp://www.tutorialspoint.com/cplusplus/cpp_loop_types.htmhttp://www.tutorialspoint.com/cplusplus/cpp_loop_types.htmhttp://www.tutorialspoint.com/cplusplus/cpp_loop_types.htm
  • 8/13/2019 Loops in C ++

    7/26

  • 8/13/2019 Loops in C ++

    8/26

    Loops in C++ with Examples

    What are Loops in C++:-Loop is the process of repetition of the certain steps until the given condition becomes false. They

    are always remains continue until the condition remains true. If the condition becomes false they will

    terminate.

    Types of Loops:-

    There are three types of loops in C++. These are while loop, Do while loop and For loop.

    While loop in C++:-

    It allows the program to repeat the collection of the statements enclosed in a body of a loop until the

    given condition becomes false.

    Syntax of a While Loop:-

    hile !condition"

    #

    Collection of $tatements !the loops body"

    %

    Example of a While Loop:-

  • 8/13/2019 Loops in C ++

    9/26

    $ource&'y (lu)e !*wn wor)" CC-, via i)imedia Commons

    1

    23

    45

    6

    7

    89

    10

    1112

    131415

    16

    17

    1819

    20

    #include

    #includemain()

    {

    inta2$;couta;

    cout

  • 8/13/2019 Loops in C ++

    10/26

    21

    22

    23

    Explanation:-

    The above program will ta)e the two values from the user. first value will be assigned to /a/ andsecond value will be assigned to /b/. Then the while loop will come into the action and will chec) the

    condition if the condition is true then it will print 0a1, after printing 0a1 it will increment the value of 0a1

    and the control returned to it. Then it will terminate only if the given condition becomes false

    otherwise it will continue. The loop which has no terminating condition is called infinite loop.

    Do While Loop in C++:-

    It is used when user already does not )now about the number of iterations. In this loop condition is

    written after the body of a loop. In this the body of a loop is e2ecuted at least once, even if the

    condition becomes false at the beginning.

    Syntax of a Do While Loop:-

    Do

    #

    Collection of statements

    %

    hile !condition"3

    Example of a Do While Loop:-

    http://royal52.hubpages.com/hub/Loops-in-C-with-Exampleshttp://royal52.hubpages.com/hub/Loops-in-C-with-Examples
  • 8/13/2019 Loops in C ++

    11/26

    1

    2

    34

    5

    67

    8

    910

    11

    12

    1314

    15

    16

    1718

    1920

    21

    22

    2324

    25

    2627

    #include

    #include

    main()

    {int52w;char62 ;

    do

    {

    cout5;

    coutw;

    cout

  • 8/13/2019 Loops in C ++

    12/26

    For Loop in C++:-

    For loop is )nown as the counter controlled loop. 6nli)e while and do while loop, for loop is given thespecified number of iterations, This loop will continue until the ma2imum number of iteration given by

    the programmer completes or in other words condition becomes false.

    Syntax of For Loop:-

    For !starting value3 given Condition3 Increment in a starting value"

    #

    Collection of statements !The body"

    %

    Input and Output y usin! For Loop:-

    The code written below provides is an easy e2ample of input and output of a For loop. It will give a

    good idea of how to use it.

  • 8/13/2019 Loops in C ++

    13/26

    The below written program will ta)e 7 inputs in array named ne"with the help of For loop. 5fter

    ta)ing the input, another For loop will came into action and print the numbers into the screen which

    was assigned to array ne"#

    12

    3

    45

    6

    78

    9

    10

    1112

    13

    1415

    16

    1718

    19

    2021

    22

    23

    24

    #include

    #include

    main()

    {

    int52w; intnew'-=;

    for(5&!;5

  • 8/13/2019 Loops in C ++

    14/26

    2

    3

    45

    6

    78

    9

    1011

    12

    13

    1415

    16

    17

    1819

    20

    #include

    main()

    {

    long@2A;cout@;

    coutA;

    cout

  • 8/13/2019 Loops in C ++

    15/26

    C++ for loops while loops

    In this C++ programming tutorial we will look at loops.

    There are circumstances were you want to do the same thing many times. For instance you want

    to

    print the same words ten times. You could type ten cout statements, but it is easier to use a loop,

    such as a for loop or a while loop. The only thing you ha!e to do is to setup a loop that

    e"ecute the same cout statement ten times.

    There are three basic types of loops which are#

    for loop

    while loop

    do while loop

    The for loop

    The for loop loops from one number to another number and increases by a specified !alue each

    time.

    The for loop uses the following structure#

    for (Btart value; end condition; increase value)

    statement(s);

    $ook at the e"ample below#

    #include

    using namespace std;

    int main()

    {

    int i;

    for (i & !; i < '!; i**)

    {

    cout

  • 8/13/2019 Loops in C ++

    16/26

    $ets look at the for loop from the e"ample# &e first start by setting the !ariable i to '. This is

    where we start to count. Then we say that the for loop must run if the counter i is smaller then

    ten. $ast we say that e!ery cycle i must be increased by one (i++).

    In the e"ample we used i++ which is the same as using i * i + . This is called incrementing. The

    instruction i++ adds to i. If you want to subtract from i you can use i. It is also possible to

    use ++i or -i. The difference is is that with ++i the one is added before the for loop tests if i

    '. &ith i++ the one is added after the test i '.

    The while loop

    The while loop can be used if you don/t know how many times a loop must run.

    0ere is an e"ample#

    #include

    using namespace std;

    int main()

    {

    int counter2 howmuch;

    cin >> howmuch;

    counter & !;

    while ( counter < howmuch)

    {

    counter**;

    cout

  • 8/13/2019 Loops in C ++

    17/26

    while (epression);

    2o something first and then test if we ha!e to continue. The result is that the loop always runs

    once. (3ecause the e"pression test comes afterward). Take a look at an e"ample#

    #include

    using namespace std;

    int main()

    {

    int counter2 howmuch;

    cin >> howmuch;

    counter & !;

    do

    {

    counter**;cout

  • 8/13/2019 Loops in C ++

    18/26

    In the e"ample abo!e, the while loop will run, as long i is smaller then twenty. In the while loop

    there is an if statement that states that if i e4uals ten the while loop must stop (break). The result

    is that only ten 0ello will be printed.

    &ith continue5 it is possible to skip the rest of the commands in the current loop and start from

    the top again. (the loop !ariable must still be incremented). Take a look at the e"ample below#

    #include

    using namespace std;

    int main()

    {

    int i;

    i & !;

    while ( i < ! )

    {i**;

    continue;

    cout

  • 8/13/2019 Loops in C ++

    19/26

    4). For Loop Example Program In C++Contents

    !. 1 Definition:

    ". 2 #ynta$:

    %. 3 E$ample &ro'ram

    (. 4 #ample )utput:

    #imple C++ E$ample &ro'rams

    Definition:

    *n C++ a for loop is a pro'rammin' lan'ua'e statement which allows coe to ,e repeate

    loop is

    classifie as an iteration statement.

    Syntax:

    for ( varia$le initiali6ation; condition; varia$le incrementassignment ) {

    Dode to eecute while the condition is true

    Example Program

    E ample 4rogram For for oop 3n D**

    little drops G thi:[email protected]

    Doded H:%TC3?7I7J77K L4 E

    #include

    #include

    http://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/for-loop-example-program-in-c#TOC-Definition:http://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/for-loop-example-program-in-c#TOC-Definition:http://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/for-loop-example-program-in-c#TOC-Definition:http://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/for-loop-example-program-in-c#TOC-Syntax:http://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/for-loop-example-program-in-c#TOC-Syntax:http://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/for-loop-example-program-in-c#TOC-Syntax:http://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/for-loop-example-program-in-c#TOC-Example-Programhttp://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/for-loop-example-program-in-c#TOC-Example-Programhttp://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/for-loop-example-program-in-c#TOC-Example-Programhttp://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/for-loop-example-program-in-c#TOC-Example-Programhttp://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/for-loop-example-program-in-c#TOC-Sample-Output:http://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/for-loop-example-program-in-c#TOC-Sample-Output:http://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/for-loop-example-program-in-c#TOC-Sample-Output:http://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/for-loop-example-program-in-c#TOC-Sample-Output:http://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/for-loop-example-program-in-c#TOC-Definition:http://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/for-loop-example-program-in-c#TOC-Syntax:http://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/for-loop-example-program-in-c#TOC-Example-Programhttp://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/for-loop-example-program-in-c#TOC-Sample-Output:
  • 8/13/2019 Loops in C ++

    20/26

    using namespace std;

    int main()

    {

    Maria$le 8eclaration

    int a;

    Iet 3nput Malue

    couta;

    for oop HlocA

    for (int counter & '; counter

  • 8/13/2019 Loops in C ++

    21/26

    E$ecute ! time

    E$ecute " time

    E$ecute % time

    E$ecute ( time

    E$ecute time

  • 8/13/2019 Loops in C ++

    22/26

    5). While Loop Example Program In

    C++Contents

    !. 1 Definition

    ". 2 #ynta$

    %. 3 E$ample &ro'ram

    (. 4 #ample )utput:

    #imple C++ E$ample &ro'rams

    Definition

    *n C++a while loop is a control flow statement that allows coe to ,e e$ecute repeately

    ,oolean

    conition. 0he while loop can ,e thou'ht of as a repeatin' if statement.

    Syntax

    while ( condition ) {

    Dode to eecute while the condition is true

    Example Program

    E ample 4rogram For Ohile oop 3n D**

    little drops G thi:[email protected]

    Doded H:%TC3?7I7J77K L4 E

    #include

    http://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/while-loop-example-program-in-c#TOC-Definitionhttp://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/while-loop-example-program-in-c#TOC-Definitionhttp://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/while-loop-example-program-in-c#TOC-Definitionhttp://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/while-loop-example-program-in-c#TOC-Syntaxhttp://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/while-loop-example-program-in-c#TOC-Syntaxhttp://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/while-loop-example-program-in-c#TOC-Syntaxhttp://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/while-loop-example-program-in-c#TOC-Example-Programhttp://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/while-loop-example-program-in-c#TOC-Example-Programhttp://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/while-loop-example-program-in-c#TOC-Example-Programhttp://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/while-loop-example-program-in-c#TOC-Example-Programhttp://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/while-loop-example-program-in-c#TOC-Sample-Output:http://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/while-loop-example-program-in-c#TOC-Sample-Output:http://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/while-loop-example-program-in-c#TOC-Sample-Output:http://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/while-loop-example-program-in-c#TOC-Sample-Output:http://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/while-loop-example-program-in-c#TOC-Definitionhttp://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/while-loop-example-program-in-c#TOC-Syntaxhttp://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/while-loop-example-program-in-c#TOC-Example-Programhttp://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/while-loop-example-program-in-c#TOC-Sample-Output:
  • 8/13/2019 Loops in C ++

    23/26

    #include

    using namespace std;

    int main()

    {

    Maria$le 8eclaration

    int a;

    Iet 3nput Malue

    couta;

    int counter & ';

    while oop HlocA

    while (counter

  • 8/13/2019 Loops in C ++

    24/26

    Sample Otpt:

    Enter the Num,er :(

    E$ecute 1hile ! time

    E$ecute 1hile " time

    E$ecute 1hile % time

    E$ecute 1hile ( time

    ). !o While Loop Example Program In

    C++Contents

    !. 1 Definition

    ". 2 #ynta$

    %. 3 E$ample &ro'ram

    (. 4 #ample )utput

    #imple C++ E$ample &ro'rams

    Definition

    *n C++ a o while loop sometimes 2ust calle a o loop is a control flow statement that a

    e$ecute

    repeately ,ase on a 'ien 3oolean conition.

    Syntaxdo {

    Dode to eecute while the condition is true

    while ( condition );

    http://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/do-while-loop-example-program-in-c#TOC-Definitionhttp://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/do-while-loop-example-program-in-c#TOC-Definitionhttp://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/do-while-loop-example-program-in-c#TOC-Definitionhttp://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/do-while-loop-example-program-in-c#TOC-Syntaxhttp://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/do-while-loop-example-program-in-c#TOC-Syntaxhttp://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/do-while-loop-example-program-in-c#TOC-Syntaxhttp://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/do-while-loop-example-program-in-c#TOC-Example-Programhttp://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/do-while-loop-example-program-in-c#TOC-Example-Programhttp://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/do-while-loop-example-program-in-c#TOC-Example-Programhttp://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/do-while-loop-example-program-in-c#TOC-Example-Programhttp://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/do-while-loop-example-program-in-c#TOC-Sample-Outputhttp://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/do-while-loop-example-program-in-c#TOC-Sample-Outputhttp://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/do-while-loop-example-program-in-c#TOC-Sample-Outputhttp://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/do-while-loop-example-program-in-c#TOC-Sample-Outputhttp://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/do-while-loop-example-program-in-c#TOC-Definitionhttp://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/do-while-loop-example-program-in-c#TOC-Syntaxhttp://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/do-while-loop-example-program-in-c#TOC-Example-Programhttp://www.cpp.thiyagaraaj.com/c-programs/c-basic-example-programs/do-while-loop-example-program-in-c#TOC-Sample-Output
  • 8/13/2019 Loops in C ++

    25/26

    Example Program

    E ample 4rogram For 8o..Ohile 3n D**

    little drops G thi:[email protected]

    Doded H:%TC3?7I7J77K L4 E

    #include

    #include

    using namespace std;

    int main()

    {

    Maria$le 8eclaration

    int a;

    Iet 3nput Malue

    couta;

    int counter & ';

    8o while oop HlocA

    do

    {

    cout

  • 8/13/2019 Loops in C ++

    26/26

    counter**;

    while (counter