C Primer Plus learning _20 selection loop + nested loop + array introduction

Review the do while loop

Keywords : do while

Common format :

do

    statement

while ( expression );

Example :

do

    {scanf ("%d",&number);}

while ( number != 20 );

How to choose a loop

The first thing to decide is whether to choose an entry-conditioned loop or an exit-conditioned loop. Usually the entry condition loop is used more, for several reasons.

  1. It is generally better to test the conditions before executing the loop.
  2. The test is placed at the beginning of the program, the program is more unreadable
  3. In many applications, it is required to simply skip the entire loop if the test conditions are not met at the beginning.

So if you need to add an entry conditional loop, which one should you use? The answer is to use whatever you want.

In general, a for loop is appropriate when the loop involves initializing and updating variables, and a while loop is better in other cases. For the following conditions, a while loop is suitable:

    while( scanf ("%ld", &num) == 1))

For loops involving index counting, a for loop is more appropriate:

    for( count = 1; count <= 100; count++)

nested loop

Nested loops only contain one loop within another. Nested loops are often used to display data in rows and columns, that is, one loop processes all columns in a row and another loop processes all rows in a row.

/*rows1.c -- use loop nesting */
#include <stdio.h>
#define ROWS 6
#define CHARS 10
int main (void)
{
	int rows;
	char ch;
	
	for(rows = 0; rows < ROWS; rows++)
	{
		for(ch = 'A'; ch < 'A'+10; ch++)
		{
			printf("%c", ch);
		 }
		 printf("\n");
	}
	return 0;
}

The result will be this:

ABCDEFGHIJ
ABCDEFGHIJ
ABCDEFGHIJ
ABCDEFGHIJ
ABCDEFGHIJ
ABCDEFGHIJ

The first for loop in the program is called the outer loop, and the second for loop is naturally called the inner loop. Just know that the inner loop executes all the loops on each iteration of the outer loop.

In the previous program, the two loops are doing the same thing. You can also control the inner loop through the outer loop to complete different tasks each time.

/*rows2.c -- use loop nesting */
#include <stdio.h>
#define ROWS 10
#define CHARS 10
int main (void)
{
	int rows;
	char ch;
	
	for(rows = 0; rows < ROWS; rows++)
	{
		for(ch = ('A' + rows); ch < ('A'+ CHARS); ch++)
		{
			printf("%c", ch);
		 }
		 printf("\n");
	}
	return 0;
}

The result will be this:

ABCDEFGHIJ
BCDEFGHIJ
CDEFGHIJ
DEFGHIJ
EFGHIJ
FGHIJ
GHIJ
HIJ
IJ
J

Because the value of rows is added to 'A' in each iteration, the variable ch will be assigned a different value each time, and the principle can be understood by sorting out the order.


Introduction to Arrays

In many programs, numbers are important. Arrays can be a convenient way to store multiple related items. The book will explain it in detail in the later chapters. In the end, this is just an introduction, because the later programs will encounter some arrays, so let's understand them in advance.

An array (array) stores a series of values ​​of the same type in order, such as 10 characters of type char or 15 values ​​of type int or any other type.

float debts [ 20 ] ;

This declaration states that this is an array of type float called debts, containing 20 elements, which are sequentially stored in adjacent locations in memory. The first element is debts [ 0 ] and so on to debts [ 19 ], although declared as 20, this starts at 0 and goes all the way to 19.

You can assign values ​​to array elements, like this: debts[5] = 12.34 ; but if you mistype the subscript value in square brackets, such as debts[55] = 12.34; the compiler will not check whether the subscript is correct, but When running the program, this can cause data to be placed in places already occupied by other data, possibly corrupting program results or simply aborting. Another point, the subscript must be an integer.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324652749&siteId=291194637