This is the 25th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

One, foreword

This article, as the basic knowledge of C language, introduces the usage, rules, and use cases of several common STATEMENTS of C language.

The statements are as follows:

if.elsestatementforLooping statementswhileLooping statementsdo.whileLooping statementsswitchstatementsgotostatementsreturnstatementsbreakstatementscontinuestatementsCopy the code

The second chapter introduces grammar rules and usage cases, and the third chapter lists some exercises for consolidating knowledge points with the sentences introduced in the second chapter.

Two, knowledge points and case codes

2.1 Syntax rules and use cases of if statements

If statement syntax:

/ / form 1
if(< conditional expression >)// Execute if the condition is true{... Execute code.. }2 / / form
if(< conditional expression >)// Execute if the condition is true{... Execute code.. }else  // Execute when condition is false (else block optional, can write or not write){}3 / / form
if(< conditional expression >)// Execute if the condition is true{... Execute code.. }else if(< conditional expression >)// Execute when condition is false (else block optional, can write or not write){... Execute code.. }else if(< conditional expression >) {... Execute code.. }... What code can be written in < conditional expression >? Written statements must return concrete results after execution. Such as:12>34Such as:if(printf("12345"))
Copy the code

Use case

#include <stdio.h>
int main(int argc,char *argv[])
{   
    int a=100;
    int b=20;
    int c=30;
    if(a>b)
    {
        printf("a>b\n");
    }

    if(a>b)printf("a>b\n");

    // Error prone area
    if(a==0)  //if(a=0)
    {
        printf("a>b a>c\n");
    }

     // Error prone area
    if(a&&b)//if(a&0)
    {
        printf("a>b a>c\n");
    }
    return 0;
}

