简体   繁体   中英

will a break statment break out of a while loop and an other if loop?

I have a thread that logs serial data from a USB port. When the semaphore is posted and the variable serial_logging_running is 1 it proceeds to the while(serial_logging_running == 1) loop and continually reads the serial port and writs to file. When the program is exited the signal handler sets the variable serial_logging_running to 0 . Will the break statement exit the while() loop and the outside if (res == 0) and then proceed to return null statement?

static void *logging(void *param) {

    int rdlen, res;
    char ibuf[1024];

    sem_wait(&logging_semaphore); 

    /*Open log file and write to it from /dev/USB1*/
    create_open_log_file();
    res = log_dut_serial_data(serial_port);

    if (res) {
        fprintf(stderr,"Error opening the serial port%s\n",serial_port);
    }

    if (res == 0) {

        while(serial_logging_running == 1) {

            /*read from serial port write to log file*/
            rdlen = read(fd_joule, ibuf, sizeof(ibuf));

            if (rdlen > 0) {
                fwrite(ibuf, sizeof(char), rdlen, log_file);
                fflush(log_file);
            }

            if (rdlen < 0) {
                fprintf(stderr, "rdlen les than 0\r\n");
            }

            /* Exit the serial logging thread*/
            if (serial_logging_running == 0) {
                printf("Exiting serial logging thread\r\n");
                break;
            }


        }
    }

    close_serial_port_joule(); /*Exiting close the serial port*/

    return NULL;

}

will a break statement break out of a while loop and an other if loop?

The if blocks are not relevant. The break will "jump" to the end of the while () { ... } loop.

Yet in OP's case, the break will leave the if (serial_logging_running == 0) { } and jump to almost the end of the if (res == 0) { } .

if (res == 0) {
    while(serial_logging_running == 1) {
        ...
        if (serial_logging_running == 0) {
            ...
            break;  //  Jump to end of while loop
        }
    }
    // break "lands" here
}

Will the break statement exit the while() loop and the outside if (res == 0) and then proceed to return null statement?

No. Code flow will first proceed to end of the while loop. Yet since the if (res == 0) has no more code, code flow will then complete that if() . Then next code is close_serial_port_joule(); , then return NULL; .


Also note the break here is not needed. @Iharob Al Asimi

Understand the keyword break . It breaks out of the current last-initiated loop-block. An if -statement does not in any way generate a loop-block.

So yes, your program breaks out of the while loop at the break -statement and executes normally after that.

Break breaks the loop caused by while. There is no loop to break with if . So yes, the while is "terminated" and the programs flow towards the return NULL -statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM