Chapter 6 : Control Statements : Looping

Using for for flexbility

Although the for loop looks similar to the FORTRAN DO loop, the Pascal FOR loop, and the BASIC FOR...NEXT loop, it is much more flexible than any of them. This flexibility stems from how the three expressions in a for specification can be used. The examples so far have used the first expression to initialize a counter, the second expression to express the limit for the counter, and the third expression to increase the value of the counter by 1. When used this  way, the C for statement is very much like the others we have mentioned. However, there are many more possibilities; here are nine variations:

  • You can use the decrement operator to count down instead of up:

/* for_down.c */
#include <stdio.h>

int main(void) {
    int secs;

    for (secs = 5; secs > 0; secs--) 
        printf("%d seconds!\n", secs);
    printf("We have ignition!\n");
    return 0;
}

Here is the output:

5 seconds!

4 seconds!

3 seconds!

2 seconds!

1 seconds!

We have ignition!

 

  • You can count by twos, tens, and so on, if you want:
/* for_13s.c */
#include <stdio.h>

int main(void) {
    int n; // count by 13s from 2

    for (n = 2; n < 60; n = n + 13)
        printf("%d \n", n);
    return 0;
}

 

This would increase n by 13 during each cycle, printing the following:

2

15

28

41

54

 

  • You can count by characters instead of by numbers:
    /* for_char.c */
    #include <stdio.h>
    
    int main(void) {
        char ch;
        for (ch = 'a'; ch <= 'z'; ch++)
            printf("The ASCII value for %c is %d.\n", ch, ch);
        return 0;
    }
    

    The program assumes the system uses ASCII code for characters. Here’s the abridged output:

    The ASCII value for a is 97.

       The ASCII value for b is 98.

        ...

       The ASCII value for x is 120.

       The ASCII value for y is 121.

       The ASCII value for z is 122.    

      The program works because characters are stored as integers, so this loop really counts by integers anyway.

 

  • You can test some condition other than the number of iterations. In the for_cube

program, you can replace

for (num = 1; num <= 6; num++)

with

for (num = 1; num * num * num <= 216; num++)

 You would use this test condition if you were more concerned with limiting the size of the cube than with limiting               the number of iterations.

  • The first expression need not initialize a variable. It could, instead, be a printf() statement of some sort. Just remember that the first expression is evaluated or executed only once, before any other parts of the loop are executed.
    /* for_show.c */
    #include <stdio.h>
    
    int main(void) {
        int num = 0;
        for (printf("Keep entering numbers!\n"); num != 6;)
            scanf("%d", &num);
        printf("That's the one I want!\n");
        return 0;
    }
    

This fragment prints the first message once and then keeps accepting numbers until you enter 6:

Keep entering numbers!

3

5

8

6

That's the one I want!

  • The parameters of the loop expressions can be altered by actions within the loop. For example, suppose you have the loop set up like this:
    for (n = 1; n < 10000; n = n + delta)
    If after a few iterations your program decides that delta is too small or too large, an if statement (see Chapter 7, “C Control Statements: Branching and Jumps”) inside the loop can change the size of delta. In an interactive program, delta can be changed by the user as the loop runs. This sort of adjustment is a bit on the dangerous side; for example, setting delta to 0 gets you (and the loop) nowhere.

In short, the freedom you have in selecting the expressions that control a for loop makes this loop able to do much more than just perform a fixed number of iterations. The usefulness of        the for loop is enhanced further by the operators we will discuss shortly.


Using Functions with return Values

Declaring the function, calling the function, defining the function, using the return

keyword—these are the basic elements in defining and using a function with a return value.

At this point, you might have some questions. For example, if you are supposed to declare functions before you use their return values, how come you used the return value of scanf() without declaring scanf()? Why do you have to declare power() separately when your defini- tion of it says it is type double?

Let’s take the second question first. The compiler needs to know what type power() is when it first encounters power() in the program. At this point, the compiler has not yet encoun- tered the definition of power(), so it doesn’t know that the definition says the return type is double. To help out the compiler, you preview what is to come by using a forward declaration.

This declaration informs the compiler that power() is defined elsewhere and that it will return type double. If you place the power() function definition ahead of main() in the file, you can omit the forward declaration because the compiler will know all about power() before reach- ing main(). However, that is not standard C style. Because main() usually provides the overall framework for a program, it’s best to show main() first. Also, functions often are kept in sepa- rate files, so a forward declaration is essential.

Next, why didn’t you declare scanf()? Well, you did. The stdio.h header file has function declarations for scanf(), printf(), and several other I/O functions. The scanf() declaration states that it returns type int.


Arrays consist of adjacent memory locations all of the same type. You need to keep in mind that array element numbering starts with 0 so that the subscript of the last element is always one less than the number of elements. C doesn’t check to see if you use valid subscript values, so the responsibility is yours.

The prototype allows the compiler to see whether you’ve used the function correctly, and the definition sets down how the function works. The prototype and definition are examples of the contemporary programming practice of separating a program element into an interface and an implementation. The interface describes how a feature is used, which is what a prototype does, and the implementation sets forth the particular actions taken, which is what the definition does.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

「已注销」

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值