Lesson 2: Statements, Expressions and Operators

During this lesson, we remind ourselves of the definition of a C++ statement, before introducing the new concept of an expression. The greater part of the lesson is then used discussing arithmetic operators.

Sourcecode

The following source code is used during this video.

#include <iostream>

int main()
{
    // this is a statement:
    std::cout << "This output is the result of a statement." << std::endl;

    // each of these sums is an expression
    std::cout << 4 + 5 << ' ' << 7 - 1 << ' ' << 3.14 * 7.0 << ' ' << 4.0 / 7.3 << std::endl;
    // they can be more complex
    std::cout << 4 - (5 * 6) / 2 << std::endl;

    // take care mixing integers and decimal numbers, and dividing integers
    std::cout << 5 / 3 << ' ' << 4.0 * 5 << std::endl;

    // introducing the modulus operator (integers only!)
    std::cout << 4 % 3 << ' ' << 41 % 8 << std::endl;

    return 0;
}

Exercises

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 72 other followers

%d bloggers like this: