Home > Tutorials > C Tutorials > Programming In C: For Loops, Numbers and Pictures

Programming In C: For Loops, Numbers and Pictures

Listing numbers

We can use for loops to perform various automated tasks. For example, if you want to list a set of numbers on the screen, simply use a for loop and the %d format specifier.

Here is a program that lists numbers from 1 to 4 on the screen:

#include<stdio.h>
void main( )
{
int num;
for(num = 1; num < 5; ++num) 

printf(“\n%d“, num);

}

Save your source code as numloop.c. Compile it. Link it. Run it. You will see this statement on your screen:

 

1

 

2

 

3

 

4

Listing numbers and characters

One of my favorite ways to use this type of for loop is to see what the character formats (%c) are for various numbers.

 

To do this, I create a loop that counts a specific list of numbers (from 3 to 6). Then I display a column of numbers (%d), followed by the tab escape character (\t), and then a column of corresponding characters (%c). Here is what it looks like:

#include<stdio.h>
void main( )
{
int loop;
for(loop = 3; loop < 7; ++loop) 

printf(“\n%d\t%c“, loop, loop);

}

 

Save your source code as floopy.c. Compile it. Link it. Run it. Depending on whether your computer interprets the numbers in ASCII format or not, you will get a different result in the second column.

NOTE: If you are using the GCC compiler on a Unix system, this example will run but you won’t see the second column.

Drawing Pictures

For loops are useful for drawing pictures on the screen. In the previous example, we discovered the character format for the number 3. Let’s use this character to draw a box. Here are the steps:

  1. The top row will have 5 of the symbols.
  2. The left and right sides of the box will have 5 symbols each.
  3. The bottom row will also have 5 of the symbols.

We must initialize two variables: one for the counter (loop < 6) and one for the number that we’ll display as a symbol (3). Here is the script:

#include<stdio.h>
void main( )
{
int loop;
int symbol = 3;
printf(“\n%c%c%c%c%c”, symbol, symbol, symbol, symbol, symbol);
for(loop = 1; loop < 6; ++loop) 

printf(“\n%c %c”, symbol, symbol);

printf(“\n%c%c%c%c%c\n”, symbol, symbol, symbol, symbol, symbol);
}

 

Save your source code as pic.c. Compile it. Link it. Run it. Depending on whether your computer interprets the numbers in ASCII format or not, you will get a different symbol for the box.

NOTE: If you are using the GCC compiler on a Unix system, you will not see the box. Replace the symbol variable with a star (*) from your keyboard.

Decrement for loop

To decrement means to loop again, counting down (… 4, 3, 2, 1) The symbol for decrement is

The three parts of a decrement for loop are different from an increment for loop:

  1. when we count down, we don’t want to intialize the counter at a particular number, so we use ; instead of loop = 1
  2. set the counter to be greater than zero, like this: loop > 0, so that it will stop when the loop counts down to 0
  3. use the decrement operator to count down: –loop

Putting these three parts together, we get this:

 

for( ; loop > 0; –loop)

Sample decrement for loop

Now let’s use a decrement for loop. This program gives the viewer 5 chances to answer the question “What year did Maryland become a state?”.

  1. If the answer is correct, we print “Yes, Maryland became a state in 1788” and the loop is exited, using return;
  2. If the answer is wrong, we print “No” and the loop repeats the question again, for 5 attempts.
  3. After 5 attempts, if the answer is still wrong, the loop is exited and we print “Thank you for trying. Maryland became a state in 1788.”

We must initialize three variables: correct for the year Maryland became a state, attempt for the counter, and year for the viewer’s guess.

#include<stdio.h>
void main( )
{
int correct = 1788;
int attempt = 5;
int year;
printf(“\nYou have five attempts to answer this question:”);
for( ; attempt > 0; –attempt)

printf(“\nWhat year did Maryland become a state? “);
scanf(“%d”, &year);
if (year == correct) {

printf(“\nYes, Maryland became a state in %d.”, year);
return;

 

}
else


printf(“\nNo.”);

}
printf(“\nThank you for trying. Maryland became a state in 1788.\n”);
}

 

Save your source code as decloop.c. Compile it. Link it. Run it.

Continue on to Infinite and Nested Loops.

About Michael Boguslavskiy

Michael Boguslavskiy is a full-stack developer & online presence consultant based out of New York City. He's been offering freelance marketing & development services for over a decade. He currently manages Rapid Purple - and online webmaster resources center; and Media Explode - a full service marketing agency.

Check Also

How To Populate A DIV via an HTML SelectBox, PDO, and jQuery

Imagine you have a database full of information, and you want to let users browse ...

Like every other website, this site uses cookies to analyze our traffic. Cookies may also be utilized by our advertisers and partners. By using this website you agree to the use of said cookies. More Information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings, continuing to navigate past this message, or you click "Accept" below then you are consenting to the use of cookies on the Rapid Purple website.

Close