C-language break and continue statement usage

Source: Internet
Author: User

In C language, break and continue are jump out of the loop. They can jump out of the for, while, And do while loop. Next I will introduce the usage of break and continue statements in C language.

1break statement
Break statements are usually used in loop statements and switch statements. When a break is used to switch a switch statement, the program can jump out of the switch and execute the statement after the switch. If there is no break statement, it will become an endless loop and cannot exit. The usage of break in the switch has been met in the previous example when we introduced the switch statement. Here we will not give an example.

When the break statement is used in do-while, for, and while loop statements, the program can terminate the loop and execute the statements following the loop. Generally, the break statement is always associated with the if statement. That is, the loop jumps out when the conditions are met.
[Example 6.8]

The Code is as follows: Copy code
Main ()
{
Int I = 0;
Char c;
While (1)/* Set loop */
{
C = '';/* variable Initial Value */
While (c! = 13 & c! = 27)/* Receives characters on the keyboard until you press ENTER or the Esc key */
{
C = getch ();
Printf ("% cn", c );
}
If (c = 27)
Break;/* judge to exit the loop if the Esc key is pressed */
I ++;
Printf ("The No. is % dn", I );
}
Printf ("The end ");
}

Note:

The break statement does not work for the if-else Condition Statement.
In a multi-tier loop, a break statement only jumps one layer outward.
6.1.2 continue statement
The continue statement is used to forcibly execute the next loop by skipping the remaining statements in the loop. The continue statement is only used in loop bodies such as for, while, and do-while. It is often used together with the if Condition Statement to accelerate the loop. The execution process can be expressed.
1) while (expression 1)
{......
If (expression 2) break;
......
}
 
2) while (expression 1)
{......
If (expression 2) continue;
......
}
 
[Example 6.9]

The Code is as follows: Copy code

Main ()
{
Char c;
While (c! = 13)/* loop if it is not a carriage return */
{
C = getch ();
If (c = 0X1B)
Continue;/* If you press the Esc key and do not output it, perform the next loop */
Printf ("% cn", c );
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.