C – Loops

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.

C

// C program to illustrate need of loops printf ( "Hello World\n" ); printf ( "Hello World\n" ); printf ( "Hello World\n" ); printf ( "Hello World\n" ); printf ( "Hello World\n" ); printf ( "Hello World\n" ); printf ( "Hello World\n" ); printf ( "Hello World\n" ); printf ( "Hello World\n" ); printf ( "Hello World\n" ); Output
Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World

There are mainly two types of loops in C Programming:

  1. 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.
  2. 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.

loops in c

for loop in c

Infinite Loop

An infinite loop is executed when the test expression never becomes false and the body of the loop is executed repeatedly. A program is stuck in an Infinite loop when the condition is always true. Mostly this is an error that can be resolved by using Loop Control statements.

Using for loop:

C

// C program to demonstrate infinite // loops using for loop // Driver code // This is an infinite for loop // as the condition expression printf ( "This loop will run forever.\n" );

Output

This loop will run forever. This loop will run forever. This loop will run forever. .

Using While loop:

C

// C program to demonstrate // infinite loop using while // Driver code printf ( "This loop will run forever.\n" );

Output

This loop will run forever. This loop will run forever. This loop will run forever. .

Using the do-while loop:

C

// C program to demonstrate // infinite loop using do-while // Driver code printf ( "This loop will run forever.\n" );

Output

This loop will run forever. This loop will run forever. This loop will run forever. .
Like Article -->

Please Login to comment.

Similar Reads

Print 1 to 100 in C++ Without Loops and Recursion

We can print 1 to 100 without using loops and recursion using three approaches discussed below: 1) Template Metaprogramming: Templates in C++ allow non-datatypes also as parameters. Non-datatype means a value, not a datatype. Example: C/C++ Code // CPP Program to print 1 to 100 // without loops and recursion #include <iostream> using namespac

3 min read C | Loops & Control Structure | Question 1

#include <stdio.h> int main() < int i = 1024; for (; i; i >>= 1) printf("GeeksQuiz"); return 0; >How many times will GeeksQuiz be printed in the above program? (A) 10 (B) 11 (C) Infinite (D) The program will show compile-time error Answer: (B) Explanation: In for loop, mentioning expression is optional. >>= is a composi

1 min read C | Loops & Control Structure | Question 2 1 min read C | Loops & Control Structure | Question 3

What is the output of the below program? C/C++ Code #include int main() < int i = 2; switch (i) < case 0: printf("Geeks"); break; case 1: printf("Quiz"); break; default: printf("GeeksQuiz"); >return 0; > (A) Geeks (B) Quiz (C) GeeksQuiz (D) Compile-time error Answer: (C) Explanation: As i=2, so GeeksQuiz is printed. Quiz of this Question Please co

1 min read C | Loops & Control Structure | Question 4

C/C++ Code #include int main() < int i = 3; switch (i) < case 1: printf("Geeks"); break; case 1+2: printf("Quiz"); break; default: printf("GeeksQuiz"); >return 0; > What is the output of the above program? (A) Geeks (B) Quiz (C) GeeksQuiz (D) Compile-time error Answer: (B) Explanation: Expression gets evaluated in cases. The control goes to the se

1 min read C | Loops & Control Structure | Question 5

Predict the output of the below program: C/C++ Code #include #define EVEN 0 #define ODD 1 int main() < int i = 3; switch (i % 2) < case EVEN: printf("Even"); break; case ODD: printf("Odd"); break; default: printf("Default"); >return 0; > (A) Even (B) Odd (C) Default (D) Compile-time error Answer: (B) Explanation: The following C code will print 'o

1 min read C | Loops & Control Structure | Question 6

C/C++ Code #include int main() < int i; if (printf("0")) i = 3; else i = 5; printf("%d", i); return 0; >Predict the output of above program? (A) 3 (B) 5 (C) 03 (D) 05 Answer: (C) Explanation: The control first goes to the if statement where 0 is printed. The printf(\"0\") returns the number of characters being printed i.e. 1. The block under if st

1 min read C | Loops & Control Structure | Question 7

