当前位置:网站首页>Part I - Fundamentals of C language_ 4. Procedure flow structure

Part I - Fundamentals of C language_ 4. Procedure flow structure

2022-07-19 09:32:00 qq_ forty-three million two hundred and five thousand two hundr

4.1 summary

C The language supports three basic program running structures : Sequential structure Selection structure Loop structure .

  1. Sequential structure : The program is executed in sequence , No jump .
  2. Selection structure : According to whether the conditions are met , Selectively perform corresponding functions .
  3. Loop structure : According to whether the conditions are met , Loop executes a piece of code many times .

4.2  Selection structure

4.2.1 if sentence

 

#include <stdio.h>

int main()
{
	int a = 1;
	int b = 2;

	if (a > b)
	{
		printf("%d\n", a);
	}

	return 0;
} 

4.2.2 if…else sentence

 

#include <stdio.h>
int main()
{
	int a = 1;
	int b = 2;

	if (a > b)
	{
		printf("%d\n", a);
	}
	else
	{
		printf("%d\n", b);
	}
	return 0;
}

4.2.3 if…else if…else sentence

 

#include <stdio.h>

int main()
{
	int a;
	scanf("%u", &a);

	if (a < 10)
	{
		printf(" bits \n");
	}
	else if (a < 100)
	{
		printf(" ten \n");
	}
	else if (a < 1000)
	{
		printf(" Hundred bit \n");
	}
	else
	{
		printf(" It's big \n");
	}

	return 0;
}

4.2.4 Ternary operator

Grammar format :

Judge the condition ? sentence 1: sentence 2

If the condition is true, execute the statement 1, Otherwise, execute the statement 2.

#include <stdio.h>

int main()
{
	int a = 10;
	int b = 20;
    int c;
	
	c = ( a > b ? a : b );
	printf("c2 = %d\n", c);

	return 0;
}

4.2.5 switch sentence

Grammar format :

switch( The number )

     case ( Conditions 1):

         ( Execute statement 1)

         break;

    

     case ( Conditions 2):

          ( Execute statement 2)

         break;

     .

     .

     .

     default:

          ( Execute statement n)

}

#include <stdio.h>

int main()
{
	char c;
	c = getchar();

	switch (c)                           // Parameter can only be integer ( Include character type ) Variable 
	{
	case '1':
		printf("OK\n");
		break;                           //switch encounter break It was interrupted , Otherwise, the following statement will continue to be executed 
	case '2':
		printf("not OK\n");
		break;
	default:                             // If none of the above conditions are met , Then perform default
		printf("are u ok?\n");
	}
	return 0;
}

4.3 Loop structure

4.3.1 while sentence

 

#include <stdio.h>

int main()
{
	int a = 20;
	while (a > 10)                  // If the conditions are met, the cycle will continue 
	{
		scanf("%d", &a);
		printf("a = %d\n", a);
	}

	return 0;
}

4.3.2 do…while sentence

 

#include <stdio.h>

int main()
{
	int a = 1;
	do
	{
		a++;
		printf("a = %d\n", a);
	} while (a < 10);                   //do...while The loop is to execute first and then judge 

	return 0;
}

4.3.3 for sentence

#include <stdio.h>

int main()
{
	int i;
	int sum = 0;
	for (i = 0; i <= 100; i++)      // If the conditions are met, the cycle will continue 
	{
		sum += i;

	}

	printf("sum = %d\n", sum);

	return 0;
}

4.3.4 Nested loop

Loop statements can be nested with each other :

#include <stdio.h>

int main()
{
	int num = 0;
	int i, j, k;

	for (i = 0; i < 10; i++)
	{
		for (j = 0; j < 10; j++)
		{
			for (k = 0; k < 10; k++)
			{
				printf("hello world\n");
			}
		}
	}

	return 0;
}

4.4 Jump statements breakcontinuegoto

4.3.1 break sentence

stay switch Conditional statements and Loop statement You can use break sentence :

  1. When it appears in switch When in a conditional statement , To terminate a case And jump out of switch structure .
  2. When it appears in a loop statement , Role is Jump out of the current inner loop statement , Execute the following code .
  3. When it appears in a nested loop statement , Jump out of the latest inner loop statement , Execute the following code .
#include <stdio.h>

int main()
{
	int i = 0;
	while (1)
	{
		i++;
		printf("i = %d\n", i);

		if (i == 10)
		{
			break;                     // Jump out of while loop 
		}
	}

	return 0;
}

4.3.3 goto sentence ( Jump unconditionally , Use less as far as possible )

#include <stdio.h>

int main()
{
	goto End;                           // Jump unconditionally to End The logo of 
	printf("aaaaaaaaa\n");

End:
	printf("bbbbbbbb\n");

	return 0;
}

原网站

版权声明
本文为[qq_ forty-three million two hundred and five thousand two hundr]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/200/202207171023056396.html