I also want to learn C ++ _ Chapter 2: integer expressions

Source: Internet
Author: User
Tags integer division

 

Chapter 1

C ++'s learning finally started, and I was very excited in my heart. in the previous chapter, I learned to define some variables and then made some simple operations on these variables, and some of these simple operations are associated with some structure control and the basic methods for compiling errors. In this chapter, I will learn the most basic arithmetic operators.

Read code

What does this code mean ?! I have read one row:

Row 8-9: I initialize the value of sum and count as 0. There is a bracket behind the variable. You can write any value of the declared variable type, and this value can also be a constant. If you do not want to initialize the variable value, you must delete the parentheses!

Row 10th: Declares the variable X. Why didn't I initialize it here, because std: cin> x. In general, declares that one variable should be initialized for it. Because the uninitialized value may be a junk value, it is not safe. However, this is safe.

Row 14-15: Addition and assignment are used. I know that one INT type variable has a size. What should I do if the input value causes overflow.

Row 18th: output the average value of the input value.

I tried to run this program:

Good! This result is what I expected, hey! However, if we do not enter anything, what will happen if we directly type list0301 <NUL program ?! Give it a try, because we have to take into account any situation to write a program!

Depressed, just like this! Isn't the divisor 0 allowed in C ++ ?! Yes, or how can the program be like this! What should we do! How can I modify the program? Since C ++ does not allow the divisor to be 0, I will check that if it is 0, it will not allow the program to perform Division operations! How can I check it? Use if to make a conditional structure control, and rewrite the program now!

Well, now! In order to detect the program, I also enter a few pieces of data to run and play:

Strange! What's going on here? 2 + 5 + 4 = 11 11/3 = 3! No! How to decimal places! C ++ threw away decimal places. After searching for relevant information, I learned that some other languages cannot use integer division or floating point Division operators, C ++ uses the same operator and then decides which division to use based on the context. If both are integers, the result is also an integer. For example, 5/3 and 4/3 are equivalent to 1.

Test the parity of Integers

  

This program is one step worse! Now we have to judge the parity! Just use the remainder! If it is an even number % 2 = 0, the odd number % 2 = 1! Yes! That's it!

This program is successful! I used it here! =, Can I use =! Of course you can, but the following printed information must be changed! I made a mistake when I was writing this program. I wrote = As = when I changed it. I didn't know what was going on at the beginning! Pay attention to this issue in the future.

Modify the print average Program

  I suddenly thought of a problem, that is, the program that just prints the average value. When count is 0, the program exits and exits without any prompts, I think this is really bad! One prompt should be given, so change the program now:

Well! It's almost the same now! Strong enough! I almost made another mistake. I almost habitually wrote count = 0 as count = 0! The compiler does not report an error,

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.