C語言之嵌套循環

在前面的幾篇文章之中,寫到了for,while,do while循環的基本用法,for循環用法鏈接:https://blog.csdn.net/qqj3066574300/article/details/105038846  while循環用法鏈接:https://blog.csdn.net/qqj3066574300/article/details/105039377  do while循環用法鏈接:https://blog.csdn.net/qqj3066574300/article/details/105040121  而本篇文章主要寫的內容是嵌套循環,相對於前面幾篇文章來說,代碼內容是複雜一些,但對於嵌套循環,可以這樣子理解,循環中再加一個循環,基本的結構如下:

    for(條件){
        for(條件)

代碼塊
    }

下面的內容就以兩個案例爲解析,如有疑問的,可以私聊本人或者到網上查看基本資料。

代碼案例:

#include <stdio.h>
#include <stdlib.h>
#define ROWS 6
#define CHSRS 10

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(){
	int row;
	int ch;
	
	for(row = 0;row < ROWS;row++){
		for(ch = 1;ch < (1 + CHSRS);ch++)
		printf("%d",ch);
	printf("\n");
	}
	return 0;
}

運行結果;

代碼案例:

#include <stdio.h>
#include <stdlib.h>
#define ROWS 6
#define CHSRS 10

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(){
	int row;
	char ch;
	
	for(row = 0;row < ROWS;row++){
		for(ch = 'A';ch < ('A' + CHSRS);ch++)
			printf("%c",ch);
		printf("\n");
	} 
	return 0;
}
 

運行結果: 

 如上就是兩個簡單的嵌套循環案例,其實代碼還是很簡單的,可能對於初學者而言,其中有兩個疑問,#define是什麼?代碼還可以這樣子寫?(for(ch = 'A';ch < ('A' + CHSRS);ch++))。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章