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

Programming Lecture 6

This document discusses C++ programming concepts including variables, increment/decrement operators, compound assignments, constants, and provides an example program. The program [1] takes feet and inches as input, [2] converts to total inches, [3] converts inches to centimeters and outputs the result.

Uploaded by

Mostafa Mohamed
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)
36 views

Programming Lecture 6

This document discusses C++ programming concepts including variables, increment/decrement operators, compound assignments, constants, and provides an example program. The program [1] takes feet and inches as input, [2] converts to total inches, [3] converts inches to centimeters and outputs the result.

Uploaded by

Mostafa Mohamed
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/ 12

1

Programming

Lecture 6

VARIABLES EXAMPLES
Dr. Badria Nabil
Assignment 3
2

 Write a C++ program that takes three numbers


from the user and calculates the sum and the
average of them and print the sum and the
average to user.
Assignment Solution
3
Incrementing and Decrementing
Values
 The ++/-- operator increments/decrements by 1:

int var1 = 3;
var1++; // var1 now equals 4
Var1--; // var1 now equals 2
 The ++/-- operators can be used in two ways:

int var1 = 3, var2 = 0;


var2 = ++var1; // Prefix: var2=4 Increment var1 first,
// then assign to var2.
var2 = var1++; // Postfix: var2= 3 Assign to var2 first,
// then increment var1.
Increment & Decrement Operators
5

 Increment operator: increment variable by 1


 Pre-increment: ++variable
 Post-increment: variable++

 Decrement operator: decrement variable by 1


 Pre-decrement: --variable
 Post-decrement: variable--

 What is the difference between the following?


x = 5; x = 5;
y = ++x; y = x++;

C++ Programming: From Problem Analysis to Program Design, Fourth Edition


Examining Compound Assignments

 An assignment operator can be combined with


any conventional binary operator:

double total=0, num = 1;


double percentage = 0.50;

total = total + num; // total is now 1
total += num; // total is now 2
total -= num; // total is now 1
total *= percentage; // total is now .5
Allocating Memory with Constants
7

 Named constant: memory location whose content


can’t change during execution
 The syntax to declare a named constant is:

 In C++, const is a reserved word

C++ Programming: From Problem Analysis to Program Design, Fourth Edition


Programming Example:
8
Convert Length
 Write a program that takes as input a given length
expressed in feet and inches
 Convert and output the length in centimeters
 One inch is equal to 2.54 centimeters

 One feet is equal to 12 inches

C++ Programming: From Problem Analysis to Program Design, Fourth Edition


Programming Example: Convert
9
Length (continued)
 The algorithm is as follows:
 Get the length in feet and inches
 Convert the length into total inches

 Convert total inches into centimeters

 Output centimeters

C++ Programming: From Problem Analysis to Program Design, Fourth Edition


Programming Example: Variables
10
and Constants
 Variables
int feet; //variable to hold given feet
int inches; //variable to hold given inches
int totalInches; //variable to hold total inches
double centimeters; //variable to hold length in
//centimeters

 Named Constant
const double CENTIMETERS_PER_INCH = 2.54;
const int INCHES_PER_FOOT = 12;

C++ Programming: From Problem Analysis to Program Design, Fourth Edition


11

C++ Programming: From Problem Analysis to Program Design, Fourth Edition


Programming Example: Sample Run
12

Enter two integers, one for feet, one for inches: 15 7

The numbers you entered are 15 for feet and 7 for inches.
The total number of inches = 187
The number of centimeters = 474.98

C++ Programming: From Problem Analysis to Program Design, Fourth Edition

You might also like