当前位置:网站首页>C language learning

C language learning

2022-07-01 12:53:00 51CTO

                Branches and loops (1)

Text

One . Branch statements and loop statements

Branch statement

if       switch

Loop statement

while       for        do while       go to sentence

What is a sentence ?

c There is a semicolon in language (;) Separated by a statement .

      
      
; // Is the statement , Empty statement
  • 1.

1. Branch statement ( Selection structure )

1).if sentence

if The grammatical structure of a sentence :

if( expression )                    or                   if( expression )

sentence ;                              person                   sentence 1;

                                                           else

                                                            sentence 2;

Multiple branches

if( expression 1)

sentence 1;

else if( expression 2)

sentence 2;

else

sentence 3;

If the result of the expression is true , Then the statement executes (0 Said the false , Nonzero means true )

      
      
#include <stdio.h>
int main()
{
int age = 111;
if ( age < 18)
printf( " A minor \n");
else if ( age >= 18 && age < 20) //&& Representation logic and , If the result of the expression is true , Then the statement executes (0 Said the false , Nonzero means true )
printf( " teenagers \n");
else if ( age >= 20 && age < 50)
printf( " Prime of life \n");
else if ( age >= 50 && age < 90)
printf( " aged \n");
else
printf( " Old man does not die \n");
return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.

If the condition holds , To execute multiple statements , Code blocks should be used

#include <stdio.h>

int main()

{

if( expression )

{

Statement list 1;

}

else

{

Statement list 2

}

return 0;

}

      
      
#include <stdio.h>
int main()
{
int age = 111;
if ( age < 18)
{
printf( " A minor \n"); // Statement list 1
printf( " Can't fall in love \n"); // You can print multiple printf
}
else
{
if ( age >= 18 && age < 20)
printf( " teenagers \n");
else if ( age >= 20 && age < 50) // Statement list 2
printf( " Prime of life \n");
else if ( age >= 50 && age < 90)
printf( " aged \n");
else
printf( " Old man does not die \n");
}
return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.

there {} It's just a code block

In the air else

      
      
#include <stdio.h>
int main()
{
int n = 2;
int m = 2;
if ( 1 == n)
if( 2 == m) // Try to be with else For its
printf( " Hello \n");
else //else And the nearest unmatched if matching
printf( " You're so bad \n");

return 0;
}
The output result is null , Can run
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

// Use properly {} It can make the logic of the code clearer

// Code style is important

practice : Output 1~100 Between the odd numbers

      
      
#include <stdio.h>
int main()
{
int n = 1;
while( n < 100)
{
if( 1 == n % 2)
printf( "%d\n", n);
n ++;
}

return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

2).switch sentence

switch The grammatical structure of a sentence :

switch( Integer expression )

{

Statement item ;

}

What are statement items ?

// It's some case sentence

// as follows :

case Integral constant expression :

sentence ;

//switch There can be if sentence

      
      
#include <stdio.h>
int main()
{
int day = 0;
scanf( "%d", & day); //& Fetch address , Take out variables day
if ( 1 == day) // Judgment variable day Is it equal to 1, If yes, output on Monday
printf( " Monday \n");
else if( 2 == day)
printf( " Tuesday \n");
else if ( 3 == day)
printf( " Wednesday \n");
else if ( 4 == day)
printf( " Thursday \n");
else if ( 5 == day)
printf( " Friday \n");
else if ( 6 == day)
printf( " Saturday \n");
else if ( 7 == day)
printf( " Sunday \n");
return 0;
}
// use else if...else if More complicated , use switch Multi branch statements are relatively simple
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.

After the change

      
      
#include <stdio.h>
int main()
{
int day = 0; //day For integer variables
scanf( "%d", & day);
switch ( day) //day Is an integer expression
{
case 1: //case 1(1 Is a constant variable expression ):
printf( " Monday \n");
break; //break It means to end the cycle
case 2:
printf( " Tuesday \n");
break;
case 3:
printf( " Wednesday \n");
break;
case 4:
printf( " Thursday \n");
break;
case 5:
printf( " Friday \n");
break;
case 6:
printf( " Saturday \n");
break;
case 7:
printf( " Sunday \n");
break;
}

return 0;
}
// summary : Easy to understand
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.

stay switch Statement break:

stay switch In the sentence , We can't Branch , With break You can realize the real Branch .

practice : Input 1~5, Output “weekday”; Output 6~7, Input “weekend”

      
      
#include <stdio.h>
int main()
{
int day = 0;
scanf( "%d", & day);
switch( day)
{
case 1:
case 2:
case 3:
case 4:
case 5:
printf( "weekday\n");
break;
case 6:
case 7:
printf( "weekend\n");
break;
default: //default Can be placed in switch Anywhere , When switch The value of the expression is not equal to case The value of the tag , This is the time default The following statement will execute .
printf( " Input error \n");
break;
}
return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.

break The actual effect of the statement is to divide the statement list into different parts .

default Clause (default and case There is no order , It can be used anywhere )

If the expressed value is the same as all case What if the value of the tag is different ?

It's nothing , The result is that all statements are skipped ; The program will not terminate , No errors reported , Because this situation is c It is not suitable to report errors .

What if you don't want to ignore the values of expressions that don't match all tags ?

You can add an item to the statement list default Clause , Put the label below default: Write in any one case Where labels can appear ( When switch The value of the expression does not match all case The value of the tag , This default The statement after the clause will be executed . all , Every switch Only one... Can appear in the statement default Clause , But it can appear anywhere in the statement list , And the statement flow will run through case The label runs through default Clause .)

practice :

      
      
#include <stdio.h>
int main()
{
int n = 1;
int m = 2;
switch ( n)
{
case 1: m ++;
case 2: n ++;
case 3:
switch ( n)
{ //switch Allow nesting
case 1: n ++;
case 2: m ++; n ++; break;
}
case 4: m ++;
break;
default:
break;
}
printf( "m=%d,n=%d\n", m, n);
return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.

1. Loop statement

while       for     do while

1).while sentence

while Grammatical structure :(while sentence , Loop can be realized )

while( expression )

Loop statement ;

practice : Print... On the screen 1~10

      
      
#include <stdio.h>
int main()
{
int a = 1;
while( a <= 10)
{
printf( "%d\n", a);
a ++;
}
return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

break stop it

      
      
#include <stdio.h>
int main()
{
int i = 1;
while( i <= 10)
{
if( 5 == i)
break;
printf( "%d\n", i);
i ++;
}
return 0; // Print as 1,2,3,4
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

continue skip , Make the entry judgment of the next cycle

      
      
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main()
{
int i = 1;
while ( i <= 10)
{
if ( 5 == i)
continue;
printf( "%d\n", i); // Print 1,2,3,4, The cursor is still flashing ( The program is not over )
i ++;
}
return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

Conclusion :continue It means a dead cycle ( The cycle is not over )

原网站

版权声明
本文为[51CTO]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/182/202207011232349410.html