Search

1.x — Chapter 1 summary and quiz

Quick Summary

A statement is a type of instruction that causes the program to perform some action. Statements are often terminated by a semicolon.

A function is a collection of statements that execute sequentially. Every C++ program must include a special function named main. When you run your program, execution starts at the top of the main function.

Preprocessor directives tell the compiler to perform a special task. In this chapter, we use them to #include <iostream>, which allows us to access the input/output routines in the standard library.

The rules that govern how elements of the C++ language are constructed is called a syntax. A syntax error occurs when you violate the grammatical rules of the language.

Comments allow the programmer to leave notes in the code. C++ supports two types of comments. Line comments start with a // and run to the end of the line. Block comments start with a /* and go to the paired */ symbol. Don’t nest comments.

You can use comments to temporarily disable lines or sections of code. This is called commenting out your code.

Data is any sequence of symbols that can be interpreted to mean something. A single piece of data, stored somewhere in memory is called a value.

A variable is a named piece of memory that we can use to store values. A variable’s name is called an identifier. In order to create a variable, we use a statement called a definition statement. When the program is run, each defined variable is instantiated, which means it is assigned a memory address.

A data type tells the compiler how to interpret a piece of data into a meaningful value. An integer is a number that can be written without a fractional component, such as 4, 27, 0, -2, or -12.

Copy assignment (via operator=) can be used to assign an already created variable a value.

Initialization can be used to give a variable a value at the point of creation. C++ supports 3 types of initialization: copy initialization, direct initialization, and uniform initialization.

You should prefer uniform initialization over the other initialization forms, and prefer initialization over assignment.

Although you can define multiple variables in a single statement, it’s better to define and initialize each variable on its own line, in a separate statement.

std::cout and operator<< allow us to output an expression to the console as text. std::endl outputs a new line character, forcing the console cursor to move to the next line. std::cin and operator>> allow us to get a value from the keyboard.

A variable that has not been given a value is called an uninitialized variable. Trying to get the value of an uninitialized variable will result in undefined behavior, which can manifest in any number of ways.

C++ reserves a set of names called keywords. These have special meaning within the language and may not be used as variable names.

A literal constant is a fixed value inserted directly into the source code. Examples are 5 and “Hello world!”.

An operation is a mathematical calculation involving zero or more input values, called operands. The specific operation to be performed is denoted by the provided operator. The result of an operation produces an output value.

Unary operators take one operand. Binary operators take two operands, often called left and right. Ternary operators take three operands.

An expression is a combination of literals, variables, operators, and function calls that are evaluated to produce a single output value. The calculation of this output value is called evaluation. The value produced is the result of the expression.

An expression statement is an expression that has been turned into a statement by placing a semicolon at the end of the expression.

Programming is hard, and your programs will rarely come out perfect (or close to it) the first time. Get your programs working first, then refine them into something great.

Quiz time

Question #1


What is the difference between initialization and assignment?

Show Solution

Question #2


When does undefined behavior occur? What are the consequences of undefined behavior?

Show Solution

Question #3


Write a program that asks the user to enter a number, and then enter a second number. The program should tell the user what the result of adding and subtracting the two numbers are.

The output of the program should match the following (assuming inputs of 6 and 4):

Enter an integer: 6
Enter another integer: 4
6 + 4 is 10.
6 - 4 is 2.

Show Solution


2.1 -- Introduction to functions
Index
1.10 -- Developing your first program

540 comments to 1.x — Chapter 1 summary and quiz

  • Logan

    Just one point, I did the question above and rather than declare the variable under two lines, I used one for num1 and num2; Is this okay? And if so, is there also a more concise way of doing ,"cin" in one line?

    The result below:
    Enter a number:
    4
    Enter a second number:
    8
    The result of 4 + 8 = 12
    The result of 4 - 8 = -4
    Press any key to continue . . .

    • > I used one for num1 and num2; Is this okay?
      No, this makes your code harder to read and can lead to mistakes. @num1 is uninitialized. The curly braces only apply to @num2.

      > is there also a more concise way of doing ,"cin" in one line?
      You'll learn about functions later. They can be used to wrap a call to @std::cin.operator>> so you can do this

      • Logan

        Thank you, yeah, I ran it separate and got the uninitialized message. Slight moment of madness. I think my crazy thinking was it would default both to zero. I tested it by then adding its own curly braces with amount inside, least that then worked, but as you say it looks messy.

        As for the cin, I am curious to what you mentioned up there, as opposed to using a Loop. To see if data can be placed into multiple variables without using a Loop. Just something I was wondering about. Thanks again!

  • Prateek

    I am trying to find area of a circle.

    #include <iostream>
    int main()
    {
    std::cout<<"Enter the radius of the circle : ";

    int radius{};                    // I just want user to enter an integral value of radius.

    std::cin>>radius;
    std::cout<<"Area of circle is "<< "π"<< "*"<< radius<<" *"<<radius<< "="<< π*radius*radius<<'\n';
    return 0;
    }

    I am getting a compilation error.
    What exactly is wrong in my program?

    • Nice

      Probably the fact that your compiler thinks π is an undefined identifier and not a number that can be multiplied.

    • no Name

      try this bro

      #include <iostream>
      #include <math.h>
      int main()
      {
      std::cout<<"Enter the radius of the circle : ";
      double radius{};
      double pi {16 * atan2(1.0, 5.0) - 4 * atan2(1.0, 239.0)};
      std::cin>>radius;
      std::cout<<"Area of circle with radius " << radius<< ' ' << "is " << pi*pow(radius,2)<<'\n';
      return 0;
      }

    • no Name

      wtf is this
      std::cout<<"Area of circle is "<< "π"<< "*"<< radius<<" *"<<radius<< "="<< π*radius*radius<<'\n';

      try to make like this
      #include <iostream>
      int main()
      {
      std::cout<<"Enter the radius of the circle : ";

      int radius{};                    // I just want user to enter an integral value of radius.
      double pi {3.1415926535897}; //pi value
      std::cin>>radius;
      std::cout<<"Area of circle with radius "<< radius << ' ' <<  "is: " << pi*radius*radius<<'\n';
      return 0;
      }

      • Prateek

        My problem was indeed silly,as I am just a beginner, and was trying to figure out a random simple question that came to my mind.
        I appreciate your efforts.
        Thank you

    • Alex

      This program has several things wrong with it. Namely, π hasn't been defined. Generally, you shouldn't use non-ASCII characters for variable names. So you should rename this to pi, and then define pi with a value. Or replace it with the literal value 3.14159.

  • Austin

    Hi thank you so much for the time and effort you put into these lessons! I've really enjoyed following along.

    I have a question about defining a variable and assigning a variable. I noticed at question 3 you defined the variable, used std::cout, then assigned the variable through user input.

    I was under the impression that it's better to assign a value to a variable maybe right after declaring it. Maybe this is inconsequential, but I was wondering what was considered "best practice". I think I remember it being said in the lessons that this structure is debated but that you recommended a certain way of doing it.

    I'd be happy to clarify myself if necessary. Thank you!

    Here is how I wrote it:

  • ptitBarbu

    Great course Alex, very well explained !
    Thank you for that.

Leave a Comment

Put all code inside code tags: [code]your code here[/code]