Skip to content Skip to sidebar Skip to footer

How to Use if Loop to Run a Program Again C

Loop In C Language

A loop argument allows us to execute a argument or group of statements multiple times. Given beneath is the general class of a loop argument in nigh of the programming languages −

Loop Architecture

C programming language provides the post-obit types of loops to handle looping requirements.

Sr.No. Loop Blazon & Clarification
one while loop

Repeats a statement or grouping of statements while a given condition is true. It tests the condition earlier executing the loop body.

2 for loop

Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.

3 practise...while loop

Information technology is more like a while statement, except that information technology tests the condition at the end of the loop body.

four nested loops

You tin can use one or more than loops within any other while, for, or do..while loop.


While Loop in C

A while loop is the most straightforward looping structure. While loop syntax in C programming language is as follows:

Syntax of While Loop in C:

while (condition) {              statements; }          

It is an entry-controlled loop. In while loop, a condition is evaluated before processing a body of the loop. If a condition is truthful then and only then the trunk of a loop is executed. Later the body of a loop is executed then control again goes back at the beginning, and the condition is checked if it is true, the same process is executed until the condition becomes false. One time the condition becomes faux, the control goes out of the loop.

Afterwards exiting the loop, the control goes to the statements which are immediately later on the loop. The body of a loop tin incorporate more ane statement. If it contains only one statement, then the curly braces are not compulsory. It is a good do though to employ the curly braces fifty-fifty we have a single argument in the torso.

In while loop, if the condition is non true, then the body of a loop will not exist executed, not even once. It is unlike in do while loop which we volition see presently.

Following program illustrates while loop in C programming case:

#include<stdio.h> #include<conio.h> int principal() { 	int num=one;	//initializing the variable 	while(num<=10)	//while loop with condition 	{ 		printf("%d\n",num); 		num++;		//incrementing operation 	} 	render 0; }          

Output:

ane 2 three 4 5 6 7 eight 9 10          

Do-While loop in C

A do…while loop in C is similar to the while loop except that the status is always executed after the body of a loop. It is too called an exit-controlled loop.

Syntax of practise while loop in C programming language is every bit follows:

Syntax of Practise-While Loop in C:

            do {   statements } while (expression);

As we saw in a while loop, the body is executed if and only if the condition is truthful. In some cases, we have to execute a body of the loop at to the lowest degree in one case even if the condition is false. This blazon of functioning can be achieved by using a practice-while loop.

In the do-while loop, the body of a loop is always executed at least once. After the body is executed, so it checks the condition. If the condition is truthful, and so it will over again execute the body of a loop otherwise command is transferred out of the loop.

Like to the while loop, once the control goes out of the loop the statements which are immediately after the loop is executed.

The disquisitional difference between the while and do-while loop is that in while loop the while is written at the get-go. In do-while loop, the while condition is written at the end and terminates with a semi-colon (;)

The following loop program in C illustrates the working of a do-while loop:

Beneath is a do-while loop in C case to print a table of number 2:

#include<stdio.h> #include<conio.h> int primary() { 	int num=1;	//initializing the variable 	practice	//do-while loop  	{ 		printf("%d\n",2*num); 		num++;		//incrementing functioning 	}while(num<=10); 	return 0; }          

Output:

ii 4 6 viii ten 12 14 16 eighteen xx          
            For loop in C          
            

A for loop is a more than efficient loop structure in 'C' programming. The full general structure of for loop syntax in C is equally follows:

Syntax of For Loop in C:

for (initial value; condition; incrementation or decrementation )  {   statements; }            
  • The initial value of the for loop is performed only once.
  • The condition is a Boolean expression that tests and compares the counter to a fixed value subsequently each iteration, stopping the for loop when false is returned.
  • The incrementation/decrementation increases (or decreases) the counter by a gear up value.

Following program illustrates the for loop in C programming case:

#include<stdio.h> int main() { 	int number; 	for(number=ane;number<=10;number++)	//for loop to print 1-10 numbers 	{ 		printf("%d\northward",number);		//to impress the number 	} 	return 0; }            

Output:

i two 3 iv 5 six 7 8 9 ten            

The in a higher place programme prints the number serial from ane-10 using for loop.

thurmanbablin63.blogspot.com

Source: https://cprogrammaterial.blogspot.com/2022/04/loop-in-c-language.html

Postar um comentário for "How to Use if Loop to Run a Program Again C"