Introduction to C language Programming--switch, break and continue

Source: Internet
Author: User

Switch, break, and continue keywords are often used in process control, and the use of these three keywords is listed here.


Break Example 1:

# include <stdio.h>int main (void) {int i;for (i=0;i<3;++i) {if (3>2) break;  The break here is used to terminate the FOR Loop statement, not the IF statement. The following printf statement is no longer executed and the output is empty. printf ("Haha! \ n ");} return 0;}


Break Example 2:

# include <stdio.h>int main (void) {int I, j;for (i=0; i<3; ++i) {for (j=1; j<4; ++j) break;//output is three "Oh! ", break can only terminate a loop closest to it. printf ("Hehe! \ n ");} return 0;}


Switch Example:

# include <stdio.h>int main (void) {int i;printf ("Please enter the number of floors you want:"), scanf ("%d", &i), switch (i) {case 1:printf (" The elevator will open on the 1 floor! \ n "); Break;case 2:printf (" The elevator will open on the 2 floor! ") \ n "); Break;case 3:printf (" The elevator will open on the 3 floor! ") \ n "); Break;case 4:printf (" The elevator will open on the 4 floor! ") \ n "); Break;case 5:printf (" The elevator will open on the 5 floor! ") \ n "); Break;case 6:printf (" The elevator will open on the 6 floor! ") \ n "); Break;case 7:printf (" The elevator will open on the 7 floor! ") \ n "); Break;case 8:printf (" The elevator will open on the 8 floor! ") \ n "); break;default:printf (" Sorry, I haven't covered this floor! ") \ n ");} /* Character connection: printf ("Elevator will be in");p rintf ("%d", i);p rintf ("Layer open! \ n "); */return 0;}


Continue example:

# include <stdio.h>int main (void) {int I, j;for (i=0; i<3; i++) {A; B;continue; Continue will c;d the remainder of the loop; skip to execute i++. C;d;} j = 0while (j<3) {A; b;j++;  Updating some J + + will not be skipped, otherwise it becomes a dead loop. Continue Continue will c;d the remainder of the loop; skip to execute i++. C;d;} return 0;}

Switch and break examples:

# include <stdio.h>int main (void) {int x = 1;int y = 0;int A = 0;int B = 0;switch (x)  //First switch{case 1:switch ( Y)  //second switch{case 0:a++;break;  Here the break terminates the switch that is closest to it, the second switch, and the output is a = 1 b = 100;
If the first switch is terminated, a = 1 B = 0;case 1:b++;break is output;} b = 100;break;case 2:a++;b++;break;} printf ("a =%d B =%d\n", A, b); return 0;}


Use conitnue to process illegal input:

# include <stdio.h>int main (void) {int I, J;char ch;printf ("Enter the value of I:"), scanf ("%d", &i);p rintf ("i =%d\n", i); while ((Ch=getchar ()) = ' \ n ')//The illegal character after I is processed. GetChar () is the character function, ' \ n ' is the carriage return. continue;printf ("Please enter the value of J:"), scanf ("%d", &j);p rintf ("j =%d\n", j); return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Getting Started with C programming--switch, break, and continue

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.