Variables and Operators in C++

Sep 16, 2024

Lecture 2 Notes

Subject: Variables, Data Types, and Operators

Introduction

  • Today we will discuss variables, data types, and operators.
  • See the channel's playlist for other DSA concepts.
  • Share lecture updates and progress on Twitter.

C++ Programming

  • Writing the first program in C++.
  • Producing output using cout.
    • Example: cout << "Hello World!";
  • C++ is a case-sensitive language.
  • The process of creating a folder to write code when using VS Code.

C++ Program Structure

  1. Main Function: int main()
    • Program starting point.
    • Main logic code is written here.
  2. Preliminary Directive: #include <iostream>
    • Necessary for input and output.
  3. For clarity: using namespace std;
    • Using the standard namespace.

Output Statements

  • Printing output using cout.
  • Use a semicolon to end every statement.
  • Use endl to move to a new line.

Variables and Data Types

  • Variable: A container to store data.
  • Different data types:
    • int (integer)
    • char (single character)
    • float (decimal number)
    • bool (true or false)
    • double (decimal number, for more precision)
  • Variable names must always start with a letter or an underscore.

Type Casting

  • Type Casting: Changing from a smaller data type to a larger data type.
    • Example: int new_price = (int) price;
  • Type Conversion: Automatic change, such as float to double.

Taking Input

  • Taking data from the user using cin.
    • Example: cin >> age;
  • Output: cout << "Your age is: " << age;

Operators

  1. Arithmetic Operators: +, -, *, /, % (modulo)
  2. Relational Operators: <, >, ==, !=, <=, >=
  3. Logical Operators: && (and), || (or), ! (not)
  4. Unary Operators: ++ (increment), -- (decrement)

Conclusion of Programming

  • A homework problem to create a simple calculator program.
  • A mix of input, operation, and output.

Conclusion

  • Covered principles of variables, data types, input, output, and operators in C++.
  • Understanding of the basic structure and logic of programming.

Homework

  • Create a calculator program that can perform all four operations (addition, subtraction, multiplication, division).
  • Confirm the success of Lecture 2 by sharing the date and what was learned in the comments.