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

For, Continue

For Loop

The for loop incorporates the counter's initialization, the condition to exit the loop, and the counter's update.

Let's take the example from the previous lesson (displaying array elements) and reframe it using a for loop:

c

Main.c

Loop's Flexibility

You can use any valid expression as the update counter. This means the counter can adapt based on any valid rule, such as (++x * 3) - 5.

Furthermore, the counter can increment by characters instead of numbers.

c

Main.c

Note

Keep in mind that characters are stored as numbers in memory.

Even if you leave out parts of the loop, like the counter initialization, the program will still compile. However, the semicolon ; remains essential:

c

Main.c

You can use the comma , to separate parts within the loop:

c

Main.c

Continue

In loops, both the break and continue commands come in handy. While the break statement forces an exit from the loop, the continue command prompts a skip to the subsequent iteration.

Consider a program designed to exclude negative integers, utilizing the continue operator:

c

Main.c

New Operators

The following operators can also function as expressions to update the counter:

OperatorExpression ExampleDescription
+=a += 5Equivalent to a = a + 5
-=a -= 5Equivalent to a = a - 5
*=a *= 5Equivalent to a = a * 5
/=a /= 5Equivalent to a = a / 5
%=a %= 5Equivalent to a = a % 5

Everything was clear?

Section 4. Chapter 6
course content

Course Content

C Basics

For, Continue

For Loop

The for loop incorporates the counter's initialization, the condition to exit the loop, and the counter's update.

Let's take the example from the previous lesson (displaying array elements) and reframe it using a for loop:

c

Main.c

Loop's Flexibility

You can use any valid expression as the update counter. This means the counter can adapt based on any valid rule, such as (++x * 3) - 5.

Furthermore, the counter can increment by characters instead of numbers.

c

Main.c

Note

Keep in mind that characters are stored as numbers in memory.

Even if you leave out parts of the loop, like the counter initialization, the program will still compile. However, the semicolon ; remains essential:

c

Main.c

You can use the comma , to separate parts within the loop:

c

Main.c

Continue

In loops, both the break and continue commands come in handy. While the break statement forces an exit from the loop, the continue command prompts a skip to the subsequent iteration.

Consider a program designed to exclude negative integers, utilizing the continue operator:

c

Main.c

New Operators

The following operators can also function as expressions to update the counter:

OperatorExpression ExampleDescription
+=a += 5Equivalent to a = a + 5
-=a -= 5Equivalent to a = a - 5
*=a *= 5Equivalent to a = a * 5
/=a /= 5Equivalent to a = a / 5
%=a %= 5Equivalent to a = a % 5

Everything was clear?

Section 4. Chapter 6
some-alt