Learn to Code via Tutorials on Repl.it!

← Back to all posts
C wars: Chapter 4: A new loop
CodeLongAndPros (1644)

Chapter four: A new loop

Today, I'll talk about one of the most important structures in programming:

This series is taking a 24 hour break. Yes, no post on Wednesday.

The loop

You know what a loop is in real life, right?


A loop in programming does the same thing, except it does it based on a boolean value.

Loop #1: While.

This loop has no magic. Just does the code inside {} while bool is true:

For example:

Will output:

Let me introduce you to a new function, getchar().
This reads a char from STDIN and returns it.

So let's make a program that reads input:

This introduces a few new concepts.

When you put an assignment in (), you can use it to compare.


is equal to

Loop #2: For loops

These are a little more complex.

They have the syntax

The assignment is usually int i = 0.

This sets i to 0;

The bool is a boolean, usually dealing with i.

For example: i < 10.

And change is how to change the variable.

For example: i++.

Now is a ripe time to introduce to two new operators:


This changes tha double/int by one.

There are two forms:

  • ++i
  • i++

What's the difference?

Well, i++ adds one to i, but returns i.

For example:

Will say:

While ++i will say:

Now, back to the for loop:

To print all the numbers between 1-100, you would use:

Loop #3: Do while.

This loop is a bit overlooked.

To know why this loop exisits, we have to look a little close at the while loop.

If you have this code:

Will it run. No. (We can't tell if it will halt, though.)

However, if you use a d-w (do-while), it will run.

The D-W syntax is:

Again, bool is a boolean condition.

So you can complete your homework, I need to teach you one more thing:

The % operator.

This computes the remainder between two numbers.

It's commonly used to check if a number is even.

if (num % 2 == 0)


Now, make a program that computes the Collatz conjucture

The Collatx conjucture states:

If a number is even, divide it by two.
If not, triple it and add one.
If you do this enough, it will become one.

Impliment this in C.

Please progmpt the user for input and print out the number after each cycle:


So long!
Part One: The Segfault menace

Part Two: The data wars

Part Three: Revenge of the if

Part Four: A new loop

Part Five: The Empire points back

Part Six: Return of the function

HahaYes (1868)

hmmm gotta find a way to make this popular

JustAWalrus (1199)

[ link redacted by moderators for advertising ]

JustAWalrus (1199)

Also C bad anyway :D

Jakman (453)

@Wuru Let me guess. You dont like Manual GC or pointers.

JustAWalrus (1199)

Pointers are incredibly easy and fun. Manual GC is a pain. @Jakman

Jakman (453)

@Wuru Read what you said,just slowly.

CodeLongAndPros (1644)

@Jakman Think about iSegmentation fault (core dumped)

JustAWalrus (1199)

Pointers are fun to work with. Especially in OS development, which I do most often. Way better than manually keeping track of memory locations in x86 Assembly. @Jakman

Jakman (453)

@CodeLongAndPros I legit had some flashbacks of Segmentation fault errors. Good thing I use Rust now.

Jakman (453)

@Wuru Show me where Rust touched you.

JustAWalrus (1199)

In the plain of unemployment. @Jakman

Jakman (453)

@Wuru It is like the weed of Programming languages. It expands your mind so that you see things in a different and more objective perspective.

JustAWalrus (1199)

It also lands you in your mom’s basement at 30. @Jakman

Jakman (453)

@Wuru my house doesnt have a basement. I live in a lower-middle class neighborhood.

HahaYes (1868)

Hey @CodeLongAndPros can you do me a favor?

CodeLongAndPros (1644)

@HahaYes Perhaps. I make no promises.

HahaYes (1868)

@CodeLongAndPros umm can you report the next comment to rick roll the repl staff?

HahaYes (1868)

@CodeLongAndPros gotta rickroll amasad and patrick

HahaYes (1868)

@CodeLongAndPros You spelled chapter wrong.

lightningrock (155)



CodeLongAndPros (1644)

@lightningrock No, wait. three. Taking a break between D4 and 5

CodeLongAndPros (1644)

@lightningrock Sorry, have to brush up on pointers.

HahaYes (1868)

@lightningrock wait did you mean me? This is @CodeLongAndPros C tutorial not C++ tutorial. And yes, day 6 is coming out today.

lightningrock (155)

oh wait i get them mixed up. tbh, i'm not sure which one i meant @HahaYes

HahaYes (1868)

@lightningrock hmmm I think you mean me. Now unvote this and vote yes for mine.

HahaYes (1868)

@lightningrock jk just upvote mine so I can beat everyone. If I'm Pewdiepie, then he is bad T-Series. But I'm T series?

HahaYes (1868)

@CodeLongAndPros what did you make that yourself

CodeLongAndPros (1644)

@HahaYes Well yes. Try to find the link to it