ankitweblogic logo
W e b t u t o r i a l s

Programing Approach

Algorithm

Flow Chart

Define C

Define Programming

C - Syntax

C - Variables

C - Keywords

C - Data Types

C - Comments

Turbo C - Shortcut keys

C - Operators

Hierarchy of Operators

Ex: Arithmetic Operator

Constant Variable

C - Escape Sequence

Control Structure - if

Ex: If condition

Ex: Inc / Dec Operator

C - loops

Ex: Loops

C - Nesting Of loops

Ex: Nested Loops

Jumping Statements

C - Arrays 1D

C - Arrays 2D

C - Sorting

ASCII Value

Character I/O Function

C - Strings

Ex: Strings

Array of Strings

C - Math Functions

User-defined Function

Exercise Function

C - Types of Functions

Storage Class

C - Header Files

C - Preprocessor

C - Pointers

C - Structures

C - Typedef

C - File Handling

Ex: File Handling

Command Line Argument

MCQ

C Loops

While writing a program, there may be a situation when you need to repeat a block of code again and again upto certain number of times or up to infinity. In such situation, you would need to write loop statements. In C-Language, there are 3 types of loops available. They are:
for loop.
while loop.
do...while loop.

For Loop

For loop is divided into 3 parts i.e. initialization, condition and increment/decrement. Initialization is the initial value from where the loop start execution, condition will execute a loop till the specified condition is true and quits the loop if it is false, increment/decrement will change the variable value for the next iteration.

Example 1: WAP to print counting 1 to 10 using For Loop.

void main()
{
    int i;
    for(i=1;i<=10;i++)
    {
        printf("For Loop %d",i);
    }
}
Output

For Loop 1 For Loop 2 For Loop 3 For Loop 4 For Loop 5 For Loop 6 For Loop 7 For Loop 8 For Loop 9 For Loop 10

While Loop

In While loop, initialization statement appears before the while loop begins, than condition appears to test the loop, than increment/decrement statement will appear inside the while loop to change the variable value for the next iteration.

Example 2: WAP to print counting 1 to 10 using While Loop

void main()
{
    int i=1; //initialization
    while(i<=10) //condition 
    {
        printf("While Loop %d",i);
        i++; //increment
    }
}

Output

While Loop 1 While Loop 2 While Loop 3 While Loop 4 While Loop 5 While Loop 6 While Loop 7 While Loop 8 While Loop 9 While Loop 10

Do While Loop

Do while loop is similar to while loop, only the different is that the condition appears in the end.

Example 3: WAP to print counting 1 to 10 using Do While Loop

void main()
{
    int i=1; //initialization
    do
    {
        printf("Do While Loop %d",i);
        i++; //increment
    }while(i<=10); //condition 
}

Output

Do while loop 1 Do while loop 2 Do while loop 3 Do while loop 4 Do while loop 5 Do while loop 6 Do while loop 7 Do while loop 8 Do while loop 9 Do while loop 10