The structured statements in c ++ determine the use of if branch statements switch loop statements while and do while loop statements for, structured switch

Source: Internet
Author: User
Tags switch loop

The structured statements in c ++ determine the use of if branch statements switch loop statements while and do while loop statements for, structured switch

Job 1:

Use the if statement, according to 1 ~ 7. What is the day of the week output? Program.

Method 1: directly use a separate if statement

# Include <iostream> using namespace std; int main () {int numberWeek; cout <"enter a 1 ~ Integer between 7 "; cin> numberWeek; if (numberWeek = 1) {cout <" Today is Monday "<endl;} if (numberWeek = 2) {cout <"Today is Tuesday" <endl;} if (numberWeek = 3) {cout <"Today is Wednesday" <endl ;} if (numberWeek = 4) {cout <"Today is Thursday" <endl;} if (numberWeek = 5) {cout <"Today is Friday" <endl;} if (numberWeek = 6) {cout <"Today is Saturday" <endl ;} if (numberWeek = 7) {cout <"Today is Sunday" <endl;} return 0 ;}

Method 2: Use the associated if statement

# Include <iostream> using namespace std; int main () {int numberWeek; cout <"enter a 1 ~ Integer between 7 "; cin> numberWeek; if (numberWeek = 1) {cout <" Today is Monday "<endl;} else if (numberWeek = 2) {cout <"Today is Tuesday" <endl;} else if (numberWeek = 3) {cout <"Today is Wednesday" <endl ;} else if (numberWeek = 4) {cout <"Today is Thursday" <endl;} else if (numberWeek = 5) {cout <"Today is Friday" <endl;} else if (numberWeek = 6) {cout <"Today is Saturday" <endl ;} else if (numberWeek = 7) {cout <"Today is Sunday" <endl ;}else {cout <"the number you entered does not meet the requirements. "<Endl;} return 0 ;}

Exercise questions: Finish the seasonal output of one year and 12 months. If the output is in August, it is in summer.

Job 2:

Use the switch statement to complete the seasonal output for one year and 12 months. If it is output in August, This is the summer program.

# Include <iostream> using namespace std; int main () {int monthNumber; cout <"enter a 1 ~ Integer between 12 "; cin> monthNumber; switch (monthNumber) {case 3: case 4: case 5: cout <" this is Spring "<endl; break; case 6: cout <"this is Summer" <endl; break; case 7: case 8: cout <"this is Summer" <endl; break; case 9: case 10: case 11: cout <"this is Autumn" <endl; break; case 12: case 1: case 2: cout <"this is winter" <endl; break; default: cout <"the number you entered does not meet the requirements. "<Endl; break;} return 0 ;}

Use the switch statement to practice the use of arithmetic operators. Input two floating-point numbers at will and output the +-*/% of the two floating-point numbers.

Case: Can a statement be switched anywhere? Default: Can statements be written anywhere? Is the case statement empty? Yes.

Job 3:

Use the do while and while statements to output a rectangle consisting of *. The width and height of the rectangle must be 10 and 20.

 

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.