Skip to content
← Back to Community
C wars: Chapter 4: A new loop
Profile icon
CodeLongAndPros

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?

Good.

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:

while (bool) { }

For example:

while (true) { printf("Linux is better\n"); }

Will output:

Linux is better Linux is better Linux is better Linux is better Linux is better Linux is better Linux is better Linux is better Linux is better Linux is better ...

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:

#include <stdio.h> int main() { char c; while ((c = getchar()) != EOF) { printchar(c); } }

This introduces a few new concepts.

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

So

char c = 65; while (c != EOF) { printchar(c); c = getchar() }

is equal to

char c; while ((c = getchar()) != EOF) { printchar(c); }

Loop #2: For loops

These are a little more complex.

They have the syntax

for (assignment; bool; change){}

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:

int i = 1 printf(i++); printf(i)

Will say:

1 2

While ++i will say:

2 2

Now, back to the for loop:

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

for (int i = 0; i < 100; i++) // Needs 0 printf(i);

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:

int i = 0; while (i != 0) { super_important(); }

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:

do { } while (bool)

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)

Homework

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:

8 42 1

or

17 52 26 13 40 20 10 5 16 8 4 2 1

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

Voters
Profile icon
VincentL19
Profile icon
renaudtp
Profile icon
elcorreodeangel
Profile icon
EpicGamer007
Profile icon
awesome10
Profile icon
mkw
Profile icon
HahaYes
Profile icon
Jakman
Profile icon
lightningrock
Profile icon
CodeLongAndPros
Comments
hotnewtop
Profile icon
HahaYes

hmmm gotta find a way to make this popular

Profile icon
JustAWalrus

[ link redacted by moderators for advertising ]

Profile icon
CodeLongAndPros

@Wuru I C.

Profile icon
HahaYes

@Wuru I'm suprised you aren't banned yet

Profile icon
JustAWalrus

HahaYes @HahaYes

Profile icon
JustAWalrus

Also C bad anyway :D

Profile icon
Jakman

@Wuru lol no.

Profile icon
JustAWalrus

Lol yes @Jakman

Profile icon
Jakman

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

Profile icon
JustAWalrus

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

Profile icon
Jakman

@Wuru Read what you said,just slowly.

Profile icon
CodeLongAndPros

@Jakman Think about iSegmentation fault (core dumped)

Profile icon
Jakman

@CodeLongAndPros PTSD Intensifies

Profile icon
JustAWalrus

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

Profile icon
CodeLongAndPros

@Jakman

GDB has entered the room

Profile icon
Jakman

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

Profile icon
JustAWalrus

Ew Rust. @Jakman

Profile icon
Jakman

@Wuru Show me where Rust touched you.

Profile icon
JustAWalrus

In the plain of unemployment. @Jakman

Profile icon
Jakman

@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.

Profile icon
JustAWalrus

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

Profile icon
Jakman

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

Profile icon
JustAWalrus

You get the point. @Jakman

Profile icon
Jakman

@Wuru yeah

Profile icon
JustAWalrus

Manjaro bad Ubuntu is better tbh -_-

Profile icon
CodeLongAndPros
Profile icon
HahaYes

Hey @CodeLongAndPros can you do me a favor?

Profile icon
CodeLongAndPros

@HahaYes Perhaps. I make no promises.

Profile icon
HahaYes

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

Profile icon
CodeLongAndPros
Profile icon
HahaYes

@CodeLongAndPros gotta rickroll amasad and patrick

Profile icon
HahaYes

@CodeLongAndPros You spelled chapter wrong.

Profile icon
lightningrock

yay

WHEN ARE U GOING TO DO D6?

Profile icon
CodeLongAndPros

@lightningrock Day 6? In two days?

Profile icon
lightningrock
Profile icon
CodeLongAndPros

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

Profile icon
lightningrock
Profile icon
CodeLongAndPros

@lightningrock Sorry, have to brush up on pointers.

Profile icon
lightningrock

no it's fine @CodeLongAndPros

Profile icon
HahaYes

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

Profile icon
lightningrock

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

Profile icon
HahaYes

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

Profile icon
lightningrock
Profile icon
HahaYes

@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?

Profile icon
CodeLongAndPros

@HahaYes Now that is unfair.

image

Profile icon
HahaYes

@CodeLongAndPros what did you make that yourself

Profile icon
CodeLongAndPros

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

Profile icon
lightningrock

I upvoted both none.
@HahaYes
@CodeLongAndPros