Explain our code so far in Triple X!

What does our preprocessor directive do?
Tells the processor to call on a library of pre-written code.

Why do we have a main function? What does it’s return statement do?
The main function is the starting point to all code and is required as a starting point to return too.

Explain our expression and declaration statement that we have written so far, to summarise what our program is doing.

The expression is in our case is the text displayed to instruct the operator how to proceed,

The declaration is telling the compiler what we want it to do in our case make our named variables act as a number and also change that number using math followed by displaying it.

The preprocessor directive lets the compiler know which library or header file to use when executing the program.

We always have a main function because its the MAIN function of the program. The compiler automatically knows to look for this function within the body of the code and will not execute unless this is in the code. The main() acts as the hub of the program, executing the code from top to bottom.

The return value of zero in the main() is to let the compiler know that the program has executed correctly and can now be terminated.

The expression statements in the code that has been written so far are iostream commands, such as cout and endl. They display strings or data we have instructed the compiler to display and also the end line code.

The declaration statements we have written are currently in const int, which is a constant integer, thus making the declarations essentially private so they can not be changed during runtime by the compiler or the user. The compiler protects this code for us. We have three const int declarations for the code numbers to be guessed by the user, and two const int statements for the mathematical procedures (one addition and one multiplication).

Triple X Code Breakdown

  1. Preprocessor Directives
    These statements take precedence above other statements in the fact that the compiler processes this code before other statements. Hence, “pre- processor directives”.

    The #include statement specifically tells the director to include a header file before compilation, creating a reference to existing code. (For the C# people, this is like a using statement.)

  2. The main() Function
    The main() function serves as the entry point for the program. During compilation, the compiler will look for it. When an executable is run, this function will be executed immediately. If it doesn’t exist in the code, an executable file will not be produced.

    main(), like all functions, has a signature

    int main()
    

    where int is our return type and main is the name of the method

    …and a body, which contains more statements.

    {
        ...
    }
    
  3. Expression Statements
    I like to think of expression statements as basic commands for a computer to execute. It can be printing text to the screen or assigning values to variables.

    std::cout << "Hello World!" << std::endl;
    

    OR

    b = 5;
    
  4. Declaration Statement
    Declaration statements tell the compiler to declare variables to store values in from various calculations performed in expression statements. As said before, declared variables can be given a value (a.k.a. initialized) in a declaration statement. Although, there are some instances in which one may want to leave a variable uninitialized or blank.

    Additionally, a variable can be declared as const, after which point the variable cannot be assigned to again. If one tries to do this, the compiler won’t compile the code.

  5. Return Statement
    A return statement is called inside of a function and tells the computer to exit the function. Whenever a function has a return type in its method signature (remember item 2 on this list), a value is specified after the word return.

    return 0;
    

    In the case of main(), when the return statement is called, the program stops.

1 Like

The preprocessor directive gathers the needed information in order to run the program that follows.
The main function essentially houses the program and gives it its form.
Expression statements give instruction to the user and give feedback depending on the variables at play.
The Declaration Statement houses the parameters of the program and gives the user markers as boundaries.
The return statement is the program’s close.

the preprocessor adds code (idk how to explain it)
the main function is where the code starts
im not quite sure about the expression but the decleration declars things

  1. What does out pre-processor directive do?
    It loads built-in code that allows us to perform input and output operations.

  2. Why do we have a main() function? What does its return statement do?
    As you pointed out, it is a requirement of all c++ programs, without it, the program will not compile, but it is also the starting point for program execution. The return 0; statement signals the successful completion of the main() function.

  3. Explain our expression and declaration statement that we have written so far, to summarise what our program is doing.

  • Our expression statements call ‘cout’ from the ‘standard’ namespace. Again, this loads built-in code that allows us to output to the terminal.
    The first expression statement outputs the game’s introductory text and prompts the player to enter a code.
    The second expression statement outputs the values of ‘sum’ and ‘product’.

  • Our declaration statements declare and initialise the variables we wish to use, while also carrying out some basic mathematical operations (resulting in the declaration and initialisation of ‘sum’ and ‘product’).

What does our preprocessor directive do?

The preprocessor directive is used to include other code or libraries for the program to use.

Why do we have a main function? What does it’s return statement do?

We have a main function because it is required for the program to start. When the program is executed, the main function is the first thing that is looked for. Also, the return statement is used to return a value when the function is done executing. In this case since main() is of type int, we return 0 (or any other int) when the function is finished.

Explain our expression and declaration statement that we have written so far, to summarize what our program is doing.

The declaration statements in our program are used to create variables that we need to use throughout the program. The created variables have been declared as the type int and were each assigned a number as it’s value. The were also variables created to hold the value of the calculations (using operators) done with the three int variables. The expression statements in this program are used to print out the results to the terminal and to calculate different operations.

Loads code from a designated library before being compiled.

The main function determines what parameters to compile and to give control to those parameters.

  • Text is printed for the user to see
  • The variables are being compiled and assigned a variable value
  • The values are being calculated to find the “sum” and “product” totals
  • The “sum” and “product” are being displayed as text with line spacing

#include loads an already written code to our code
main function is the center from which the code executes
declaration statements help to declare the variable’s content
expression statements helps to display and retrieve information from user.
return statement is used to get a feedback of the error. Generally if 0, the code has no error.

The preprocessor directive is used to import code that other programmers have already written, to make our lives easier and do things that otherwise wouldn’t be possible. In this case we had to #include iostream even just to print strings (“Hello, World!”) to the terminal.

