0% found this document useful (0 votes)
32 views

Programming Lecture 4

The document provides an example C++ program that outputs the multiplication table of 5 using stream insertion operations. It demonstrates declaring and initializing integer variables, using cout to output strings and arithmetic expressions, and chaining multiple stream insertion operations in a single statement. The document also discusses basic C++ programming concepts like variables, data types, identifiers, and input/output streams.

Uploaded by

fadydesoky45
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)
32 views

Programming Lecture 4

The document provides an example C++ program that outputs the multiplication table of 5 using stream insertion operations. It demonstrates declaring and initializing integer variables, using cout to output strings and arithmetic expressions, and chaining multiple stream insertion operations in a single statement. The document also discusses basic C++ programming concepts like variables, data types, identifiers, and input/output streams.

Uploaded by

fadydesoky45
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/ 25

1

Programming

Lecture 4

VARIABLES
Dr. Badria Nabil
Assignment 2
2

Write a C++ program that computes and display the


multiplication table of "5" as the following:
5*1=5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
Assignment 2 Solution
3

#include <iostream>
using namespace std;
int main()
{
cout << "the multiplication table of 5 \n";
cout << "5*1= " << 5 * 1 << endl;
cout << "5*2= " << 5 * 2 << endl;
cout << "5*3= " << 5 * 3 << endl;
cout << "5*4= " << 5 * 4 << endl;
cout << "5*5= " << 5 * 5 << endl;
cout << "5*6= " << 5 * 6 << endl;
cout << "5*7= " << 5 * 7 << endl;
cout << "5*8= " << 5 * 8 << endl;
cout << "5*9= " << 5 * 9 << endl;
cout << "5*10= " << 5 * 10 << endl;
return 0;
}
Chaining Stream Insertion Operations

 stream insertion operations


 Use multiple stream insertion operators in a single
statement
◼ Stream insertion operation knows how to output each type
of data
 Alsocalled chaining or cascading
 Example
◼ std::cout << "Sum is " << number1 + number2
<< std::endl;
◼ Outputs "Sum is “
◼ Then, outputs sum of number1 and number2
4
What Are Variables?
 Locationin memory where value can be stored
 Variables must be explicitly declared.

 Each variable has a type, an identifier, and a scope.

Type
Identifier

Initial value
int myAge;
boolean isAMovie;
float maxItemCost = 17.98F;
Declaring Variables

 The basic form of variable declaration:


◼ type identifier [ = initial value];

int itemsRented = 1;
float itemCost;
int i, j, k;
double interestRate;

 Variables can be initialized when declared.


Data Types

 Defines a set of values and operations that can


be performed on those values
 integers
 positive and negative whole numbers,
e.g. 5, -52, 343222
 short, int, long

 represented internally in binary


8
Data Types (con’t)
 Floating point (real)
 number has two parts, integral and fractional
 e.g. 5, 3.66666666, -.000034, 5.0

 float, double, long double

 stored internally in binary as mantissa and exponent

 10.0 and 10 are stored differently in memory


Data Types (con’t)
 Boolean
 named for George Boole
 represent conditional values

 values: true and false


11
Data Types (con’t)
 Characters
 represent individual character values
E.g. ’A’ ’a’ ’2’ ’*’ ’”’ ’ ’
 stored in 1 byte of memory

 special characters: escape sequences


E.g. ’\n’ ’\b’ ’\r’ ’\t’ ‘\’’
13
14
string Class
 Strings not built-in, but come from library
 Classes extend C++
 string literal enclosed in double quotes
E.g.: “Enter speed: “ “ABC” “B” “true”
“1234”
 #include <string>
 for using string identifiers, but not needed for literals
16
Data type Size Example

Char 1 byte -128 to 127

unsigned char 1 byte 0 to 255

int 2 bytes -32,768 to 32,767

unsigned int 2 bytes 0 to 65,535

long or (long int) 4 bytes -2,147,483,648 to 2,147,483,647

unsigned long 4 bytes 0 to 4,294,967,295

float 4 bytes 3.4 x 10^-38 to 3.4 x 10^38

double 8 bytes 1.7 x 10^-308 to 1.7 x 10^308

long double 10 bytes 3.4 x 10^-4932 to 3.4 x 10^4932


17
10/30/2022
Identifiers
 Names for data and objects to be manipulated
by the program
 Must begin with a letter or underscore (not
recommended)
 Consist only of letters, digits, and underscores
 Cannot be reserved word
 Upper and lower case significant
Defining Variable Names

 Variable names must start with a letter of the


alphabet, or underscore.
 Other characters may include digits.

a item_Cost item#Cost item-Cost


itemCost _itemCost item*Cost abstract
item1Cost itemCost2 2itemCost

 Use meaningful names for variables, such as


customerFirstName, ageNextBirthday.
Variables Declarations (Cont.)
 Can declare several variables of same type in one
declaration
 Comma-separated list
 int integer1, integer2, sum;

 Variable names
 Valid identifier
◼ Series
of characters (letters, digits, underscores)
◼ Cannot begin with digit

20
Good Programming Practice
 Place a space after each comma (,) to make programs more
readable.
 Use identifiers of 31 characters or fewer to ensure portability.
 Choosing meaningful identifiers helps make a program self-
documenting—a person can understand the program simply by
reading it rather than having to refer to manuals or comments.
 Avoid identifiers that begin with underscores and double
underscores, because C++ compilers may use names like that
for their own purposes internally.
 Always place a blank line between a declaration and
executable statements.
21
Input stream object
 Obtain data for program to use - different each time
program executes
 std::cin
 from <iostream>
 Usually connected to keyboard

 Stream extraction operator >>


◼ Waits for user to input value, press Enter (Return) key
◼ Stores value in variable to right of operator
◼ Converts value to variable data type
 Example
◼ std::cin >> number1;
◼ Reads an integer typed at the keyboard
22
◼ Stores the integer in variable number1
Assignment 3
23
24
25

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy