Decision Making and Looping:
Loops in programming are used to repeat a block of code until the specified condition is met. A loop statement allows programmers to execute a statement or group of statements multiple times without repetition of code
Type of loop:
1. Entry controlled loop
2. Exit controlled loop
1. Entry controlled loop:
In this type of loop first of all condition is checked and then the loop body gets executed till the condition is satisfied and the loop body terminated as soon the condition becomes false.
Entry Controlled loops: In Entry controlled loops the test condition is checked before entering the main body of the loop.
For Loop and While Loop is Entry-controlled loops.
- while loop
- for loop
While loop:
It is the type of entry Controlled looping which first of all the condition is checked and then the loop body gets executed till the condition is satisfied and the loop terminated as soon as the condition becomes false.
Syntax of while loop
The while loop syntax is as follows:
while (test expression)
{
// body consisting of multiple statements
}
Structure of While Loop
The while loop works by following a very structured top-down approach that can be divided into the following parts:
Initialization: In this step, we initialize the loop variable to some initial value. Initialization is not part of while loop syntax but it is essential when we are using some variable in the test expression
Conditional Statement: This is one of the most crucial steps as it decides whether the block in the while loop code will execute. The while loop body will be executed if and only the test condition defined in the conditional statement is true.
Body: It is the actual set of statements that will be executed till the specified condition is true. It is generally enclosed inside { } braces.
Updation: It is an expression that updates the value of the loop variable in each iteration. It is also not part of the syntax but we have to define it explicitly in the body of the loop.
Working of while Loop
We can understand the working of the while loop by looking at the above flowchart:
STEP 1: When the program first comes to the loop, the test condition will be evaluated.
STEP 2: If the test condition is false, the body of the loop will be skipped program will continue.
STEP 3: If the expression evaluates to true, the body of the loop will be executed.
STEP 4: After executing the body, the program control will go to STEP 1. This process will continue till the test expression is true.
Infinite while loop
An infinite while loop is created when the given condition is always true. It is encountered by programmers in when:
- The test condition is incorrect.
- Updation statement not present.
For loop:
It is also the type of entry Controlled loop in which first of all condition is checked and then the loop body gets executed tell the condition is satisfied and the loop terminated as soon as the condition becomes false.
The for loop in C Language provides a functionality/feature to repeat a set of statements a defined number of times. The for loop is in itself a form of an entry-controlled loop.
Unlike the while loop and do…while loop, the for loop contains the initialization, condition, and updating statements as part of its syntax. It is mainly used to traverse arrays, vectors, and other data structures.
Syntax of for Loop
for(initialization; check/test expression; updation)
{
// body consisting of multiple statements
}
Structure of for loop:
Initialization: This step initializes a loop control variable with an initial value that helps to progress the loop or helps in checking the condition. It acts as the index value when iterating an array or string.
Check/Test Condition: This step of the for loop defines the condition that determines whether the loop should continue executing or not. The condition is checked before each iteration and if it is true then the iteration of the loop continues otherwise the loop is terminated.
Body: It is the set of statements i.e. variables, functions, etc that is executed repeatedly till the condition is true. It is enclosed within curly braces { }.
Updation: This specifies how the loop control variable should be updated after each iteration of the loop. Generally, it is the incrementation (variable++) or decrementation (variable–) of the loop control variable.
Working of for Loop:
The working of for loop is mentioned below:
Step 1: Initialization is the basic step of for loop this step occurs only once during the start of the loop. During Initialization, variables are declared, or already existing variables are assigned some value.
Step 2: During the Second Step condition statements are checked and only if the condition is the satisfied loop we can further process otherwise loop is broken.
Step 3: All the statements inside the loop are executed.
Step 4: Updating the values of variables has been done as defined in the loop.
Nested for loop in C
C provides the feature of a nested loop where we can place a loop inside another loop.
Syntax of Nested for loop
for( .. ; .. ; .. ){ for( .. ; .. ; .. ){ .... } }
Advantages of for Loop
There are certain advantages of using for loops in C as mentioned below:
- Provides code reusability
- Code size decreases
- Traversing in data structures like array and string becomes easy.
Disadvantages of for Loop
Despite so many advantages of for loops it even has certain disadvantages:
- Can’t skip any element while traversing
- Only a single condition is followed
2. Exit controlled loop:
In this type of loop first of all the loop body gets executed and then the condition is checked while exiting from the loop body. The loop body repeatedly gets executed till the condition is satisfied and the loop terminates as soon as the condition becomes false. such type of loop is used in cases were we need the execution of the loop at least for one time.
Exit Controlled loops: In Exit controlled loops the test condition is evaluated at the end of the loop body. The loop body will execute at least once, irrespective of whether the condition is true or false. do-while Loop is Exit Controlled loop.
- do while loop
do While loop:
Loops in C language are the control flow statements that are used to repeat some part of the code till the given condition is satisfied. The do-while loop is one of the three loop statements in C, the others being while loop and for loop. It is mainly used to traverse arrays, vectors, and other data structures.
The do…while in C is a loop statement used to repeat some part of the code till the given condition is fulfilled. It is a form of an exit-controlled or post-tested loop where the test condition is checked after executing the body of the loop. Due to this, the statements in the do…while loop will always be executed at least once no matter what the condition is
Syntax of do…while Loop in C
do {
// body of do-while loop
} while (condition);
The working of the do…while loop
- When the program control first comes to the do…while loop, the body of the loop is executed first and then the test condition/expression is checked, unlike other loops where the test condition is checked first. Due to this property, the do…while loop is also called exit controlled or post-tested loop.
- When the test condition is evaluated as true, the program control goes to the start of the loop and the body is executed once more.
- The above process repeats till the test condition is true.
- When the test condition is evaluated as false, the program controls move on to the next statements after the do…while loop.