C ++ basic tutorial-if statement

Source: Internet
Author: User

If statement

The if statement is also called a judgment statement, for example, whether it is correct or not, the ratio size, etc. Let's take a look at the exercise;

1. Start Geany

1) Click "Application-programming-Geany" in the menu to start Geany and create a c ++ source program;

2) Click the "file-save as" command in the menu and save the file to your own folder with "if" as the file name;

2. Enter the program code.

1) First, let's look at a traffic light. Enter a number. 1 indicates bright, 0 indicates off, the red light stops, and the green light goes ;;

2) In the following blue code area, enter the following code (press the Tab key to skip the grid, // The comment is next to it, you can skip it first );

Int hong = 0;

Cout <"input hong: (0 | 1 )";

Cin> hong;

If (hong = 1)

{

Cout <"ting"; // click the Tab key

}

Else // click the backspace key

{

Cout <"xing ";

}

3) The first section defines the variable and assigns the initial value,

The second part is a prompt and enter a number to the variable hong,

4) The third part is divided into two if and else parts. Either of them depends on the conditions in the brackets,

If the value of hong is 1, run cout <"ting"; otherwise, run the following line "cout <xing"

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.