C while Loop

You are Here:

C while Loop

The while loop executes a block of code as long as the test condition evaluates to true.

Note: Unlike do while loop, In while loop, the condition is evaluated before executing the statement.

Syntax

while(condition){ // code block }

Simple while Loop

In the following program, we will print 'Hello World' for 5 times using while loop.

Source Code

C Compiler
#include <stdio.h> int main() { int i = 0; while(i<5){ printf("Hello World \n"); i++; } return 0; }

Output

Hello World Hello World Hello World Hello World Hello World

while Loop to print numbers

In the following program, we will print numbers from 0 to 4 using while loop.

Source Code

C Compiler
#include <stdio.h> int main() { int i = 0; while(i<5){ printf("%d ",i); i++; } return 0; }

Output

0 1 2 3 4

while Loop to print Elements in a Array

In the following program, we will print all the elements in an array using while loop.

Source Code

C Compiler
#include <stdio.h> int main() { int i = 0; char absent[3] = {13, 25, 36}; printf("Absent Roll No's are: "); while(i<3){ printf("%d ", absent[i]); i++; } return 0; }

Output

Absent Roll No's are: 13 25 36

while Loop to print Array of Strings

In the following program, we will print the array of strings using while loop.

Source Code

C Compiler
#include <stdio.h> int main() { int i = 0; char students[3][6] ={"gouri", "ram", "alex"}; while(i<3){ printf("%s \n", students[i]); i++; } return 0; }

Output

gouri ram alex
congratulation

Congratulation! you have learned everything about while loop in C Programming.

Reminder

Hi Developers, we almost covered 98% of String functions and Interview Question on C with examples for quick and easy learning.

We are working to cover every Single Concept in C.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author