0% found this document useful (0 votes)
3 views5 pages

Coding Notes

The document provides an introduction to the fundamentals of C++, covering variables, data types, operators, and the basic structure of a C++ program. It explains the execution process of code, how to use input/output functions, and the importance of comments. Additionally, it details arithmetic, relational, and logical operators, along with examples of type conversion and user input.

Uploaded by

s65501350
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Coding Notes

The document provides an introduction to the fundamentals of C++, covering variables, data types, operators, and the basic structure of a C++ program. It explains the execution process of code, how to use input/output functions, and the importance of comments. Additionally, it details arithmetic, relational, and logical operators, along with examples of type conversion and user input.

Uploaded by

s65501350
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

🧩 Introduction

 The lecture covers the foundation of C++:

o Variables (storing data)

o Data Types (what kind of data we store)

o Operators (performing operations)

 Basic structure of a C++ program and its execution process.

 Reminder: C++ is case-sensitive (e.g. cout ≠ Cout).

💻 Output in C++

 cout is used to print text to the console.

Eg- cout << "Hello World";

 Use endl or "\n" for a new line.

 Multiple outputs can be chained:

Eg- cout << "Sum = " << a + b << endl;

⚙️How Code Runstttt

 Steps: Source code → Compilation → Machine code → Execution

 The compiler checks syntax and converts .cpp code into a .exe or .out file.

 g++ [Link] && ./[Link] (Linux/Mac) compiles and runs together.

 Commands like clear (to clean terminal) and ↑ (to recall last command).

📁 Boilerplate Structure & Comments

Structure of a C++ Program:

#include <iostream>

using namespace std;

int main() {

-------

-------

return 0;

 #include <iostream> → adds input/output library.


 using namespace std; → allows use of cout, cin without std:: prefix.

 main() → entry point of every C++ program.

 return 0; → indicates successful execution.

Comments: parts of code that are not compiled and are used to explain the code

// single-line comment

/* multi-line

comment */

🧠 Variables

 A variable stores data that can change during execution.

 int age = 19;

 float pi = 3.14;

 char grade = 'A';

 Rules:

o Must start with a letter or underscore _

o No spaces or special symbols

o Case-sensitive

 Uninitialized variables contain garbage values.

 Stored in RAM, each variable has:

o Name

o Type

o Value

o Memory address

🔢 Data Types

🔹 Basic Data Types:

Type Size Example Description

int 4 bytes int age = 18; Whole numbers

float 4 bytes float pi = 3.14; Decimal (single precision)

double 8 bytes double bigPi = 3.141592; More precise decimal


Type Size Example Description

char 1 byte char grade = 'A'; Single character

bool 1 byte bool isTrue = true; True or false

🔹 sizeof() Operator

Finds how much memory a data type takes:

cout << sizeof(int); // 4

cout << sizeof(double); // 8

🔹 ASCII Concept

 Characters are stored as integer ASCII codes.


Example:

 char ch = 'A';

 cout << (int)ch; // prints 65

🔄 Type Conversion & Casting

1. Implicit Type Conversion

 Done automatically by compiler (type promotion):

 int a = 5;

 double b = 2.5;

 double sum = a + b; // a is promoted to double

2. Explicit Type Conversion

 Forced conversion by the programmer:

 double pi = 3.14159;

 int x = (int)pi; // x = 3

 Conversion from double → int truncates (not rounds).

 If one operand is double and the other is int, result becomes double.

⌨️Input in C++

 Use cin to take user input:

 int age;

 cout << "Enter your age: ";


 cin >> age;

 cout << "You are " << age << " years old";

 cin ignores spaces for basic types.

 To take multiple inputs:

 cin >> a >> b;

➕ 47:07 — Operators Overview

1. Arithmetic Operators

Operator Meaning Example Output NOTE->

+ Addition 5+3 8

- Subtraction 5-3 2

* Multiplication 5 * 3 15

/ Division 5/2 2 (int division)

% Modulus 5%2 1

2. Relational Operators

Used for comparisons; returns true (1) or false (0).


<, >, <=, >=, ==, !=

3. Logical Operators

Operator Description Example

&& Logical AND (a > 0 && b > 0)

` `

! Logical NOT !(a > b)

4. Assignment Operators

=, +=, -=, *=, /=, %=

a += 3; // same as a = a + 3;

🧮 Example: Sum of Two Numbers

Simple program demonstrating input, processing, and output:


#include <iostream>

using namespace std;

>> b;

cout << "Sum = " << a + b; int main() {

int a, b;

cout << "Enter two numbers: ";

cin >> a

return 0;

Increment and Decrement:

Operator Description Example Behavior

++a Pre-increment Increments before use Uses new value

a++ Post-increment Increments after use Uses old value

--a, a-- Similarly for decrement

Example:

int x = 5;

cout << ++x; // 6

cout << x++; // prints 6, then x = 7

Homework:

 Create a Calculator Program performing all 5 arithmetic operations (+ - * / %).

You might also like