Nexus Learn

Lecture 2: Variables Data Types IO

Lecture Notes CS-307 Programming Fundamentals 10 min read Updated 8 Nov 2026

↓ Download notes (Word)

On this page
  1. Learning Outcomes
  2. 1. Where We Are
  3. 2. Variables
  4. 3. Data Types
  5. 4. Output with cout
  6. 5. Input with cin
  7. 6. Arithmetic Operators
  8. 7. Complete Programs
  9. 8. Common Mistakes
  10. 9. Word Bank (Key Terms)
  11. 10. Self-Check Questions

UNIVERSITY OF AGRICULTURE, FAISALABAD

Department of Computer Science

LECTURE 2 · STUDENT NOTES

Variables, Data Types & Input/Output

CS-307 Programming Fundamentals · BSCS Semester 1

Week

Slides

Linked practical

Reference ID

1

Lecture2_Variables_DataTypes_IO.pptx

Practical 2

N02

Learning Outcomes

SLO

By the end of this lecture you can…

Builds CLO

Bloom level

SLO 2.1

Declare and initialise variables using valid names

CLO4: Describe the syntax and semantics of the C++ programming language.

C3

SLO 2.2

Choose the right data type: int, float, double, char, bool, string

CLO4: Describe the syntax and semantics of the C++ programming language.

C3

SLO 2.3

Take input with cin and display output with cout

CLO4: Describe the syntax and semantics of the C++ programming language.

C3

SLO 2.4

Write complete programs that input, calculate and display results

CLO1: Identify the necessary properties of good problem-solving techniques.

C3

How to use these notes

Read these notes after the lecture and before the practical. Section numbers (for example §3.2) are used in the question bank, so you can always find where an answer comes from. Type every code example yourself — do not copy-paste.

1. Where We Are

In Lecture 1 we printed fixed text. Today our programs will store data and talk to the user: they will take input, calculate, and show results. This is the full IPO model in real C++. (Slide L02-S02)

2. Variables

2.1 What is a variable?

A variable is a named location in memory (RAM) that stores a value. The value can change (vary) while the program runs. (Slide L02-S05)

Real-world example: labelled boxes

Think of a variable as a labelled box. The label is the variable's name (age). The kind of box is the data type (int). What is inside is the value (19). A box for shoes cannot hold water — in the same way, an int box cannot hold text.

Name (label)

Type (kind of box)

Value (inside)

age

int

19

cgpa

float

3.45

grade

char

'A'

name

string

"Ali"

2.2 Declaring, initialising and assigning

(Slide L02-S06) The general form is: dataType name = value;

Action

Code

What happens

Declare

int marks;

A box is created. Its value is garbage (an unknown leftover value).

Initialise

int marks = 85;

The box is created and filled in one step.

Assign later

marks = 90;

The old value (85) is replaced by 90.

Many at once

int a = 5, b = 10;

Two int variables declared in one line.

= means ASSIGN, not "equals"

In maths, x = 5 means x is 5. In C++, x = 5; means put 5 into the box x. So x = x + 1; is correct C++: take x, add 1, and put the result back into x.

2.3 Rules for naming variables

(Slide L02-S07)

1. Use only letters, digits and underscore ( _ ).

2. Do not start with a digit.