C/C++ Code #include int i; int main() < if (i) < // Do nothing >else < printf("Else"); >return 0; > What is correct about the above program? (A) if block is executed. (B) else block is executed. (C) It is unpredictable as i is not initialized. (D) Error: misplaced else Answer: (B) Explanation: Since i is defined globally, it is initialized with d

1 min read C | Loops & Control Structure | Question 10

# include <stdio.h> int main() < int i = 0; for (i=0; i<20; i++) < switch(i) < case 0: i += 5; case 1: i += 2; case 5: i += 5; default: i += 4; break; >printf("%d ", i); > return 0; > (A) 5 10 15 20 (B) 7 12 17 22 (C) 16 21 (D) Compiler Error Answer: (C) Explanation: Initially i = 0. Since case 0 is true i becomes 5, and since t

1 min read C | Loops & Control Structure | Question 11

Output of following C program? #include<stdio.h> int main() < int i = 0; for (printf("1st\n"); i < 2 && printf("2nd\n"); ++i && printf("3rd\n")) < printf("*\n"); >return 0; > (A) 1st 2nd * 3rd 2nd * (B) 1st 2nd * 3rd 2nd * 3rd (C) 1st 2nd 3rd * 2nd 3rd (D) 1st 2nd 3rd * 1st 2nd 3rd A

1 min read C | Loops & Control Structure | Question 12

#include <stdio.h> int main() < int i; for (i = 1; i != 10; i += 2) printf(" GeeksQuiz "); return 0; >(A) GeeksQuiz GeeksQuiz GeeksQuiz GeeksQuiz GeeksQuiz (B) GeeksQuiz GeeksQuiz GeeksQuiz . infinite times (C) GeeksQuiz GeeksQuiz GeeksQuiz GeeksQuiz (D) GeeksQuiz GeeksQuiz GeeksQuiz GeeksQuiz GeeksQuiz GeeksQuiz Answer: (B) Exp

1 min read C | Loops & Control Structure | Question 13

What will be the output of the following C program segment? (GATE CS 2012) char inchar = 'A'; switch (inchar) < case 'A' : printf ("choice A \n") ; case 'B' : printf ("choice B ") ; case 'C' : case 'D' : case 'E' : default: printf ("No Choice") ; >(A) No choice (B) Choice A (C) Choice A Choice B No choice (D) Program

1 min read C | Loops & Control Structure | Question 15

In the following program, X represents the Data Type of the variable check. #include <stdio.h> int main() < X check; switch (check) < // Some case labels >return 0; > Which of the following cannot represent X? (A) int (B) char (C) enum (D) float Answer: (D) Explanation: A switch expression can be int, char and enum. A float variable/expressi

1 min read C | Loops & Control Structure | Question 16

What is the output of the following program? #include <stdio.h> int main() < char check = 'a'; switch (check) < case 'a' || 1: printf("Geeks "); case 'b' || 2: printf("Quiz "); break; default: printf("GeeksQuiz"); >return 0; > (A) Geeks (B) Geeks Quiz (C) Geeks Quiz GeeksQuiz (D) Compile-time error Answer: (D) E

1 min read C | Loops & Control Structure | Question 17 1 min read C | Loops & Control Structure | Question 18

How many times GeeksQuiz is printed #include<stdio.h> int main() < int i = -5; while (i <= 5) < if (i >= 0) break; else < i++; continue; >printf("GeeksQuiz"); > return 0; > (A) 10 times (B) 5 times (C) Infinite times (D) 0 times Answer: (D) Explanation: The loop keeps incrementing i while it is smaller than 0. When i becomes

1 min read C | Loops & Control Structure | Question 19

#include <stdio.h> int main() < int i = 3; while (i--) < int i = 100; i--; printf("%d ", i); >return 0; > (A) Infinite Loop (B) 99 99 99 (C) 99 98 97 (D) 2 2 2 Answer: (B) Explanation: Note that the i–- in the statement while(i-–) changes the i of main() And i== just after declaration statement int i=100; changes local i of while l

