background image

LECTEURE # 4 :
STRUCTURED PROGRAMMING

Variables , input , memory


background image

Textbook and Software:

The C++ Programming Language (4th Edition) By: 
Bjarne Stroustrup. 

Programming: Principles and Practice Using C++ 
(2nd Edition) By: Bjarne Stroustrup.

The software that is used in this course is the Code:: 
Blocks as a C++ compiler.

2


background image

Variable

Location on computer

’s memory to store data then use and change 

its value in a program

Each variable has

1.

Name (identifier)

Series of letters, digits, underscores 

Not a keyword

Start with a letter

Case sensitive

Meaningful

2.

Type

Programmer-defined

3


background image

Keywords Shared with C

4

  C++ keywords

 

  Keywords common to the C and C++ programming languages 

  auto 

break 

case 

char 

const 

  continue 

default 

do 

double 

else 

  enum 

extern 

float 

for 

goto 

  if 

int 

long 

register 

return 

  short 

signed 

sizeof 

static 

struct 

  switch 

typedef 

union 

unsigned 

void 

  volatile 

while 

 

 

 

 

Cannot be used as identifiers or variable names


background image

Introduction to C++

5

New Keywords in C++

  C++ keywords

 

  C++-only keywords 

  and 

and_eq 

asm 

bitand 

bitor 

  bool 

catch 

class 

compl 

const_cast 

  delete 

dynamic_cast  explicit 

export 

false 

  friend 

inline 

mutable 

namespace 

new 

  not 

not_eq 

operator 

or 

or_eq 

  private 

protected 

public 

reinterpret_cast static_cast 

  template 

this 

throw 

true 

try 

  typeid 

typename 

using 

virtual 

wchar_t 

  xor 

xor_eq 

 

 

 

 

Cannot be used as identifiers or variable names


background image

Variable Condition

_under_bar_

z2

and

2h

67h2

his_account_total

t5

87

m928134

her_sales

3g

for

a

main

int

6

1.

Start with a letter

2.

Not a keyword

3.

Series of letters, digits, underscores 

4.

Case sensitive

5.

Meaningful

Home work: which of the following variables name is valid?


background image

C++ Built-in Data Types

Called fundamental types or primitives types: numericcharacter

logical

7

Some common data 
types are:

1. int - integer numbers

2. char - characters

3. double - floating 

point numbers


background image

bool

Data Type

Has two values, 

true

and 

false

Manipulate logical (Boolean) expressions

true

and 

false

are called logical values

bool

true

, and 

false

are reserved words (keyword)

8


background image

char

Data Type

Used for characters

letters, digits, and special symbols

Each character is enclosed in single quotes

Examples:  

'A'

,

'a'

,

'0'

,

'*'

,

'+'

,

'$'

,

'&' 

A blank space is a character and is written 

' '

with a space left 

between the single quotes

9


background image

Declaring Variables

All variables must be declared anywhere in program with a name 

and data type before they used

Syntax rule: begin with a data type then variable name

Variables of the same type can be declared in 

Multiple lines

One line separated by commas

10

int

num1;

int

num2;

int

num3;

int

num1, num2, num3;

dataType varName ;


background image

Initializing Variable

Variables can be initialized once declared

first

and 

second

are 

int

variables with the values 13 and 10, 

respectively

ch

is a 

char

variable whose value is empty

x

and 

y

are 

double

variables with 12.6 and 123.456, respectively

11

int

first=13, second=10;

char

ch=' ';

double

x=12.6, y=123.456;


background image

Ex.1: Compute the mean of 3 numbers

//This program calculates the mean of three numbers.

#include <iostream.h>
int main()
{

cout << " First Arithmetic Program by 

Big Bird.\n\n ";

cout << (12+5+10)/3;

return 0;

}

12

First Arithmetic Program by Big Bird.

2

9


background image

Ex. 2: Compute the mean of 3 numbers

Now, let’s try using a variable to store the mean before printing it.

// 

This program calculates the mean of three numbers.

#include <iostream.h> 

int main()

{

float mean;

cout << "Second Arithmetic Program by Big Bird.\n\n";

mean = (12+5+10)/3;

cout << mean;

return 0;

}

If we need to calculates the mean of 

ANY

three 

numbers??    we should interd it from the user

.

13

Second Arithmetic Program by Big Bird.

2

9


background image

14

// This program calculates the mean of 

ANY

three numbers.

#include <iostream>

