Menu
Accueil
Forum
Liste des membres
Livre d'or
S'inscrire
Maths
Modules pour CrazyCMS
Consommation électricité
Me contacter
Espace Membre
Statistiques
livre_dor
Pas de commentaires dans le livre d'orRetour au menu
1)
The “for” loop
2) The “while” loop
3)
The “until” loop
4) Some keywords used in the loops: “break” and “continue”
The loops are a part of all
languages, which is very interesting. Indeed, the aim is to deal with a great
number of data and write less code than without loops. Often writing a code
without loops is impossible. There are two or three kinds of loops. I say two or
three because two loops are very similar. I will call these three loops: the
“for” loop, the “while” loop and the “until” loop. The “until” loop do not
really exist in C language but I will explain it later.
1) The “for” loop
I begin with this loop because it is
the first loop I have learned. Indeed, before starting dealing with much data,
the most interesting is, for example, to repeat several times the same text on
the screen. Of course, if you want, you can make several times “copy & paste”
but your code will become very huge and unreadable. I start with a brief example
and after I will explain the syntax and the aims of this loop.
// C Source File
// Created 01/11/2002; 21:58:08
#define USE_TI89 // Produce .89z File
#define USE_TI92PLUS // Produce .9xz File
#define OPTIMIZE_ROM_CALLS // Use ROM Call Optimization
#define SAVE_SCREEN // Save/Restore LCD Contents
#include <tigcclib.h> // Include All Header Files
// Main Function
void _main(void)
{
int i;
clrscr();
for (i = 1; i < 10; i++ ) {
printf ("You have to type 9 times anywhich key to exit the program\n");
ngetchx();
}
}
If you would not use a loop, you
should write this code:
// C Source File
// Created 01/11/2002; 21:58:08
#define USE_TI89 // Produce .89z File
#define USE_TI92PLUS // Produce .9xz File
#define OPTIMIZE_ROM_CALLS // Use ROM Call Optimization
#define SAVE_SCREEN // Save/Restore LCD Contents
#include <tigcclib.h> // Include All Header Files
// Main Function
void _main(void)
{
clrscr();
printf ("You have to type 9 times anywhich key to exit the program\n");
ngetchx();
printf ("You have to type 9 times anywhich key to exit the program\n");
ngetchx();
printf ("You have to type 9 times anywhich key to exit the program\n");
ngetchx();
printf ("You have to type 9 times anywhich key to exit the program\n");
ngetchx();
printf ("You have to type 9 times anywhich key to exit the program\n");
ngetchx();
printf ("You have to type 9 times anywhich key to exit the program\n");
ngetchx();
printf ("You have to type 9 times anywhich key to exit the program\n");
ngetchx();
printf ("You have to type 9 times anywhich key to exit the program\n");
ngetchx();
printf ("You have to type 9 times anywhich key to exit the program\n");
ngetchx();
}
You can see this second code has 5
lines in more. However, if you do not know how many times you want to display
the same text, you had to use a loop or else you stop programming. :-) The
syntax is:
For (var = initial value; condition; step) {
Statement
}
In fact, firstly you assign your
variable called var. This assignment is the initial value of your variable.
Afterwards, I will often call this variable “a” and “z” because I use a French
keyboard and it is easier. It is a good or a bad habit? I do not know; it is my
way to program. Secondly, you must tell your computer when it has to stop the
loop and goes on the following code. That is why the syntax is like a condition:
“var < ended value + 1”. We can translate with the following sentence: “the
computer goes on the loop until the value of ‘var’ is equal to the ended value”.
Thirdly, you should give the step of your loop i.e. you choose the values which
your variable will take. In fact, it is like an assignment. The assignment: “var++”
is generally used but you can also use another assignment, for example, var += 2
if you want to get the odd or even values. Here I am showing the greatest part
of loop. Indeed, remember that var is a variable and the variable is very
interesting and can be used with the statement of your loop so as to deal with
data. To simplify, we can see a brief example. If you want to display the
numbers from 1 to 10. You can write:
clrscr();
printf ("1\n2\n3\n3\n4\n5\n6\n7\n8\n9\n10");
ngetchx();
However, that is not the aim of this
part. That is why we are going to use a for loop to simplify and especially to
understand the aim and the power of loops.
int a;
clrscr();
for (a = 1; a < 11 ; a++)
printf ("%u\n", a);
ngetchx();
For 10 numbers, it is not really
faster but if you want to display 100 numbers, it is a compulsory or else you
are stubborn and stupid. ;:) You can view the code:
// C Source File
// Created 23/12/2002; 18:01:29
#define USE_TI89 // Produce .89z File
#define USE_TI92PLUS // Produce .9xz File
#define OPTIMIZE_ROM_CALLS // Use ROM Call Optimization
#define SAVE_SCREEN // Save/Restore LCD Contents
#include <tigcclib.h> // Include All Header Files
// Main Function
void _main(void)
{
clrscr();
printf ("1\n2\n3\n3\n4\n5\n6\n7\n8\n9\n10");
ngetchx();
int a;
clrscr();
for (a = 1; a < 11 ; a++)
printf ("%u\n", a);
ngetchx();
}
On the other hand, you should be
careful because you shouldn’t modify the loop variable in the statement loop. It
is very dangerous but the compiler does not forbid you it.
Generally, when you deal with a
number of data, you use arrays because it is more convenient. As we have not
seen the arrays yet, I will show you later an example of data deal with arrays
and loops, it is very great!
2) The “while” loop
I continue with this loop because the
C language prefers this loop or rather the code writes “while” to make an
“until” loop. This next example is the same display than the precedent example
with the for loop.
// C Source File
// Created 23/12/2002; 18:34:10
#define USE_TI89 // Produce .89z File
#define USE_TI92PLUS // Produce .9xz File
#define OPTIMIZE_ROM_CALLS // Use ROM Call Optimization
#define SAVE_SCREEN // Save/Restore LCD Contents
#include <tigcclib.h> // Include All Header Files
// Main Function
void _main(void)
{
int a = 1;
clrscr();
while (a < 11) {
printf ("%u\n", a);
a++;
}
ngetchx();
}
As you can see, you can use a for or
a while loop but you should choose the loop which has the simplest for your
algorithm. In fact, one is more readable but it is not true for each algorithm,
you should choose your way to program. Now, we can see the syntax:
statement
}
On the other hand, you should be careful. You mustn’t forget the first assignment “a = 1” or else the loop makes anything and especially the loop goes on and never stops!!! This loop is more dangerous because you can easily forget this assignment. Indeed, this assignment does not belong to the syntax of the loop unlike the for loop. Nevertheless, you should not avoid this loop because of this little inconvenient and you can also make mistakes with a complex for loop. Remember that you can make several tries before finding the good algorithm. If the loop never stops, you stop easily the emulator and you search the mistake, it is simple, isn’t it? That is why I advise you to use an emulator rather your calculator to make your try; to stop an emulator is easier than to take out the batteries when your program crashes. In addition, you can use the function of the emulator: “revert to saved state”; you do not need to start again the emulator!
The applications of this loop are the same ones with any loops. The big differences between the for and while loop is that you control the counter more easily but all loops can make the same work you should think more, it is all! So as to improve, you should play around this two loops and other knowledges. It is the best way to improve efficiently.
3) The “until” loop
In the C language, there is not the
keyword: “until” that is the reason why I told you that there is not really this
loop in C. I call this loop ‘until’ but it is a mistake because the meaning of
‘until’ is false. In fact, only the location of the test changes compare to the
‘while’ loop. These both loops are twin what means you can change very more
easily than with for loop. An example is always welcome.
// C Source File
// Created 23/12/2002; 19:15:20
#define USE_TI89 // Produce .89z File
#define USE_TI92PLUS // Produce .9xz File
#define OPTIMIZE_ROM_CALLS // Use ROM Call Optimization
#define SAVE_SCREEN // Save/Restore LCD Contents
#include <tigcclib.h> // Include All Header Files
// Main Function
void _main(void)
{
int a = 1;
clrscr();
do {
printf ("%u\n", a);
a++;
} while (a < 11);
ngetchx();
}
You can note that the test to check
up the end of the loop is at the end and not at the beginning like the “while”
loop. In the others languages like “Pascal”, when the test is at the end, the
loop is called “until” loop and the keyword: “until” is written and means
“until” and not “while”. The syntax of this loop is:
Statement
} while (condition)
4) Some keywords used in the loops: “break”
and “continue”
There are two key words which
especially belong to loops. They are “break” and “continue”. Their syntax is
very simple, it is the key word only and you add the semi-colon: ‘;’ as usual
but these keywords had to be within the statement of the loop.
“break” is able to stop the loop even
if the condition to stop the loop is false and you can put anywhere within the
statement of the loop. That depends on the result you want.
“continue” has an inverted effect in
comparison to “break”. Indeed, “continue” is able to stop the statement of the
loop, the loop condition is checked and the loop continues to the start of the
loop. That is simple, isn’t it? ;-)
These both methods are very powerful
to simplify your life but (yeah there is always a “but”) it is very dangerous.
Because you can easily make a loop which never stops or always stops and the
compiler goes on the following code.
Let us see a brief example, it is
only to increase your understanding.
1: // C Source File
2: // Created 23/12/2002; 19:38:02
3:
4: #define USE_TI89 // Produce .89z File
5: #define USE_TI92PLUS // Produce .9xz File
6:
7: #define OPTIMIZE_ROM_CALLS // Use ROM Call Optimization
8:
9: #define SAVE_SCREEN // Save/Restore LCD Contents
10:
11: #include <tigcclib.h> // Include All Header Files
12:
13: // Main Function
14: void _main(void)
15: {
16: int go_on = 1; // this variable is a boolean: TRUE=1 & FALSE=0
17: int a = 0;
18:
19: clrscr();
20:
21: do {
22: a++;
23: if (a == 13) {
24: continue;
25: break;
26: }
27: if (a == 17) {
28: break;
29: continue;
30: }
31: printf ("%d\n", a);
32: } while (go_on);
33:
34: ngetchx();
35: }
When you start this program, you can see that the number: ‘13’ and ‘17’ are not
displayed. I think you can understand why when you read this code. You can
notice that the lines: 25 and 29 are not read by the compiler, they are useless.
You can also notice that if they do not use these two new keywords, the loop
never stops because the variable: “go_on” is always true and does not change
within the loop.
To conclude, the loops are very
powerful and you can deal with a great amount of data without writing many lines
of code. As usual, you should play around these notions you have already seen.
It is the best method to improve and understand the tips of programming. Until
now we have seen the main notions of programming which are the loops.
See another lesson
Retour au menu