The main function is like the main body of the program, where the other elements of our code have to be included in order for the compiler to recognize those elements as part of the program. This is where are expression and declaration statements go before the return statement.

An expression statement is any expression enclosed by a semicolon, so they could either be any string we want to print to the terminal or the declaration of a variable like
a = 5;

Then on the other hand there are declaration statements like int a = 4 or const int b = 7 that we need the compiler to keep track of before we can use them. I don’t really understand the distinction between an expression and a declaration statement, since neither one is necessarily printed/not printed to the terminal and they both have to end in a semicolon(?) So if I’m not mistaken (and I probably am) a line like int a = 4;
is both an expression AND a declaration statement, whereas
std::cout << “Hello, World!”;
is just an expression statement because it isn’t declaring anything to the terminal.

The return statement checks for errors, it needs to return 0 or else the program didn’t run properly. It also seems like the formal conclusion to the main body function, the same way you add a period to the end of a sentence when you want to conclude a statement.

The preprocessor directive loads a library of code that’s already been written, which main uses to function.

The main function is required for the program to run – it’s the main code. The return statement gives back a 0 for the main function to show it ran without error.

Our first expression statements set strings of text to display, explaining the story and the game.

    std::cout << "\n*****\n";
    std::cout << "You work at Puppy 911 Emergency Services. You receive an EMERGENCY ALERT.\n";
    std::cout << "A puppy's beloved chewing shoe -- which he's definitely allowed to chew on -- has been locked away!\n";
    std::cout << "You must CRACK THE SECURITY CODE.\n";
    std::cout << "*****\n";

Then, the declaration statements declare integer variables a, b, c, sum, and product. Eventually for the code puzzle.

    const int a = 4;
    const int b = 6;
    const int c = 8;

    const int sum = a + b + c;
    const int product = a * b * c;

Finally, more expression statements display certain variables within the terminal.

    std::cout << std::endl;
    std::cout << sum << std::endl;
    std::cout << product << std::endl;  

So much new vocab, have mercy. Jesus take the wheel.

What does our preprocessor directive do?

Points to a library of precompiled code that we can draw on.

Why do we have a main function? What does it’s return statement do?

The main function defines what our program will do, and the variables it contains. Return closes our program after running it without errors.

Explain our expression and declaration statement that we have written so far, to summarise what our program is doing.

Our expressions instruct the user on what to do, and they also output to the user a few manipulations of our variables.

Our declarations define what these variables are.

1 Like

Preprocessor Directive: It is an instruction to the compiler to simply paste whatever is present in the libraries mentioned to our file so that it can be used with ease.

Main Function: It is the starting point of the program which is the first to be approached by the compiler. It establishes a workflow.

Expression Statements: They are simply a broad category of statements which end with a semicolon and express something. The definition is hard to put concisely for me though…

Declaration Statements : They are kind of a subcategory to expression statements where something is being declared (variables in our case)

Return: The main function returns 0 as an indication that the whole program has been successfully compiled and that there is no issue within it. (0 because it has an int type and also maybe because 0 is positive in binary).

I may have been wrong in many places, so please correct me. This is the first time i have been exposed to coding. I’m still in the 10th Grade.

preprocessor directive:

  • includes the code from the and instructs the compiler to use it

main function:

  • in order to successfully compile, the cpp file must have a main function
  • return statement instructs the compiler to compile successfully if the return statement has no errors

expression statements:

  • used to describe the purpose of the function body and describe what the variables are doing

declaration statements;

  • creates variables which can be used within the function body

Include includes other code written by other programmers to make our lives easier
main is the function that the compiler looks for when running the game
the expression statements are simply expressions
declaration is where we declare various parts of our code.

it let us to add some libraries to our script, and those libraries will compiled first

main is a special function that every program must have it, otherwise it will not run
return statement is returning out the value to user/ program

our expression does writes string on console and show it and variable’s value to user, declaration statement is defining datatype of variable

What does our preprocessor directive do?

loads another developer’s code

Why do we have a main function? What does it’s return statement do?

A main function, I think it’s like the title of our work. It helps organize our code. The return statement is a way to verify a successful code.

Explain our expression and declaration statement that we have written so far, to summarise what our program is doing.

Our expression statements are what will be seen by players. Our declaration codes are the values of our expression statements.

Our preprocessor directive loads a library prior to our main function.

The main function is so the code will run and the return statement is to determine if there are any errors or not.

Our expression statements are allowing us to write strings which are then expressed visually in our program.

Our declaration statements are giving our variables defined values or equations which can then be used in declaration statements.

The preprocessor directive loads a library that is necessary for our code to be understood and compiled.

The main function is the first function to be read by the compiler, it’s necessary in every c++ program.

A expression statement contains information to be seen by the player.

A declaration statement stores information to be used by the game.

The return statement assures that our code is running without errors as long as it return 0.

What does our preprocessor directive do?

Preprocessor directive is the code that pulls info from another library to be used in the current file.

Why do we have a main function?

Main function exists to act as a beginning call out for statements in the code that live inside the {curly brackets}. In other words it is where the program will start when executed.

What does it’s return statement do?

The return statement acts as a final check that the code is error free and will tell the program what to do next, after it has ran all expression statements up to that point.

Explain our expression and declaration statement that we have written so far, to summarize what our program is doing.

  1. We are sending text to the user.
  2. We are declaring variables and initializing them for the program.
  3. We are expressing sum/product values.
  4. We are finishing the program and closing.

Privacy & Terms