0% found this document useful (0 votes)
20 views19 pages

IP Lecture 2 DataTypesOperators

Uploaded by

Al Nahian Abid
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
20 views19 pages

IP Lecture 2 DataTypesOperators

Uploaded by

Al Nahian Abid
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 19

Variables, Data Types, and Arithmetic

Expressions
Course Code: CSC1102 &1103 Course Title: Introduction to Programming

Dept. of Computer Science


Faculty of Science and Technology

Lecturer No: 2 Week No: 1 Semester: Spring 23-24


Lecturer: Md. Faruk Abdullah Al Sohan; faruk.sohan@aiub.edu
Lecture 2: Outline
 Variables, Data Types, and Arithmetic Expressions
 Working with Variables
 Understanding Data Types and Constants
 The Basic Integer Type int
 The Floating Number Type float
 The Extended Precision Type double
 The Single Character Type char
 The Boolean Data Type bool
 Storage sizes and ranges
 Type Specifiers: long, long long, short, unsigned, and signed
 Working with Arithmetic Expressions
 Integer Arithmetic and the Unary Minus Operator
 The Modulus Operator
 Integer and Floating-Point Conversions
 Combining Operations with Assignment: The Assignment Operators
Variables
 Programs can use symbolic names for storing computation data

 Variable: a symbolic name for a memory location


 programmer doesn’t have to worry about specifying (or even knowing)
the value of the location’s address

 Variables have to be declared before they are used


 Variable declaration: [symbolic name(identifier), type]

 Declarations that reserve storage are called definitions


 The definition reserves memory space for the variable, but doesn’t put
any value there

 Values get into the memory location of the variable by


initialization or assignement
Variable declarations

Data type Variable name

Which data types Which variable names


are possible are allowed
Declaration vs Definition

 Variable declaration: [Type, Identifier]


 Variable definition: a declaration which does also
reserve storage space (memory) !
 Not all declarations are definitions
Variables - Examples
int a; // declaring a variable of type int

int sum, a1,a2; // declaring 3 variables

int x=7; // declaring and initializing a variable

a=5; // assigning to variable a, the value 5

a1=a; // assigning to variable a1, the value of a

L-value R-value

a1=a1+1; // assigning to variable a1 the value of a1+1


// (increasing value of a1 with 1)
Variable names
Rules for valid variable names (identifiers) in C :
 Name must begin with a letter or underscore ( _ ) and can be followed
by any combination of letters, underscores, or digits.
 Any name that has special significance to the C compiler (reserved
words) cannot be used as a variable name.
 Examples of valid variable names: Sum, pieceFlag, I, J5x7,
Number_of_moves, _sysflag
 Examples of invalid variable names: sum$value, 3Spencer, int.
 C++ is case-sensitive: sum, Sum, and SUM each refer to a different
variable !
 Variable names can be as long as you want, although only the first 63
(or 31) characters might be significant. (Anyway, it’s not practical to
use variable names that are too long)
 Choice of meaningful variable names can increase the readability of a
program
Data types
 Basic data types in C: int, float, double, char, and bool.

 Data type int: can be used to store integer numbers (values with no decimal
places)

 Data type type float: can be used for storing floating-point numbers (values
containing decimal places).

 Data type double: the same as type float, only with roughly twice the
precision.

 Data type char: can be used to store a single character, such as the letter a,
the digit character 6, or a semicolon.

 Data type bool: can be used to store just the values 0 or 1 (used for indicating
a true/false situation). This type has been added by the C99 standard (was not
in ANSI C)
Data types
Data Type Size Range Description
-2,147,483,648 Stores whole numbers, without decimals
int 4 bytes to
2,147,483,647
float 4 bytes Stores fractional numbers, containing one or
more decimals. Sufficient for storing 7 decimal
digits
double 8 bytes Stores fractional numbers, containing one or
more decimals. Sufficient for storing 15 decimal
digits
bool 1 byte true or false Stores true or false values
Stores a single character/letter/number, or
char 1 bytes 0 to 255
ASCII values
Assigning values to char

char letter; /* declare variable letter of type char */

letter = ‘A'; /* OK */
letter = A; /* NO! Compiler thinks A is a variable */
letter = “A"; /* NO! Compiler thinks “A" is a string */
letter = 65; /* ok because characters are really
stored as numeric values (ASCII code),
but poor style */

ASCII(American Standard Code for


Information Interchange)
Working with arithmetic expressions
 Basic arithmetic operators: +, -, *, /
 Precedence: one operator can have a higher priority, or
precedence, over another operator.
 Example: * has a higher precedence than +
 a+b*c
 if necessary, you can always use parentheses in an expression to
force the terms to be evaluated in any desired order.
 Associativity: Expressions containing operators of the same
precedence are evaluated either from left to right or from right
to left, depending on the operator. This is known as the
associative property of an operator
 Example: + has a left to right associativity
 In C++ there are many more operators -> later in this course !
Working
with #include<iostream>
arithmetic #include<math.h>
using namespace std;
expression int main ()
{
s int a = 100;int b = 2;
int c = 25;int d = 4;
int result;
result = a - b; // subtraction
iostream stands cout<<"a - b = "<< result<<endl;
for standard input- result = b * c; //
output stream. multiplication
#include<iostream> cout<<"b * c = "<<
using result<<endl;
declares objects that
namespac result = a / c; // division
control reading from
e std cout<<"a / c = "<<
and writing to result<<endl;
means
the standard result = a + b * c; //
that we
streams. In other precedence
can use
words, the iostream cout<<"a + b * c =
names for
library is an object- “<<result<<endl;
objects float result1=(float)c/d;
oriented library that
and cout<<result1<<endl;
provides input and
variables return 0;
output functionality }
from the
using streams.
Precedence of Operators

Precedence
!, ++, --, (type)
*, /, %
+, -
=
Integer and Floating-Point
Conversions
 Assign an integer value to a floating variable: does
not cause any change in the value of the number;
the value is simply converted by the system and
stored in the floating
 Assign a floating-point value to an integer variable:
the decimal portion of the number gets truncated.
 Integer arithmetic (division):
 int divided to int => result is integer division
 int divided to float or float divided to int => result is real
division (floating-point)
The Type Cast Operator
 f2 = (float) i2 / 100; // type cast operator
 The type cast operator has the effect of converting the value of
the variable i2 to type float for purposes of evaluation of the
expression.
 This operator does NOT permanently affect the value of the
variable i2;
 The type cast operator has a higher precedence than all the
arithmetic operators except the unary minus and unary plus.
 Examples of the use of the type cast operator:
 (int) 29.55 + (int) 21.99 results in 29 + 21
 (float) 6 / (float) 4 results in 1.5
 (float) 6 / 4 results in 1.5
The assignment operators
 The C++ language permits you to join the arithmetic operators
with the assignment operator using the following general
format: op=, where op is an arithmetic operator, including +,
–, ×, /, and %.
 op can also be a logical later in this course
 Example:
 count += 10;
 Equivalent with:
 count=count+10;
 Example: precedence of op=:
 a /= b + c
 Equivalent with:
 a = a / (b + c)
 addition is performed first because the addition operator has higher
precedence than the assignment operator
Structure of a C++ program
Entry point of a C+
+ program

int main ()
{
int value1, value2, sum; Sequential
value1 = 50; flow of control
value2 = 25;
sum = value1 + value2;
cout<<"The sum = “ <<sum<<endl;
return 0;
}
Controlling the program flow

 Forms of controlling the


program flow: Statement1
 Executing a sequence of Statement2
statements Statement3
Statement4
 Repeating a sequence of
Statement5
statements (until some Statement6
condition is met) Statement7
(looping) Statement8
 Using a test to decide
between alternative
sequences (branching)

You might also like