Skip to main content

Loops in C programming - Computer science fundamentals tutorial



Loop control structure

**note**
PDF version of this post also available. To get the PDF click the link given at the end of this post.

Loop control structure consist of two parts. the two parts are a body of loop and control statement. Loop control structure are used to execute and repeat a block of statement depending on value of a condition
There are three types of loop control statement-
1. For loop.
2. While loop.
3. Do - while loop.
1. for loop: - The For loop provides a mechanism to repeat a task until a particular condition is true. For loop is usually known as determinate or definite loop because the programmer knows exactly how many times the loop will repeat. The number of times the loop has to be executed can be determined mathematically by checking the logic of the loop.
Syntax of for loop : -
For(initialcondition;<endcondition>;increment/decrement)
{
   Statement block;
}
Example of For loop: -




Explanation of this program: -
Here we can see in For loop, loop started from i=0.
Its end condition is i<10.
In this loop it started execute from i=0. After first iteration it incremented to one and repeat same task and print ‘hello’ second time. It continue to execute until initial condition greater than 10.when it greater than 10 loop stop execute
Output: -


While loop: - The while loop provides a mechanism to repeat one or more statements while a particular condition is true.
Syntax of while loop: -
Statement x;
While(condition)
{
  Statement block;
}
Statement y;
Example of while loop: -


Explanation of this program:-
At first initialize i=1.
Then started while loop with ’i<=10’ condition.
Initially i=1 and is less than 10,i.e. condition is true,so in the loop the value i is printed and condition is update so that with every execution of the loop. The loop execute until value of  i greater than equal to 10.
Output: -


Do-while loop:-
The do – while loop is similar to the while loop. The only difference is that in a do-while loop, the test condition evaluated at the end of the loop. Now that the test condition is evaluated at the end which means that the body of the loop gets executed at least one time even condition is false.


Syntax of the Do-while loop: -
statement x;
do
{
  statement block;
}while(condition);
statement y;
Example of Do-while loop: -


Output: -


The major disadvantage of Do-while loop is that always execute at least once, even if the user enters some invalid data, the loop will execute.



Comparison of loop structure statements: -


click here for pdf version of this post

PDF of Loop control structure in C

Comments

  1. Thats really great blog , thanks for sharing to us.
    Get all type digital machine like as AM.CO.ZA V-Series™ High-Pressure High-Speed USB Vinyl Cutters Vinyl Cutter

    ReplyDelete

Post a Comment

please subscribe my blog and let me suggest how I improve this site

Popular posts from this blog

Important MCQ of RDBMS( Relational database management system)-FCST

Important MCQ of RDBMS  1. A RDBMS consists a collection of ? a. Tables b. Fields c. Records d. Keys  ANS/- a. table 2. The term attribute refers to a ___________ of a table a. Record b. Tuple c. Column d. Key   ans/- c. Column 3. In relational model, the row of table is known to be ?  a. Relation b. Entity field c. Tuple d. Attribute  ans/- C. Tuple 4. . Address field of a person should not be part of primary key, since it is likely to ? a. Dependent b. Too long c. Changed d. Not changed  ans/- c. Changed 5. The relational model is concerned with ? a. Data structure and Data integrity b. Data Manipulation c. Both a and b d. None of these  ans/- c. Both a and b 6. Which is the false statement from the following ? a. A veiw is a named derived table b. A name relation is variable c. A veiw is a named reation and is virtual d. None of these  ans/- d. None of these 7. The union of primary key...

Short notes on database schema & database independence- computer science fundamentals tutorial

            Database Schema &  Data Independence PDF version of this post also available. to download the click the link given at the end of this post.           Database Schema : -  A database schema   is the skeleton structure that represents the logical view of the entire database. It defines how the data is organized and how relat ion among them are associated.  Database schema  formulates all the constrains that are to be applied on the data. A database schema defines its entities and the relationship among them. It contains descriptive details of the database which can be described by means of schema diagram. A database schema can be divided into two categories.   I.     Physical database schema : - this schema contain to the actual storage of data and its form of storage like files, indices etc. It defines how the data will be stored in a secondary storage. ...

C program to find factorial of number using function - FCST

C program to find factorial of number using function Problem description In this program take number as input & then it show the factorial of given number as output. Problem solution 1.take a number as input. 2.declare the void Fact(int) function globally if you want or declare the function prototype  into the main() function. 3.after taking input call the function into main() . 4.then define the function definition. 5.after taking function definition for loop started in this function definition. 6. In for loop i=1 & fact=1 then fact=1. after this loop is continue and incremented by1 until the n is grater than equal to i. continuity of loop is depend on what number take as input .  Program code: #include<stdio.h> void Fact(int);//function prototype declearation int i,fact=1;//global variable declearation int main()  { int n; printf("\n enter the  number:"); scanf("%d",&n); Fact(n);//function call } //func...