Piece of Information - The Looping Statement
Main | Registration | Login | RSSSaturday, 2016-12-10, 11:24 PM

PiEcE oF InFoRmAtIoNs

Site menu
Search
Statistics

Total online: 1
Guests: 1
Users: 0

The Looping Statement

The looping statement is a program instruction that repeats some state-ment or sequence of statements in a specified number of times. Looping statement allows a set of instructions to be performed all over again until a certain condition is reached, met, or proven and tested as false.

The three looping statements of C Programming Language are:
  • for loop
  • while loop
  • do while loop

The syntax of the for loop statement is:

for (init; condition; inc/ dec)
{
statement(s)

The three parts of the for loop are the initialization (init) part, the condition part, and the increment (inc) or decrement (dec) part. The sample of incrementation is i++ or ++1, while the decrementation formula is --i or i--. 


The syntax of the while loop statement is: 

initialization;
while(condition) 
{
statements;
inc/dec;


The syntax of the do-while loop statement: 

initialization;
do
{
statements;
inc/dec; 
} while(condition); 


Incrementation, Decrementation, and Accumulator Formulas

These three formulas are oftenly used in looping statements operation:

Incrementation Formula:

Examples:
i++ or ++i
++n or n++

Decrementation Formula

Examples:
i-- or --i
--n or n—

Accumulator Formula:

Examples:
acc=acc+n
sum=sum+n
total=total+n 

The variable initialization (init) specifies the first value where the loop starts. The conditional part specifies conditions to be evaluated or tested. If the condition is false, then no action is taken and the program pointer proceeds to the next statement after the loop. If the conditional condition is evaluated to be true, then all the statements within the body of the loop are executed. This looping process (iteration) is repeated as long as the conditional expression remains true. After each iteration, the incrementation/ decrementation (inc/ dec) part is executed, the conditional expression is re-evaluated. In a simple for loop statement, the initialization part is executed only once. This happens during the first time the program pointer points to the loop. But in nested loop, the inner loop's initialization part could be re-executed many times depending on the condition set at the counter loop. This will happen every time the program pointer re-evaluates the condition of the outer loop to T (true).

For the sake of simplicity and easy understanding about looping statement, we will focus our discussion on the simple looping statement construct.


*** Example 1

Write a program that generate the given sequence nos.

1   2   3   4   5

Source code (WHILE LOOP)
--------------------------------------------------
#include<stdio.h>
main()
{
int n;
n=1;
while(n <= 5)
{
printf(" %d ”, n);
delay (30000);
n++;
}
getch();
}
--------------------------------------------------

Source code (DO WHILE LOOP)
--------------------------------------------------
#include<stdio.h>
main()
{
int n;
n=1;
do
{
printf(" %d ”, n);
delay (30000);
n++;
} while (n <= 5);
getch();
}
--------------------------------------------------

Source code (FOR LOOP)
--------------------------------------------------
#include<stdio.h>
main()
{
int n;
for(n=1; n<=5; n++)
{
printf(" %d ”, n);
delay (30000);
}

getch();
}
--------------------------------------------------


*** Example 2

Write a program that will display the following pattern, given the value of n.

Example: if n = 5, output

*
* *
* * *
* * * *
* * * * *

Source code
--------------------------------------------------
#include<stdio.h>
main()
{
int n, x, y;
clrscr();

printf("\n Enter number: ");
scanf("%d”, &n);

x = 1;
while(x <= n)
{
y = 1;
while( y <= x)
{
printf(" * ”);
y++;
}

x++;

printf("\n”)
}

getch();
}
--------------------------------------------------

Programming Exercises

  • Write a program which produces the given sequence nos. (in alternate arrangement) using the three looping statements;
5  1  4  2  3  3  2  4  1  5

Apply three solutions using the three loop statements.
 
  • Write a program that computes the factorial value of n (as input) and displays it. Apply three solutions using the three loop statements.
Enter a no: 4

Factorial value: 24

  • Write a program that generates and display the Fibonacci sequence numbers of n (as input_. In Fibonacci, the current third number is the sum of two previous numbers. Apply three solutions using the three loop statements.

Enter a no: 9

Fibonacci series:  1    1    2    3    5    8    13    21    34
Login form
Calendar
«  December 2016  »
SuMoTuWeThFrSa
    123
45678910
11121314151617
18192021222324
25262728293031
Site friends
CLICK BELOW THE SITE YOU WANT TO GO:

Create your own site

YAHOO Mail

FACEBOOK

GOOGLE

FRIENDSTER

Our poll
Rate my site
Total of answers: 25

Copyright MyCorp © 2016
Make a free website with uCoz