How to shrink your stomach

How to shrink your stomach created by jack federbroke helpd me to loss weight so i want to feedback him for that thank you jack! How to shrink your stomach

Saturday, February 25, 2012

C Language Tutorial 3. Program Control

3.1 The While Loop

The C programming language has several structures for looping and conditional branching. We
will cover them all in this chapter and we will begin with the while loop. The while loop
continues to loop while some condition is true. When the condition becomes false, the looping
is discontinued. It therefore does just what it says it does, the name of the loop being very

Load the program while.c and display it for an example of a while loop.

/* This is an example of a "while" loop */
main( )
int count;
count = 0;
while (count < 6) {
printf("The value of count is %d\n",count);
count = count + 1;

We begin with a commentand the program name, then go on to define an integer variable "count"
within the body of the program. The variable is set to zero and we come to the while loop itself.
The syntax of a while loop is just as shown here. The keyword "while" is followed by an
expression of something in parentheses, followed by a compound statement bracketed by braces.
As long as the expression in parentheses is true, all statements within the braces will be executed.

In this case, since the variable countis incremented by one every time the statements are executed,
and the loop will be terminated. The program control will resume at the statement following
the statements in braces.

We will cover the compare expression, the one in parentheses, in the next chapter. Until then,
simply accept the expressions for whatyou think they should do and you will probably be correct.
Several things must be pointed out regarding the while loop. First, if the variable count were
initially set to any number greater than 5, the statements within the loop would not be executed
at all, so it is possible to have a while loop that never is executed. Secondly, if the variable were
not incremented in the loop, then in this case, the loop would never terminate, and the program
would never complete. Finally, if there is only one statement to be executed within the loop, it
does not need braces but can stand alone.
Compile and run this program.

3.2 The Do-While Loop

A variation of the while loop is illustrated in the program dowhile.c, which you should load
and display.

/* This is an example of a do-while loop */
main( )
int i;
i = 0;
do {
printf("the value of i is now %d\n",i);
i = i + 1;
} while (i < 5);

This program is nearly identical to the last one except that the loop begins with the reserved
word "do", followed by a compound statement in braces, then the reserved word "while", and
finally an expression in parentheses. The statements in the braces are executed repeatedly as
long as the expression in parentheses is true. When the expression in parentheses becomes false,
execution is terminated, and control passes to the statements following this statement.

Several things must be pointed out regarding this statement. Since the test is done at the end of
the loop, the statements in the braces will always be executed at least once. Secondly, if "i",
were not changed within the loop, the loop would never terminate, and hence the program would
never terminate. Finally, just like the while loop, if only one statement will be executed within
the loop, no braces are required. Compile and run this program to see if it does what you think
it should do.

It should come as no surprise to you that these loops can be nested. That is, one loop can be
included within the compound statement of another loop, and the nesting level has no limit.

3.3 The For Loop

The "for" loop is really nothing new, it is simply a new way of describe the "while" loop. Load
and edit the file named forloop.c for an example of a program with a "for" loop.

/* This is an example of a for loop */
main( )
int index;
for(index = 0;index < 6;index = index + 1)
printf("The value of the index is %d\n",index);

The "for" loop consists of the reserved word "for" followed by a rather large expression in
parentheses. This expression is really composed of three fields separated by semi-colons. The
first field contains the expression "index = 0" and is an initializing field. Any expressions in
this field are executed prior to the first pass through the loop. There is essentially no limit as
to what can go here, but good programming practice would require it to be kept simple. Several
initializing statements can be placed in this field, separated by commas.

The second field, in this case containing "index < 6", is the test which is done at the beginning
of each loop through the program. It can be any expression which will evaluate to a true or
false. (More will be said about the actual value of true and false in the next chapter.)
The expression contained in the third field is executed each time the loop is executed but it is
not executed until after those statements in the main body of the loop are executed. This field,
like the first, can also be composed of several operations separated by commas.

Following the for( ) expression is any single or compound statement which will be executed as
the body of the loop. A compound statement is any group of valid C statements enclosed in
braces. In nearly any context in C, a simple statement can be replaced by a compound statement
that will be treated as if it were a single statement as far as program control goes. Compile and
run this program.

3.4 The If Statement

Load and display the fileifelse.cfor an example of our first conditional branching statement,
the "if".

/* This is an example of the if and if-else statements */
int data;
for(data = 0;data < 10;data = data + 1) {
if (data == 2)
printf("Data is now equal to %d\n",data);
if (data < 5)
printf("Data is now %d, which is less than 5\n",data);
printf("Data is now %d, which is greater than 4\n",data);
} /* end of for loop */

Notice first, that there is a "for" loop with a compound statement as its executable part containing
two "if" statements. This is an example of how statement can be nested. It should be clear to
you that each of the "if" statements will be executed 10 times.

Consider the first "if" statement. It starts with the keyword "if" followed by an expression in
parentheses. If the expression is evaluated and found to be true, the single statement following
the "if" is executed. If false, the following statement is skipped. Here too, the single statement
can be replaced by a compound statement composed of several statements bounded by braces.
The expression "data" == 2" is simply asking if the value of data is equal to 2, this will be
explained in detail in the next chapter. (Simply suffice for now that if "data = 2" were used in
this context, it would mean a completely different thing.)

3.5 Now For The If-Else

The second "if" is similar to the first, with the addition of a new reserved word, the "else",
following the first printf statement. This simply says that, if the expression in the parentheses
evaluates as true, the first expression is executed, otherwise the expression following the "else"
is executed. Thus, one of the two expressions will always be executed, whereas in the first
example the single expression was either executed or skipped. Both will find many uses in your
C programming efforts. Compile and run this program to see if it does what you expect.

3.6 The Break And Continue

Load the file named breakcon.c for an example of two new statements.

main( )
int xx;
for(xx = 5;xx < 15;xx = xx + 1){
if (xx == 8)
printf("in the break loop, xx is now %d\n",xx);
for(xx = 5;xx < 15;xx = xx + 1){
if (xx == 8)
printf("In the continue loop, xx is the now %d\n",xx);

Notice that in the first "for" there is an if statement that calls a break if xx equals 8. The break
will jump out of the loop you are in and begin executing the statements following the loop,
effectively terminating the loop. This is a valuable statement when you need to jump out of a
loop depending on the value of some results calculated in the loop. In this case, when xx reaches
8, the loop is terminated and the last value printed will be the previous value, namely 7.

The next "for" loop, contains a continue statement which does not cause termination of the loop
but jumps out of the present iteration. When the value of xx reaches 8 in this case, the program
will jump to the end of the loop and continue executing the loop, effectively eliminating the
printf statement during the pass through the loop when xx is eight. Compile and run the program
to see if it does what you expect.

3.7 The Switch Statement

Load and display the file switch.c for an example of the biggest construct yet in the C
language, the switch.

main( )
int truck;
for (truck = 3;truck < 13;truck = truck + 1) {
switch (truck) {
case 3  : printf("The value is three\n");
case 4  : printf("The value is four\n");
case 5  :
case 6  :
case 7  :
case 8  : printf("The value is between 5 and 8\n");
case 11 : printf("The value is eleven\n");
default : printf("It is one of the undefined values\n");
} /* end of switch */
} /* end of the loop  */

The switch is not difficult, so don’t let it intimidate you. It begins with the keyword "switch"
followed by a variable in parentheses which is the switching variable, in this case "truck". As
many cases as desired are then enclosed within a pair of braces. The reserved word "case" is
used to begin each case entered followed by the value of the variable, then a colon, and the
statements to be executed.

In this example, if the variable "truck" contains the value 8 during this pass of the switch
statement, the printf will cause "The value is three" to be displayed, and the "break" statement
will cause us to jump out of the switch.

Once an entry point is found, statements will be executed until a "break" is found or until the
program drops through the bottom of the switch braces. If the variable has the value 5, the
statements will begin executing where "case 5 :" is found, but the first statements found are
where the case 8 statements are. These are executed and the break statement in the "case 8"
portion will direct the execution out the bottom of the switch. The various case values can be
in any order and if a value is not found, the default portion of the switch will be executed.

It should be clear that any of the above constructs can be nested within each other or placed in
succession, depending on the needs of the particular programming project at hand.
Compile and run switch.c to see if it does what you expect it to after this discussion.

3.8 The Goto Statement

Load and display the file gotoex.c for an example of a file with some "goto" statements in

int dog,cat,pig;
goto real_start;
printf("This is another line of the mess.\n");
goto stop_it;
/* the following section is the only section with a useable goto */
for(dog = 1;dog < 6;dog++) {
for(cat = 1;cat < 6;cat++) {
for(pig = 1;pig < 4;pig++) {
printf("Dog = %d  Cat = %d  Pig = %d\n",dog,cat,pig);
if ((dog + cat + pig) > 8 ) goto enough;
enough: printf("Those are enough animals for now.\n");
/* this is the end of the section with a useable goto statement */
printf("\nThis is the first line out of the spaghetti code.\n");
goto there;
printf("This is the third line of the spaghetti code.\n");
goto some_where;
printf("this is the second line of the spaghetti code.\n");
goto where;
printf("This is the last line of the mess.\n");

To use a "goto" statement, you simply use the reserved word "goto", followed by the symbolic
name to which you wish to jump. The name is then placed anywhere in the program followed
by a colon. You are not allowed to jump into any loop, but you are allowed to jump out of a
loop. Also, you are not allowed to jump out of any function into another. These attempts will
be flagged by your compiler as an error if you attempt any of them.

This particular program is really a mess but it is a good example of why software writers are
trying to eliminate the use of the "goto" statement as much as possible. The only place in this
program where it is reasonable to use the "goto" is the one in line 17 where the program jumps
out of the three nested loops in one jump. In this case it would be rather messy to set up a
variable and jump successively out of all three loops but one "goto" statement gets you out of
all three.

Some persons say the "goto" statement should never be used under any circumstances but this
is rather narrow minded thinking. If there is a place where a "goto" will be best, feel free to use
it. It should not be abused however, as it is in the rest of the program on your monitor.

Entire books are written on "gotoless" programming, better known as Structured Programming.
These will be left to your study. One point of reference is the Visual Calculator described in
Chapter 14 of this tutorial. This program is contained in four separately compiled programs
and is a rather large complex program. If you spend some time studying the source code, you
will find that there is not a single "goto" statement anywhere in it. Compile and run gotoex.c
and study its output. It would be a good exercise to rewrite it and see how much more readable
it is when the statements are listed in order.

3.9 Finally, A Meaningful Program

Load the file named tempconv.c for an example of a useful, even though somewhat limited
program. This is a program that generates a list of centigrade and Fahrenheit temperatures and
prints a message out at the freezing point of water and another at the boiling point of water.

/* This is a temperature conversion program written in */
/* the C programming language. This program generates */
/* and displays a table of farenheit and centigrade */
/* temperatures, and lists the freezing and boiling */
/* of water */
main( )
int count; /* a loop control variable */
int farenheit; /* the temperature in farenheit degrees */
int centigrade; /* the temperature in centigrade degrees */
printf("Centigrade to farenheit temperature table\n\n");
for(count = -2;count <= 12;count = count + 1 ){
centigrade = 10 * count;
farenheit = 32 + (centigrade * 9)/5;
printf("  C =%4d   F =%4d  ",centigrade,farenheit);
if (centigrade == 0)
printf(" Freezing point of water");
if (centigrade == 100)
printf(" Boiling point of water");
  }  /* end of for loop */

Of particular importance is the formatting. The header is simply several lines of comments
describing what the program does in a manner the catches the readers attention and is still
pleasing to the eye. You will eventually develop your own formatting style, but this is a good
way to start.

Also if you observe the for loop, you will notice that all of the contents of the compound statement
are indented a few spaces to the right of the "for" reserved word, and the closing brace is lined
up under the "f" in "for". This makes debugging a bit easier because the construction becomes
very obvious.

You will also notice that the "printf" statements that are in the "if" statements within the big
"for" loop are indented three additional spaces because they are part of another construct. This
is the first program in which we used more than one variable. The three variables are simply
defined on three different lines and are used in the same manner as a single variable was used
in previous programs. By defining them on different lines, we have opportunity to define each
with a comment.

3.10 Another Poor Programming Example

Recalling uglyform.c from the last chapter, you saw a very poorly formatted program. If
you load and display dumbconv.c you will have an example of poor formatting which is much
closer to what you will actually find in practice. This is the same program as tempconv.c
with the comments removed and the variable names changed to remove the descriptive aspect
of the names. Although this program does exactly the same as the last one, it is much more
difficult to read and understand. You should begin to develop good programming practices

main( )
int x1,x2,x3;
printf("Centigrade to Farenheit temperature table\n\n");
for(x1 = -2;x1 <= 12;x1 = 1){
x3 = 10 * x1;
x2 = 32 + (x3 * 9)/5;
printf("  C =%4d   F =%4d  ",x3,x2);
if (x3 == 0)
printf("Freezing point of water");
if (x3 == 100)
printf("Boiling point of water");

Compile and run this program to see that it does exactly what the last one did.

3.11 Programming Exercises

1. Write a program that writes your name on the monitor ten times. Write this program three
times, once with each looping method.

2. Write a program that counts from one to ten, prints the values on a separate line for each,
and includes a message of your choice when the count is 3 and a different message when the
count is 7.

next lesson


No comments:

Post a Comment

App creators your key for sucsses

If you can click a mouse, you can
profit from online video!
Recent news articles show how some YouTubers are earning upwards of $100,000 per year from the ads they display on and around their videos...

What if you could earn money from those ads, without actually creating the content?


All you need to know in one place

Small step for huge knowledge
This is the place you learn everything you need to move forward and become the leader in programming and creating mobile apps.

No need to be an expert anyone can start from scratch !

You just need to find the area that interests you and access, from there the way to success is up to you .

Good Luck!

 Click Here App Creators

App creators


i want to know

hello everyone

i wanted to ask from the viewers

if ther any subject thet you read and want continue reading and its not completed yet please let me know by comment or email

or if ther any subject thet you want me to add pls let me know