czwartek, 14 czerwca 2012

Pętla do ... while

Składnia pętli do ... while


do
{
    //... tu powtarzany kod
} while( warunek_konczacy );


Przykłady


#include <iostream>
int main()
{
    int ile = 4;
    do
    {
        std::cout << "Napis" << std::endl;
        ile--;
    } while( ile > 0 );
    
    return 0;
}

_________________________________________________________________________________

#include <iostream>
int main()
{
    int ile;
    
    std::cout << "Ile razy wypisac napis? ";
    std::cin >> ile;
    if( ile > 0 )
    do
    {
        std::cout << "Napis" << std::endl;
        ile--;
    } while( ile > 0 );
    
    return 0;
}

_________________________________________________________________________________

#include <iostream>
int main()
{
    int ile;
    
    std::cout << "Ile razy wypisac napis? ";
    std::cin >> ile;
    int pozycja = 1;
    if( ile > 0 )
    do
    {
        std::cout << "Napis nr" << pozycja << std::endl;
        pozycja++;
        ile--;
    } while( ile > 0 );
    
    return 0;
}

_________________________________________________________________________________

#include <iostream>
int main()
{
    int liczba;
    do
    {
        std::cout << "Podaj liczbe 123 aby zakonczyc petle" << std::endl;
        std::cin >> liczba;
        std::cout << "Podales liczbe " << liczba << std::endl;
    } while( liczba != 123 );
    
    std::cout << "Koniec" << std::endl;
    return 0;
}


Brak komentarzy:

Prześlij komentarz