int main()

{

float num1, num2, num3, mean;

cout << "Big Bird learns about input data.\n";

cout << endl;

cout << "Enter first number:   ";  

cin >> num1;    

c

out << "Enter second number:   ";

cin >> num2;

cout << "Enter third number:   ";

cin >> num3;

mean =  (num1+num2+num3)/3.0;

cout << "The average of " << num1 << " and " << num2 << 

" and " << num3;

cout << " is equal to = " << mean; 

return 0;

}

Ex. 3: Compute the mean of 3 numbers

Big Bird learns about input data 

2

Enter first number: 

4

Enter second number:

5

Enter third number:

6

The average of 12 and 10 and 5 is equal to = 9


background image

1. Load <iostream>

2. main

2.1 Initialize variables 
integer1

, integer2, 

and sum

2.2 Print "Enter first 
integer"

2.2.1 Get input

2.3 Print "Enter 
second integer"

2.3.1 Get input

2.4 Add variables and put 
result into sum

2.5 Print "Sum is"

2.5.1 Output sum

2.6 exit (return 0)

15

Enter first integer
45
Enter second integer
72

Sum is 117

1

// Addition program that display the sum of two numbers.

2
3      

#include

<iostream.h> 

// allow program to perform input and output

4      

5

// function main begins program execution

6      

int

main()

7      

{

8      

// variable declaration

9

int

number1;  

// first integer to add    

10     

int

number2;  

// second integer to add   

11     

int

sum;  

// sum of number1 and number2

12    
13    

cout << 

"Enter first integer: \n"

// prompt user for data

14    

cin >> number1;  

// read first integer from user to number1

15    
16    

cout << 

"Enter second integer: \n"

// prompt user for data

17    

cin >> number2;  

// read second integer from user to number2

18    
19    

sum = number1 + number2;  

// add the numbers; stor result in sum

20    
21    

cout << 

"Sum is "

<< sum << endl; 

// display sum; end line

22    
23    

return

0

;   

// indicate that program ended successfully

24    

// end function main

Declare integer variables.

obtain user input.

endl outputs a newline.

Concatenating, chaining or 
cascading stream insertion 
operations.

Calculations can be performed in output 
statements: alternative for lines 19 and 21:

cout << "Sum is " << number1 + number2 << 
endl;


background image

Variables and Memory

Variables names correspond to location in the computer

’s memory 

(RAM)

Every variable has 

name, type, size and value.

Placing new value into variable (memory location), overwrites old 

value 

– called destructive (Whenever a new value is placed into a 

variable, it replaces the previous value - it is destroyed)

Reading value of variable in memory 

– called nondestructive

16


background image

Variables and Memory (cont.)

cin >> number1;

Assume user entered 45

cin >> number2;

Assume user entered 72

sum = number1 + number2;

17

number1

45

number1

45

number2

72

number1

45

number2

72

sum

117


background image

Constants

Like variables 

data storage locations

Unlike variables

Values never changed during program execution

Any attempt to change a const creates a compilation error

Declared in two ways and follow identifier naming rules

With  

const

keyword

With

#define 

keyword

18

const int  

pie=3.14

const char 

Gender = ‘F’;

#define 

studentsPerClass = 15


background image

Exercise - 1

What prints when each of the following C++ statements is performed? If nothing prints, 

then answer “nothing.”

Assume x = 2 and y = 3.

1.

cout << x;

2.

cout << x + x;

3.

cout << "x=";

4.

cout << "x = " << x;

5.

cout << x + y << " = " << y + x;

6.

z = x + y;

7.

//cout << "x + y = " << x + y;

19


background image

Exercise - 2

Find the errors in the following program:
// Addition program that display the sum of two numbers.

#include <iostram.h> 

int main()

{

int number1;  
int number2;    

cout << "Enter first integer: \n";  
cin << number1;     
cout << "Enter second integer: \n"; 
cin >> number2;   

sum = number1 + number2  

cout << "Sum is " << sum << endl 

}

20




رفعت المحاضرة من قبل: Mohammed Aldhanuna
المشاهدات: لقد قام 0 عضواً و 59 زائراً بقراءة هذه المحاضرة








تسجيل دخول

أو
عبر الحساب الاعتيادي
الرجاء كتابة البريد الالكتروني بشكل صحيح
الرجاء كتابة كلمة المرور
لست عضواً في موقع محاضراتي؟
اضغط هنا للتسجيل