Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
While, Do-While | Control Statements
C Basics

While, Do-While

Imagine a scenario where you need to repeatedly execute certain tasks, like reading data from a sensor, attempting password entries, or counting words in a sentence. In these situations, loops come into play.

Loops enable you to run specific blocks of code multiple times, be it tens, hundreds, or even thousands of times. Grasping the concept of loops is vital in programming. This course delves into the foundational loops: the while loop, do-while loop, and for loop.

While Loop

This loop continues running as long as a specific condition is met. Once the condition isn't satisfied, the loop stops.

A basic use of a loop is to display the count of its repetitions:

c

Main.c

To halt this loop, a terminating condition is essential. A straightforward approach is using a counter to track the number of times the loop runs.

Note

An iteration refers to a single cycle within a loop. So, if the loop runs the code block 10 times, it has completed 10 iterations.

The line iterations++; is crucial as it increments the counter (int iterations) with each pass. The counter then sets the conditions to terminate the loop.

Note

It's imperative to establish conditions to exit the loop. Failing to do so will result in an endless loop.

Let's craft a program to showcase the elements of an integer array:

c

Main.c

Focus on the expression array[i]. Here, the variable i denotes the index of the array[] elements.

Note

An iteration refers to the count of loop cycles.

With every cycle, the variable i increases by 1. This means that during each cycle, the expression array[i] accesses the subsequent array element:

do-while

The key distinction between the do-while and while loops is that the former guarantees at least one execution, even if its condition is initially false.

Example:

c

Main.c

This type of loop is handy for crafting basic user interfaces. For instance, when prompting for a password, the program will repeatedly ask until the user inputs the correct one:

c

Main.c

What will be the value of x at the 6th iteration?

Select the correct answer

Everything was clear?

Section 4. Chapter 5
course content

Course Content

C Basics

While, Do-While

Imagine a scenario where you need to repeatedly execute certain tasks, like reading data from a sensor, attempting password entries, or counting words in a sentence. In these situations, loops come into play.

Loops enable you to run specific blocks of code multiple times, be it tens, hundreds, or even thousands of times. Grasping the concept of loops is vital in programming. This course delves into the foundational loops: the while loop, do-while loop, and for loop.

While Loop

This loop continues running as long as a specific condition is met. Once the condition isn't satisfied, the loop stops.

A basic use of a loop is to display the count of its repetitions:

c

Main.c

To halt this loop, a terminating condition is essential. A straightforward approach is using a counter to track the number of times the loop runs.

Note

An iteration refers to a single cycle within a loop. So, if the loop runs the code block 10 times, it has completed 10 iterations.

The line iterations++; is crucial as it increments the counter (int iterations) with each pass. The counter then sets the conditions to terminate the loop.

Note

It's imperative to establish conditions to exit the loop. Failing to do so will result in an endless loop.

Let's craft a program to showcase the elements of an integer array:

c

Main.c

Focus on the expression array[i]. Here, the variable i denotes the index of the array[] elements.

Note

An iteration refers to the count of loop cycles.

With every cycle, the variable i increases by 1. This means that during each cycle, the expression array[i] accesses the subsequent array element:

do-while

The key distinction between the do-while and while loops is that the former guarantees at least one execution, even if its condition is initially false.

Example:

c

Main.c

This type of loop is handy for crafting basic user interfaces. For instance, when prompting for a password, the program will repeatedly ask until the user inputs the correct one:

c

Main.c

What will be the value of x at the 6th iteration?

Select the correct answer

Everything was clear?

Section 4. Chapter 5
some-alt