[C++] Example scripts on variables, constants, operators and conditions

发布于 2024-03-18  730 次阅读


Please refresh the page if equations are not rendered correctly.
---------------------------------------------------------------

Example scripts on:

  1. variables (line 7),
  2. constants (line 26),
  3. operators (line 31) and
  4. conditions (line 50).

Code variables.cpp

#include <iostream>

using namespace std;

int main() {

    /* Difining Variables */
    cout << "Difining Variables:" << endl;  // cout - console output
    cout << "The following are some important data types in C++;" << endl;
    cout << "Character: char;" << endl;
    cout << "Integer (signed): int, long, double;" << endl;
    cout << "Floating point: float, double;" << endl;
    cout << "Boolean: bool;" << endl;

    // Now we are going to define some variables
    int age = 25; // it is a good practice to initialize the variables when they are defined
    float floatAge = 25.5;
    char charA = 'A';
    bool isAdult = true;

    cout << "Age: " << age << endl;
    cout << "Float Age: " << floatAge << endl;
    cout << "Character: " << charA << endl;
    cout << "Is Adult: " << isAdult << endl;

    cout << "defining constants:" << endl;
    const int myAge = 5;
    cout << "My Age: " << myAge << endl;
    // myAge = 6; // this will through an error, because myAge is a constant variable and it cannot be changed after it is defined.

    /* Operators */
    cout << "Operators:" << endl;
    // + - addition, - - subtraction, * - multiplication, / - division, % - modulus
    cout << "Arithmetic Operators: +, -, *, /, %;" << endl; 
    // == - equal to, != - not equal to, > - greater than, < - less than, >= - greater than or equal to, <= - less than or equal to
    cout << "Relational Operators: ==, !=, >, <, >=, <=;" << endl;  
    // && - and, || - or, ! - not
    cout << "Logical Operators: &&, ||, !;" << endl; 
    // & - bitwise and, | - bitwise or, ^ - bitwise xor, ~ - bitwise not, << - left shift, >> - right shift
    cout << "Bitwise Operators: &, |, ^, ~, <<, >>;" << endl; 
    // += - add and assign, -= - subtract and assign, *= - multiply and assign, /= - divide and assign, %= - modulus and assign
    cout << "Assignment Operators: =, +=, -=, *=, /=, %=" << endl; 
    // sizeof - returns the size of a variable, & - returns the address of a variable, * - pointer to a variable, 
    // ?: - conditional expression, . - member access operator, -> - member access operator, Cast - converts one data type to another, Comma - separate the expressions
    cout << "Miscellaneous Operators: sizeof, &, *, ?:, ., ->, Cast, Comma;" << endl; 

    size_t size = sizeof(int);
    cout << "Size of int: " << size << endl;

    /* Conditionals */
    cout << "Conditionals:" << endl;
    cout << "if, else if, else;" << endl;
    cout << "Input a number x=" << endl;
    int x = 0;
    cin >> x;
    cout << "The number you entered is: " << x << endl;    

    if (x > 0) {
        cout << "x is positive" << endl;
    } else if (x < 0) {
        cout << "x is negative" << endl;
    } else {
        cout << "x is zero" << endl;
    }

    cout << "if, else if, else with logical operators;" << endl;
    cout << "Input a number y=" << endl;
    double y = 0.0;
    cin >> y;
    cout << "The number you entered is: " << y << endl;
    if (y >= 10 && y <= 20) {
        cout << "y is between 10 and 20" << endl;
    } else if (y > 20 && y <= 30) {
        cout << "y is between 20 and 30" << endl;
    } else {
        cout << "y is not between 10 and 30" << endl;
    }

    return 0;
}

Makefile

build:
    g++ variables.cpp -o main

run:
    ./main

# clean is often used as a target that removes the output of other targets
clean:
    @del *.o
    @del main.exe

allrun:
    make build
    make run
    make clean
Everything not saved will be lost.
最后更新于 2024-03-18