int main () {//2. Control flow//2.1 if statement//1. if (expression) {}//2.if (expression) {}else{}//3. There can be 0 or more else if, and the last else can omit if (expression) {Cx}else if (expression) {}else{}//2.2 switch statement//1. Single-line code switch (n) {case 0:printf ("000000"); Break;case 1:printf ("111111"); Break Case 2:printf ("222222"); Break;default:break; }//2. Multiple lines of code switch (n) {case 0:{printf ("000000"); printf ("000000"); Break }case 1:{printf ("111111"); printf ("000000"); Break } Case 2:{printf ("222222"); printf ("000000"); Break }default:break;} 2.3 Loop structure//1.while loop while (expression) {//dosomething}//2.for loop for (int i=0;i<count;i++) {//dosomethi NG}//3. Nested loops for (int i=0;i<count;i++) {for (int j=0;j<othercount;j++) {}}//4. Control loop structure//break is the direct jump out of the loop, the end of the loop, directly start Something outside the loop for (int i=0;i<10;i++) {if (i==9) {break;}} Continue is the statement that ends the loop, starting a new loop for (int i=0;i<10;i++) {if (i==1) {continue;} printf ("Hehheheh");} Return is not intended to end the loop. is used to return a function, but it can also be used to end a loop//end loop When a return is encounteredfor (int i=0;i<10;i++) {if (i>2) {return 0;} }//2.4 Goto statement, come in less or no//generally also used in nested loops inside for (int i=0;i<count;i++) {for (int j=0;j<othercount;j++) {if (i> ; 5) {goto outer; }}} outer:printf ("End nested");//3. Array//3.1 definition array Tyoe arrayname[lenth]; When the array evaluates the address, the address of the//element = First address + the memory size of the array variable * index//3.2 array assignment int arr[2] = {n}; 3.3 Using the array printf ("%d", arr[0]); Iterates through the array elements of the base type for (int i=0,length = sizeof (arr)/sizeof (arr[0]), i<length;i++) {NSLog (@ "arr[%d]:%d", I,arr[i]); }//Traversal element type is pointer type array element, NSString *arrstring = {@ "ready", @ "haha haha"}; for (int i=0,length = sizeof (arrstring)/sizeof (arrstring[0]), i<length;i++) {NSLog (@ "arr[%d]:%@", I,arrstring[i]); }//output ready haha null NULL//default assignment is null int arr[5]; arr[0]=12; arr[1]= 23; for (int i=0,length = sizeof (arr)/sizeof (arr[0]), i<length;i++) {NSLog (@ "arr[%d]:%@", I,arr[i]); }//Output 12 23 0 0 0//3.4 Multi-dimensional array type arrayname[length][length]; float arr[3][4]; int iarr[3][5]; Object-c's TwoThe essence of a dimension array is a one-dimensional array, except that its array element is a one-dimensional array arr[0]://the array again contains arr[0][0] arr[0][1]//Two-dimensional array assignment int arr2[3][4] = {{2,3,4,4},{2,4,5,6},{ 3,4,5,6}}; Use nested loops to traverse two-dimensional arrays for (int i=0,length = sizeof (ARR2)/sizeof (arr[0]), i<lenth;i++) {for (int j = 0,len=sizeof (Arr[i])/size Of (Arr[i][0]); j<len;j++) {printf ("%d", arr2[i][j]); }}//3.5 character array, string with end flag//define and initialize array char carr[] = {' I ', ' L '}; Iterate over the string array for (int i = 0,length=sizeof (CARR)/sizeof (Carr[0])) {NSLog (@ "%c", Carr[i]); } char str[] = "I love Ios"; printf ("%s\n", str); Char str[20] = "I love Ios"; printf ("%s\n", str); }
Attach a use case for a two-bit array C language version Gobang
#include <stdio.h> #include <stdlib.h> #include <time.h> #define no_chess "╋" #define Black_chess "" # Define white_chess "0" #define Board_size 15//defines the size of the checkerboard static char * board[board_size][board_size];//defines a two-dimensional array to act as a checkerboard void Initboard () {int i,j; Assign each element to the checkerboard output for (i=0;i<board_size;i++) {for (j = 0;j<board_size;j++) {Board[i][j] = no_chess;}} void Printboard () {int i,j;//assigns each element to the checkerboard output for (i=0;i<board_size;i++) {for (j = 0;j<board_size;j++) {printf ("%s", BOARD[I][J]);} printf ("\ n");}} int main () {Initboard (); Printboard (); while (1) {int xpos=0; int ypos=0; printf ("Please enter the coordinates you want to play, in X, y format: \ n"); scanf ("%d,%d", &xpos,&ypos); printf ("%d,%d", Xpos,ypos); BOARD[XPOS-1][YPOS-1] = white_chess; Generates a random number for playing chess position time_t T; Srand ((unsigned) time (&t));/* initializes the random function */int pcx; PCX = rand ()%board_size; int pcy; Pcy = rand ()%board_size; Board[pcx][pcy] = black_chess; Printboard (); } return 0;}
iOS Getting Started Tutorial (ii)-Control flow