0% found this document useful (0 votes)
38 views23 pages

Computer Programming: Values and Variables - 2

This lecture covers fundamental concepts in computer programming, focusing on values and variables in C++. It includes topics such as comments, variable definitions, basic data types, type modifiers, operations, expressions, and mixed type expressions. Additionally, it provides exercises for practical application of the concepts discussed.

Uploaded by

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

Computer Programming: Values and Variables - 2

This lecture covers fundamental concepts in computer programming, focusing on values and variables in C++. It includes topics such as comments, variable definitions, basic data types, type modifiers, operations, expressions, and mixed type expressions. Additionally, it provides exercises for practical application of the concepts discussed.

Uploaded by

setsophak71
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Computer Programming

Lecture 4
Values and Variables - 2

Chhoeum Vantha, Ph.D.


Telecom & Electronic Engineering
Contents

• Comments
• Variable
• Basic Data Types
• Common Type Modifiers
• Operations
• Expressions
• Mixed Type Expressions

2
Comments

• Comments are used in C++ to make the code more


readable and to explain what the code is doing. The
compiler ignores all comments when the program is
compiled.

3
Comments

Types of Comments:
1. Single-line Comment
– Starts with //
– Used for short explanations or notes
2. Multi-line Comment
– Starts with /* and ends with */
– Used for longer explanations or to temporarily
disable code

4
Comments

• q

5
Variable

• A variable is a name given to a memory location. It is


used to store data that can be changed during program
execution.

6
Basic Data Types

Data Type Description Example Values


int Integer values -5, 0, 100
Floating-point
float 3.14, -0.1
(decimal) numbers
Double-precision
double 3.14159, 2.71828
float
char Single character 'A', 'z', '3'
bool Boolean (true/false) true, false
No value (used for
void -
functions)

7
Basic Data Types

• q

8
Common Type Modifiers

• Signed, Unsigned, Short, long


Modifier Description Example Usage
Default for integer types; can store
signed signed int a = -10;
both positive and negative numbers.
Only stores non-negative numbers (0
unsigned unsigned int b = 20;
and positive).
Reduces the size of integer to at least
short short int c = 1000;
16 bits.
Increases the size of integer to at least
long long int d = 100000L;
32 bits.
Further increases size, usually at least long long int e =
long long
64 bits. 10000000000LL;
Declares a constant value that cannot
const const int f = 50;
be changed.
9
check the size of basic data types

• sizeof operator use to check the size of basic data types in C++

10
Operations

• Numeric Operators

11
Expressions

Commonly used Python arithmetic binary operators

12
Expressions

• An expression is a combination of operators, constants,


and variables.
• An expression may consist of one or more operands, and
zero or more operators to produce a value.

13
Expressions

• n

14
Expressions

Types of Expressions

15
Expressions

Integer division and modulus

The modulus operator (%) computes the remainder of


integer division
16
Expressions

The / operator applied to two integers produces a floating-


point result

17
Mixed Type Expressions

• Expressions may contain mixed integer and floating-point


elements
• When an operator has mixed operands—one operand an
integer and the other a floating-point number—the
interpreter treats the integer operand as a floating-point
number and performs floating-point arithmetic

18
W4 - Practice

19
Exercise 1.

Write a program that calculates and displays the area


and circumference of a circle with a radius 5. Use float
type and pi = 3.14.

20
Exercise 2.

Write a program to swap the values of two integer


variables using a third variable.
Expected output:

21
Exercise 3.

Fill in the blanks with the correct result:


int a = 10, b = 4, c = 3; float x = 2.5; float y = 1.5;
1. a + b * c = __________
2. (a + b) * c = __________
3. a / b = __________
4. a % b = __________
5. x + y * c = __________
6. (x + y) / c = __________
7. a + (int)x % b = __________
8. x / y + a - c * b = __________
22
Thanks!

23

You might also like