/* comma, semicolon comma: interval symbol. For example: int a,b,c,d; ! && logic and logic, and | | - or & or logical bitwise and (3) & 1, address-of (& a) * multiplication sign (a * b), pointer (* a) and * (block comment) * /


#include <stdio.h>
int main(int argc,char *argv[])
{   
    int year;
    printf("Enter year :");
    scanf("%d",&year);

    // Common year and leap year conditions: divisible by 4 and not by 100 condition 2: divisible by 400
    if((year%4= =0 && year%100! =0) || (year%400= =0))
    {
        printf("Leap year \ n");
    }
    else
    {
        printf("Leap year \ n");
    }

   // Common year and leap year conditions: divisible by 4 and not by 100 condition 2: divisible by 400
    if(year%4= =0 && year%100! =0)
    {
        printf("Leap year \ n");
    }
    else if(year%400= =0)
    {
        printf("Leap year \ n");
    }
    else
    {
        printf("Leap year \ n");
    }
    return 0;
}
Copy the code

2.2 while, do.. While statement

    // loop statements
    while(< conditional expression >) {... Execute code.. }do{... Execute code.. }while(< conditional expression >);#include <stdio.h>
int main(int argc,char *argv[])
{   
    int a=5;
    int b=5;
    // loop statement a--, a=a-1, a-=1;
    while(a--)
    {
       printf("a=%d\n",a);/ / 4, 3, 2, 1, 0
    }

    // loop statements
    do
    {
       printf("a=%d\n",b);/ / \ \ \ \ \ 1 2 3 4 5\0
    }while(b--);
    
    return 0;
}
Copy the code

2.3 for statement

#include <stdio.h>
int main(int argc,char *argv[])
{   
    int a=0;
    / / write 1
    for(< initialization statement >; < conditional expression >; < increment/decrement >) {} For example:for(a=0; a<10; a++) { }/ / write 2
    for(; < conditional expression >; < increment/decrement >) {} For example:for(; a<10; a++) { }/ / writing 3
    for(;;)  // Condition is always true{} is equivalent towhile(1)// Condition is always true{}return 0;
}

#include <stdio.h>
int main(int argc,char *argv[])
{   
    // Print the 99 times table
    int i,j;
    for(i=1; i<=9; i++) {for(j=1; j<=i; j++) {printf("%dx%d=%2d ",i,j,i*j);
        }
        printf("\n");
    }
    return 0;
}
/* 1x1=1 2x1=1 2x2=4.... 3x1=1 3x2=6 3x3=9 ..... . * /
Copy the code

2.4 break statement

Jumps out of the current loop or switch statement.

#include <stdio.h>
int main(int argc,char *argv[])
{   
    int i,j,cnt=1;
    for(i=0; i<5; i++) {for(j=0; j<10; j++) {if(j==5)
            {
                break;  // Jump out of the current loop
            }
            printf("cnt=%d\n",cnt++);  / / 25}}return 0;
}
Copy the code

2.5 Goto Jump statement

You can set the label inside the function and jump at will.

#include <stdio.h>
int main(int argc,char *argv[])
{   
    int i,j,cnt=1;
    for(i=0; i<5; i++) {for(j=0; j<10; j++) {if(j==5)
            {
                goto AA;  // Jump statement
            }
            printf("cnt=%d\n",cnt++);  / / 5
        }
    }
AA:
    printf("Program completed.\n");
    return 0;
}

Copy the code

2.6 the continue statement

Break out of the loop and proceed to the next new loop.

#include <stdio.h>
int main(int argc,char *argv[])
{   
    int i,j,cnt=1;
    for(i=0; i<5; i++) {for(j=0; j<10; j++) {if(j==5)
            {
               continue; // Exit the current loop
            }
            printf("cnt=%d\n",cnt++); //}}return 0;
}

Copy the code

2.7 Switch Branch Statements (C89 and C99 standards)

#include <stdio.h>
int main(int argc,char *argv[])
{   
    int a;
    scanf("%d",&a);
    switch(a)
    {
    case 1:
        printf("Choose 1. \ n");
        break;
    case 2:
        printf("Choose 2. \ n");
        break;
    case 3:
        printf("Choose 3. \ n");
        break;
    case 4:
    case 5:
    case 6:
        printf("Select 4 and 6 \ n");
        break;
    default:
        printf("Select default values.\n");
        break;
    }
    return 0;
}
Copy the code

3. Exercises

3.1 Input integer and Output in binary Mode (Data transmission)

0x23; --- 8 -time00100011
#include<stdio.h>
int main(a)
{
    unsigned int a,i;
    int flag=0;
    printf("Input integer :");
    scanf("%d",&a);
    for(i=0; i<32; i++) {//if(a&0x1
        //if(a&0x80000000
        if(a&(1<<31)) // Start from the top
        {
            flag=1;
            printf("%d".1);
        }
        else
        {
            if(flag)
            {
                printf("%d".0);
            }
        }
        a<<=1; // Move the left side of a (high overflow, low fill 0)
    }
    printf("\n");
    return 0;
}
Copy the code

3.2 Print all daffodils.

A daffodil is a three-digit flower whose cube sum of the digits equals the number

#include <stdio.h>
int main(a)
{
    int i;
    int a, b, c;
    for (i = 100; i <= 999; i++)
    {
        a = i / 100;
        b = i % 100 / 10;
        c = i % 10 / 1;
        if ((a*a*a + b*b*b + c*c*c) == i)
        {
            printf("%d ", i); }}return 0;
}

Copy the code

3.3 hundred yuan for 100 chickens

The cock is 5 yuan each, the hen is 3 yuan each, the chick is 3 yuan, ask 100 yuan to buy 100 chickens there are several ways to buy.

#include<stdio.h>
int main(a)
{
    int m, g, x;
    int m_max;
    int g_max;
    int x_max;
    int q, cnt; // CNT = quantity q: money
    printf("The cocks are 5 yuan, the hens 3 yuan, and the chickens 3 yuan \n");
    printf("Please enter the number of chickens to buy :\n");
    scanf("%d", &cnt);/ / 100 100
    printf("Please enter the price of the chicken :\n");
    scanf("%d", &q);/ / 100 100
    /*1. Check whether the input data is reasonable */
    if (q>0 && cnt>0)
    {
        m_max = cnt / 3; // 母鸡 100 /3 =33
        g_max = cnt / 5; // cock 100/5 =20
        x_max = (cnt / 1) * 3; // Chicken 100/1 *3 =300
        for (g = 0; g<g_max; g++) // Loop judgment is possible
        {
            for (m = 0; m<m_max; m++)
            {
                x = cnt - g - m; // Total number - number of roosters - Number of hens = number of chicks
                if (x + g + m == cnt && x / 3 + g * 5 + m * 3 == q)
                // The total number of chickens is equal to the total number of chickens and the total amount of money is equal to the total number of chickens
                {
                    printf("The rooster = % d \ t", g);
                    printf("The hen = % d \ t", m);
                    printf("The chicken = % d \ t \ n", x); }}}}else
    printf("Input error \n");
    return 0;
}

Copy the code

3.4 Printing regular triangles

#include<stdio.h>
/* 1 123 1234321 */
int main(a)
{
    int i, j;
    int len;
    scanf("%d", &len);
    for (i = 1; i <= len; i++) / / the total number of rows
    {
        /*1. Print space */
        for (j = 1; j <= len - i; j++)
        {
            printf("");
        }
        /*2. Print the first half */
        for (j = 1; j <= i; j++)
        {
            printf("%d", j);
        }
        /*3. Print the last half */
        for (j = i - 1; j >= 1; j--)
        {
         printf("%d", j);
        }
        printf("\n");
    }
    return 0;
}

Copy the code

3.5 Printing Inverted Triangles

#include<stdio.h>
int main(a)
{
    int i, j;
    int len;
    scanf("%d", &len);
    for (i = len; i >= 1; i--) // The total number of cycles
    {
        /*1. Print space */
        for (j = len; j>i; j--)
        {
            printf("");
        }
        /*2. Print the front part */
        for (j = 1; j <= i; j++)
        {
         printf("%d", j);
        }
        /*3. Print the last part */
        for (j = i - 1; j >= 1; j--)
        {
            printf("%d", j);
        }
        printf("\n");
    }
    return 0;
}
Copy the code