3. No spaces or special symbols ( @ # - $ ).

4. Do not use a keyword (a word C++ already uses, such as int, return, float).

5. C++ is case-sensitive: Marks and marks are two different variables.

Name

Valid?

Reason

totalMarks

✓

Letters only, camelCase

student_age

✓

Underscore is allowed

_count

✓

May start with _

2ndName

✗

Starts with a digit

my age

✗

Has a space

float

✗

Keyword

cost$

✗

Special symbol

Good habit: use meaningful names in camelCase (first word small, next words start with a capital): totalMarks, not x.

3. Data Types

3.1 Basic data types

The data type decides what kind of value a variable can hold and how much memory it uses. (Slides L02-S08, S09)

Type

Stores

Example

Typical size

int

Whole numbers

int age = 19;

4 bytes

float

Decimals (about 7 digits)

float cgpa = 3.45;

4 bytes

double

Decimals (about 15 digits)

double pi = 3.14159265;

8 bytes

char

One character, in single quotes

char grade = 'A';

1 byte

bool

true or false

bool passed = true;

1 byte

string

Text, in double quotes

string name = "Ali";

varies

1 byte = 8 bits. Sizes can differ between systems; these are typical on lab PCs. string needs #include <string> (iostream usually brings it in, but include it to be safe).

3.2 Which type should I use?

Situation

Best type

Example

Counting things (students, items)

int

int students = 45;

Money, CGPA, averages

double

double price = 249.50;

A single letter or grade

char

char section = 'B';

A yes/no answer

bool

bool isHostelite = false;

Names, addresses, text

string

string city = "Faisalabad";

3.3 Constants and literals

A constant is a value that must never change after it is set. Use the const keyword, give it a value when you declare it, and write its name in UPPER_CASE. (Slide L02-S10)

const double PI = 3.14159;

const int MAX_MARKS = 100;

PI = 3.2; // ERROR! a const cannot be changed

A literal is a fixed value written directly in the code: 100, 3.14, 'A', "Ali", true. Real-life constants: PI, days in a week (7), maximum marks (100).

4. Output with cout

4.1 Chaining output

We can print several items in one statement by joining them with <<. (Slide L02-S11)

int age = 19;

string name = "Ali";

cout << "Name: " << name << endl;

cout << "Age: " << age << "\n";

Output

Name: Ali

Age: 19

Text in quotes is printed exactly. A variable name without quotes prints its value: cout << "age"; prints the word age, but cout << age; prints 19.

4.2 Escape sequences

An escape sequence is a backslash \ followed by a character, written inside double quotes. It gives a special instruction.

Code

Meaning

Example → Output

\n

New line (same job as endl)

"Hi\nBye" → Hi (new line) Bye

\t

Tab: jump to the next column

"A\tB" → A B

\"

Print a double quote

"He said \"OK\"" → He said "OK"

\\

Print a backslash

"C:\\Users" → C:\Users

5. Input with cin

5.1 Reading a value

cin (say "see-in", console input) reads a value typed by the user and stores it in a variable. (Slide L02-S12)

int age;

cout << "Enter your age: "; // prompt: tell the user what to type

cin >> age; // value typed by the user goes INTO age

Remember the arrows

cout << data — data goes out to the screen.

cin >> variable — data comes in from the keyboard to the variable.

Always print a prompt before cin, so the user knows what to type.

5.2 Reading several values

cin >> a >> b; reads two values. The user separates them with a space or by pressing Enter.

5.3 cin and spaces

cin >> name; stops reading at the first space. If the user types "Ali Khan", only "Ali" is stored. getline(cin, name); reads the whole line — we will study it with strings (Lecture 21). (Slide L02-S17 notes)

6. Arithmetic Operators

6.1 The five operators

(Slide L02-S15)

Operator

Meaning

Example

Result

+

Add

7 + 2

9

-

Subtract

7 - 2

5

*

Multiply

7 * 2

14

/

Divide

7 / 2

3 (integer division!)

%

Remainder (modulus)

7 % 2

1

6.2 The integer division trap

When both numbers are int, C++ gives an int answer and throws away the decimal part: 7 / 2 is 3. If at least one number is a decimal, the answer keeps its decimal part: 7.0 / 2 is 3.5.

Real-world example

Sharing 7 rotis between 2 people with integer division: each gets 3, and the leftover 1 roti is the remainder (7 % 2 = 1).

6.3 Uses of % and precedence

  • % works only with integers.
  • Even or odd: n % 2 is 0 for even numbers.
  • Last digit of a number: n % 10 (for 1234 it gives 4).
  • Precedence (which runs first): * / % before + -. Use brackets when unsure: (a + b) / 2. Full details in Lecture 3.

7. Complete Programs

7.1 Add two numbers (the Lecture 1 algorithm in C++)

(Slide L02-S13)

sum.cpp

#include <iostream>

using namespace std;

int main() {

int a, b, sum;

cout << "Enter first number: ";

cin >> a;

cout << "Enter second number: ";

cin >> b;

sum = a + b;

cout << "Sum = " << sum << endl;

return 0;

}

Output

Enter first number: 7

Enter second number: 5

Sum = 12

IPO map: Input = cin >> a, cin >> b; Process = sum = a + b; Output = cout << sum.

7.2 Tracing (dry run) in memory

A dry run means following the program line by line on paper and writing what each variable holds. (Slide L02-S14)

Line executed

a

b

sum

int a, b, sum;

?

?

?

cin >> a; (user types 7)

7

?

?

cin >> b; (user types 5)

7

5

?

sum = a + b;

7

5

12

? = garbage value. Never use a variable before giving it a value.

7.3 Average of three marks

(Slide L02-S16)

average.cpp

#include <iostream>

using namespace std;

int main() {

int m1, m2, m3;

cout << "Enter 3 marks: ";

cin >> m1 >> m2 >> m3;

int total = m1 + m2 + m3;

double avg = total / 3.0;

cout << "Total: " << total << endl;

cout << "Average: " << avg << endl;

return 0;

}

Output

Enter 3 marks: 70 85 90

Total: 245

Average: 81.6667

Why 3.0 and not 3? total / 3 would give 81 (integer division). Dividing by 3.0 keeps the decimal part.

8. Common Mistakes

(Slide L02-S17)

Mistake

Wrong code

Problem

Fix

Wrong arrow

cin << age;

cin uses >>

cin >> age;

Using before assigning

int x; cout << x;

Prints a garbage value

int x = 0;

Integer division

double a = 5 / 2;

Gives 2, not 2.5

double a = 5.0 / 2;

Wrong quotes

char g = "A";

char needs single quotes

char g = 'A';

9. Word Bank (Key Terms)

Word

Simple meaning

Variable

A named memory location whose value can change

Declare

To create a variable by giving its type and name

Initialise

To give a variable its first value at the time it is created

Assign

To put a (new) value into a variable using =

Garbage value

An unknown leftover value in a variable that was never given a value

Data type

The kind of value a variable can store (int, double, char …)

Keyword

A word reserved by C++ that you cannot use as a name

Constant

A named value that cannot change (const)

Literal

A fixed value written directly in code, such as 100 or 'A'

Escape sequence

A backslash code inside quotes, such as \n or \t

Prompt

A message that tells the user what to type

Dry run (trace)

Following a program by hand, step by step, to see the values

10. Self-Check Questions

1. What is a variable? Use the box example in your answer.

2. Which data type would you use for a CGPA? For a grade letter? For a yes/no answer?

3. What is the difference between cout << and cin >>?

4. What is the result of 7 / 2 in C++? And 7 % 2? And 7.0 / 2?

5. Which of these names are invalid, and why: total_marks, 2ndRoll, my name, Return, int?

6. Homework: rectangle area and perimeter; Celsius to Fahrenheit; seconds to h:m:s; swap two numbers (Slide L02-S20).

Answers to 1–5 are in §2.1, §3.2, §5.1, §6.2 and §2.3.

← All CS-307 material