examples 4 loop in turbo c - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

examples 4 loop in turbo c

shahdshahd Posts: 1Member
hi every one :)

i'de like to ask any one who can help me to find the way to make a loop pragramme by using turbo c ..
and i need also at least one example 4 that so i can understand better;)
thanx every body 4 ur supporting

shahd

Comments

  • DonotaloDonotalo Posts: 715Member
    There are three main kind of loop: the [red]for[/red] loop, [green]while[/green] loop and [blue]do while[/blue] loop. each one is discussed briefly here.

    [red]for[/red] loop:
    [code][italic]
    for (initialization; boolean checking; increament or decreament) {
    //Loop body
    }[/italic]
    [/code]
    all of the parts [italic]initialization, boolean checking and increament or decreament[/italic] are optional here. If u want to initialize something, put the code into initialization portion. boolean checking is an expression that returns true or false. when it is false, the loop breaks. until then it continues. in [red]for[/red] loop, the initialization portion will execute first. then it evaluates boolean checking part. if it returns true, the program goes through the body (block). after executing the last statement, it will evaluate increament or decreament portion. then it checks again the boolean checking part. if it returns true the whole process will continue again. here is an example of showing all of the array elements using [red]for[/red] loop:
    [code]
    for (i = 0; i < array_size; ++i) {
    printf("%d ", array[i]);
    }
    [/code]

    [green]while[/green] loop:
    [code][italic]
    while (boolean checking) {
    //Loop body
    }[/italic]
    [/code]
    the boolean checking portion works exactly the same way as that of the [red]for[/red] loop. however, u can't keep it empty. the boolean checking is not optional here. in while loop, the boolean checking part executes first. if it returns true, then the loop body executes. after executing the last statement, the program checks the boolean xpression again and if it returns true, the whole process will continue. here is the same example shown above using [green]while[/green] loop:
    [code]
    i = 0;
    while (i < array_size) {
    printf("%d ", array[i]);
    ++i;
    }
    [/code]

    [blue]do while[/blue] loop:
    [code][italic]
    do {
    //Loop body
    }
    while(boolean checking);[/italic]
    [/code]
    the [blue]do while[/blue] loop has an exception: it executes the loop body first and then checks the boolean checking. so, unlike [red]for[/red] and [green]while[/green] loop, [blue]do while[/blue] loop will execute the loop body at least once. here is the previously shown example using [blue]do while[/blue] loop:
    [code]
    i = 0;
    do {
    printf("%d ", array[i]);
    ++i;
    }
    while(i < array_size);
    [/code]
Sign In or Register to comment.