1 min read C | Loops & Control Structure | Question 20

#include <stdio.h> int main() < int x = 3; if (x == 2); x = 0; if (x == 3) x++; else x += 2; printf("x = %d", x); return 0; >(A) x = 4 (B) x = 2 (C) Compiler Error (D) x = 0 Answer: (B) Explanation: Value of x would be 2. Note the semicolon after first if statement. x becomes 0 after the first if statement. So control goes to else

1 min read C | Loops & Control Structure | Question 21

#include&lt;stdio.h&gt; int main() < int a = 5; switch(a) < default: a = 4; case 6: a--; case 5: a = a+1; case 1: a = a-1; >printf(&quot;%d \n&quot;, a); return 0; > (A) 3 (B) 4 (C) 5 (D) None of these Answer: (C) Explanation: There is no break statement, so first a = a + 1 is executed, then a = a-1 is executed. Quiz of this Questi

1 min read Why are elementwise additions much faster in separate loops than in a combined loop?

When adding more than 2 arrays, separate loops for adding elements step by step prove to give a better performance than adding the elements in a single loop. To Test the above statement, two code blocks add the 4 array elements namely array a with b and c with d. The first code block uses a combined loop to add them while the second block uses two

3 min read Nested Loops in C with Examples

A nested loop means a loop statement inside another loop statement. That is why nested loops are also called "loop inside loops". We can define any number of loops inside another loop. 1. Nested for Loop Nested for loop refers to any type of loop that is defined inside a 'for' loop. Below is the equivalent flow diagram for nested 'for' loops: Synta

5 min read C Programming Language Tutorial

In this C Tutorial, you’ll learn all C programming basic to advanced concepts like variables, arrays, pointers, strings, loops, etc. This C Programming Tutorial is designed for both beginners as well as experienced professionals, who’re looking to learn and enhance their knowledge of the C programming language. What is C?C is a general-purpose, pro

8 min read Pattern Programs in C

Printing patterns using C programs has always been an interesting problem domain. We can print different patterns like star patterns, pyramid patterns, Floyd's triangle, Pascal's triangle, etc. in C language. These problems generally require the knowledge of loops and if-else statements. In this article, we will discuss the following example progra

15+ min read C Programs

To learn anything effectively, practicing and solving problems is essential. To help you master C programming, we have compiled over 100 C programming examples across various categories, including basic C programs, Fibonacci series, strings, arrays, base conversions, pattern printing, pointers, and more. These C Examples cover a range of questions,

8 min read Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc()

Since C is a structured language, it has some fixed rules for programming. One of them includes changing the size of an array. An array is a collection of items stored at contiguous memory locations. As can be seen, the length (size) of the array above is 9. But what if there is a requirement to change this length (size)? For example, If there is

9 min read Operators in C

In C language, operators are symbols that represent operations to be performed on one or more operands. They are the basic components of the C programming. In this article, we will learn about all the built-in operators in C with examples. What is a C Operator?An operator in C can be defined as the symbol that helps us to perform some specific math

14 min read Data Types in C

Each variable in C has an associated data type. It specifies the type of data that the variable can store like integer, character, floating, double, etc. Each data type requires different amounts of memory and has some specific operations which can be performed over it. The data type is a collection of data with values having fixed values, meaning

7 min read

Array in C is one of the most used data structures in C programming. It is a simple and fast way of storing multiple values under a single name. In this article, we will study the different aspects of array in C language such as array declaration, definition, initialization, types of arrays, array syntax, advantages and disadvantages, and many more

15+ min read Bitwise Operators in C

In C, the following 6 operators are bitwise operators (also known as bit operators as they work at the bit-level). They are used to perform bitwise operations in C. The & (bitwise AND) in C takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1. The | (bitwise OR) in C takes two n

7 min read C Language Introduction

C is a procedural programming language initially developed by Dennis Ritchie in the year 1972 at Bell Laboratories of AT&T Labs. It was mainly developed as a system programming language to write the UNIX operating system. The main features of the C language include: General Purpose and PortableLow-level Memory AccessFast SpeedClean SyntaxThese