Category: Loops in C

Interested in learning C++ Programming? Click here...


for Statement – for Loop in C Programming Language 39

for Statement – for Loop in C Programming Language

In while and do while statement we need to write logic to repeatedly execute a block of statement by initializing a counter and incrementing it after a particular steps. This sometime looks tedious and decreases the readability of the program. The readability of the C program can be improved by using for statement. Using for statement in C language, we can merge all the three parts i.e. assignment, condition checking and increment/decrementing. Syntax:

Initialization: setting up a loop counter to initial value test condition: Testing the loop counter to determine whether the loop needs to be executed or not....

do-while Statement in C Programming Language 23

do-while Statement in C Programming Language

Let us understand do while statement in C with help of an example. syntax:

Explanation: Here, while and do are the keywords in C language which is know to the compiler. Condition can be any expression. This is very similar to while loop. Here, the block of statement enclosed in the opening and closing braces after the keyword do is executed at least once. After the execution of the block of statement for the first time, the condition in the while is executed. If the condition is satisfied then the block of statement inside the do block is executed....


Loops in C Programming Language 10

Loops in C Programming Language

Loops in C are used to change the sequence or flow of the program. The basic behavior of the program is that it starts it from the beginning of the program, executes the particular statement only once and proceeds to the next statement. This behavior is continued till the end of the program is reached. But sometimes we need to execute a particular statement more than once. At this point Loops in C plays an important role. Following are the types of Loops in C language we can use in the program: while statement do-while statement for statement

while Statement in C Programming Language 20

while Statement in C Programming Language

In this article, we will understand the while loop statement in C programming language. Let us suppose you want to execute a block of statement 5 times. There is one approach using goto statement as shown below.

Output:

The above mentioned approach solves our purpose. But it is very tedious job to take care of labels. It also decreases the readability of the program. Hence there is another approach to perform the above task very effectively. We will use while loop. Syntax:

Explanation: Here, while is the keyword which is known to the compiler. Condition can be...