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

ITEC1030_Ch03_Elements of Programming Language

This document provides an overview of the basic elements of the C++ programming language, including the development cycle, fundamental components, and structure of a C++ program. It covers topics such as the integrated development environment (IDE), writing and compiling code, and the significance of variables, data types, and comments. Additionally, it includes examples of C++ syntax and the use of standard input/output streams.

Uploaded by

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

ITEC1030_Ch03_Elements of Programming Language

This document provides an overview of the basic elements of the C++ programming language, including the development cycle, fundamental components, and structure of a C++ program. It covers topics such as the integrated development environment (IDE), writing and compiling code, and the significance of variables, data types, and comments. Additionally, it includes examples of C++ syntax and the use of standard input/output streams.

Uploaded by

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

Welcome

Department of information
Technology
Basic Computer
Programming
_________________________________________
@ 2024 FTVT Institute All Rights Reserved

OF/FTI/ALL /18 Issue No: 1


PPT 1
CHAPTER 3:
Elements of Programming
Language

2
Objective
After studying this chapter you will be able
to:
• Understand the complete development
cycle in C++ programming language.
• Describe the basic elements of C++
programming language.
• Identify fundamental components of C++.
• Understand the concept of variables and
datatypes.
3
Department of Information AUGUST
Outline
1. The IDLE of programming environment
2. Fundamental components of C++
3. Basic Elements of C++
 Data Types, Variables, and Constants
 Operators
 Precedence of Operators
 Simple Type Conversion
4. Statements

4
Department of Information AUGUST
3.1 The IDLE Programming Environment
• An integrated development environment (IDE) is a software application that
helps programmers develop software code efficiently, it is simply software
editor.
• The complete development cycle in C++ is: Write the
program(Edit),Preprocess, compile the source code, link the program, and run
it.
• C++ programs typically go through five phases to be executed: these are
• edit,
• preprocess,
• compile,
• link,
• load

5
Department of Information AUGUST
Writing a Program

• To write a source code, your compiler may have its own built-in text editor, or you may
be using a commercial text editor or word processor that can produce text files.
• Source code is generally understood to mean programming statements that
are created by a programmer with a text editor or a visual programming
tool and then saved in a file.
• This is accomplished with an editor program.
• The programmer types C++ statements with the editor and makes corrections if
necessary
• The programs source file is then stored on secondary storage device such as a disk with
a “.cpp” file name.
• After the program is edited, C++ is principally compiled in three phases:
preprocessing, translation to object code, and linking

OF-FTI-ALL -18 6 6
Department of Information AUGUST
Preprocess
• In a C++ system, a preprocessor program executes automatically before the compiler’s
translation phase begins.

• The C++ preprocessor obeys command called preprocessor directives, which indicate
that certain manipulations are to be performed on the program before compilation.

• The preprocessor is invoked by the compiler before the program is converted to


machine language.

• The C++ preprocessor goes over the program text and carries out the instructions
specified by the preprocessor directives (e.g., #include).

• The result is a modified program text which no longer contains any directives.
OF-FTI-ALL -18 7 7
Department of Information AUGUST
Compiling
• Your source code file can't be executed, or run, as a program can.

• To turn your source code into a program, you use a compiler.

• How you invoke your compiler, and how you tell it where to find your
source code, will vary from compiler to compiler

• This file is often named with the extension .OBJ.

• This is still not an executable program, however to turn this into an


executable program, you must run your linker .

OF-FTI-ALL -18 8 8
Department of Information AUGUST
Linking
• C++ programs are typically created by linking together one or more OBJ files with
one or more libraries.
• A library is a collection of linkable files that were supplied with your compiler, that
you purchased separately, or that you created and compiled.
• All C++ compilers come with a library of useful functions (or procedures) and
classes that you can include in your program.
• A function is a block of code that performs a service, such as adding two numbers or
printing to the screen.
• A class is a collection of data and related functions.
• A linker links the object code with the code for the missing function to produce an
executable image (with no missing pieces).
• Generally, the linker completes the object code by linking it with the object code of
any library modules that the program may have referred to.
• The final result is an executable file
OF-FTI-ALL -18 9 9
Department of Information AUGUST
Loading

• The loader takes the executable file from disk and transfers it to memory.

• Additional components from shared libraries that support the program are

also loaded.

• Finally, the computer, under the control of its CPU, executes the program.

• In practice all these steps are usually invoked by a single command and the

user will not even see the intermediate files generated.


OF-FTI-ALL -18 1 10
Department of Information AUGUST 0
From a High-Level Program to an Executable
File

OF-FTI-ALL -18 1 11
Department of Information AUGUST 1
Source code to target code sequence

OF-FTI-ALL -18 1 12
Department of Information AUGUST 2
Structure of C++ Program
• [Comments]
• [Preprocessor directives]
• [Global variable declarations]
• [Prototypes of functions]
• [Definitions of functions]

1
Department of Information AUGUST 3
3.2 Fundamental components of
C++
• C++ program is a collection of one or more subprograms,
called functions.
• A subprogram or a function is a collection of statements
that, when activated (executed), accomplishes something
• Every C++ program has a function called main
• The smallest individual unit of a program written in any
language is called a token
• The hello world program is one of the simplest programs,
but it already contains the fundamental components that
every C++ program has.

1
Department of Information AUGUST 4
Showing Sample Program in C++
• // A hello world program in C++
• #include<iostream>
• using namespace std;
• int main()
•{
• cout << "Hello World!";
• return 0;
•}
1
Department of Information AUGUST 5
---// A hello world program in C++
• C++ supports two ways to insert comments:
1,// line comment 2, /* block comment */
• The first line in our program is a comment line.
• Every line that starts with two slash signs ( // ) are
considered comments and will have no effect on the
behavior or outcome of the program.
• (Words between /* and */ will also be considered as
comments (old style comments)).
• Use comments in your programs to explain difficult sections,
but don’t overdo it.
• It is also common practice to start every program with a brief
description on what the program will do.

1
Department of Information AUGUST 6
--- #include<iostream>
• Lines beginning with a pound sign (#) are used
by the compilers pre-processor.
• In this case the directive #include tells the pre-
processor to include the iostream standard file.
• This file iostream includes the declarations of
the basic standard input/output library in C++.
• (See it as including extra lines of code that add
functionality to your program).

1
Department of Information AUGUST 7
--- using namespace std;
• All the elements of the standard C++ library
are declared within what is called a namespace.
• In this case the namespace with the name std.
• We put this line in to declare that we will make
use of the functionality offered in the namespace
std.
• This line of code is used very frequent in C++
programs that use the standard library.
• You will see that we will make use of it in most of
the source code included in these tutorials.

1
Department of Information AUGUST 8
--- int main()
• int is what is called the return value (in this
case of the type integer. Integer is a whole
number).
• Every program must have a main()
function.
• The main function is the point where all
C++ programs start their execution.
• The word main is followed by a pair of round
brackets.
• That is because it is a function declaration. It
is possible to enclose a list of parameters within
the round brackets.
1
Department of Information AUGUST 9
---cont
• A C++ program is a collection of one or more
subprograms (functions)
• Function
• Collection of statements
• Statements accomplish a task
• Every C++ program has a function called main

2
Department of Information AUGUST 0
--- {}
• The two curly brackets (one in the beginning
and one at the end) are used to indicate the
beginning and the end of the function main. (Also
called the body of a function).
• Everything contained within these curly brackets
is what the function does when it is called and
executed.

2
Department of Information AUGUST 1
--- cout << “Hello World”;
• This line is a C++ statement.
• A statement is a simple expression that can produce
an effect.
• In this case the statement will print something to our
screen.
• cout represents the standard output stream in C++.
• In this a sequence of characters (Hello World) will be
send to the standard output stream.
• The words Hello World have to be between ” “, but
the ” ” will not be printed on the screen. They
indicate that the sentence begins and where it will
end.
2
Department of Information AUGUST 2
---cout
• cout is declared in the iostream standard file within the
std namespace. This is the reason why we needed to
include that specific file. This is also the reason why we
had to declare this specific namespace.
• As you can see the statement ends with a semicolon (;).
• The semicolon is used to mark the end of the
statement.
• The semicolon must be placed behind all statements in C+
+ programs.
• So, remember this, One of the common errors is to forget
to include a semicolon after a statement.

2
Department of Information AUGUST 3
--- return 0;
• The return statement causes the main
function to finish.
• You may return a return code (in this case a zero)
but it depends on how you start your function
main( ).
• We said that main ( ) will return an int (integer).
• So we have to return something (in this case a
zero).
• A zero normally indicates that everything went ok
and a one normally indicates that something has
gone wrong
2
Department of Information AUGUST 4
--- Indentations
• As you can see the cout and the return
statement have been indented or moved to the
right side.
• This is done to make the code more readable. In
a program as Hello World, it seems a stupid thing to
do.
• But as the programs become more complex, you
will see that it makes the code more readable.
• So, always use indentations and comments to
make the code more readable.
• It will make your life (programming life at least)
much easier if the code becomes more complex.
2
Department of Information AUGUST 5
Example
#include <iostream>
using namespace std;

int main()
{
int a = 10, b = 20;
if (a > b) {
cout << "a is greater than b";
}
else {
cout << "b is greater than a";
}
return 0;
}

OF-FTI-ALL -18 2 26
Department of Information AUGUST 6
cout and cin
 Cout is an object used for printing data to the screen.
 To print a value to the screen, write the word cout, followed by the
insertion operator also called output redirection operator (<<) and
the object to be printed on the screen.
 Syntax: Cout<<Object;
 The object at the right hand side can be:
• A literal string: “Hello World”
• A variable: a place holder in memory

OF-FTI-ALL -18 2 27
Department of Information AUGUST 7
cout and cin
 Cin is an object used for taking input from the keyboard.
 To take input from the keyboard, write the word cin, followed by
the input redirection operator (>>) and the object name to hold the
input value.
 Syntax: Cin>>Object
 Cin will take value from the keyboard and store it in the memory.
Thus the cin statement needs a variable which is a reserved
memory place holder.

OF-FTI-ALL -18 2 28
Department of Information AUGUST 8
Example

OF-FTI-ALL -18 2 29
Department of Information AUGUST 9
3.3 Basic Elements
• Five kind of tokens in C++
• Keywords (reserved words)
• Identifiers
• Literals
• Operators
• Comments

3
Department of Information AUGUST 0
3.3.1 Keywords (reserved words)
• Words with special meaning to the compiler
• Have a predefined meaning that can’t be
changed
• All reserved words are in lower-case letters
• Must not be used for any other purposes

3
Department of Information AUGUST 1
3.3.2 Identifiers
• An identifier is name associated with a function or data
object and used to refer to that function or data object.
• Programmer given names
• Identify classes, variables, functions, etc.
• Consist of letters, digits, and the underscore character
(_)
• Must begin with a letter or underscore
• Not be a reserved word
• C++ is case sensitive
• Some predefined identifiers are cout and cin
• Unlike reserved words, predefined identifiers may be
redefined, but it is not a good idea
3
Department of Information AUGUST 2
--- valid and invalid Identifiers

3
Department of Information AUGUST 3
3.3.3 Literals
• Literals are constant values which can be a
number, a character of a string.
• Explicit (constant) value that is used by a program
• Literals can be digits, letters or others that
represent constant value to be stored in variables
• Assigned to variables
• Used in expressions
• Passed to methods
• E.g.
• Pi = 3.14; // Assigned to variables
• C= a * 60; // Used in expressions

3
Department of Information AUGUST 4
3.3.4 Comments
• Remark about programs
• Explain programs to other programmers
• Improve program readability
• Ignored by compiler
• Two types
 Single-line comment
• Begin with //
• Example
• // This is a text-printing program.
 Multi-line comment
• Start with /*
• End with */
• Typical uses
 Identify program and who wrote it
 Record when program was written
3
 Department of Information AUGUST 5
Example
1 /* Fig. 2.1: fig02_01.cpp
2 Text-printing program. */
3 #include <iostream> // allows program to output data to the screen
4
5 // function main begins program execution
6 int main()
7 {
8 cout << "Welcome to C++!\n"; // display message
9
10 return 0; // indicate that program ended successfully
11

12 } // end function main

Welcome to C++!

3
Department of Information AUGUST 6
3.3.5 Variables
• Location in memory where value can be
stored
• All variables have two important attributes
• A type - Once defined, the type of a C++ variable cannot
be changed
• A value - can be changed by assigning a new value
• E.g. int a = 5;
• Type integer
• Value 5

3
Department of Information AUGUST 7
Variable Declaration
• Defining (creating) a variable
• Two parts
• Variable name - unique name of the memory location
• Data type (or type) - defines what kind of values the
variable can hold
• Syntax
• <type> <Var-idf>;
• E.g.
• double varTime;
• int myAge;
• Can declare several variables of same type in
one declaration
• Comma-separated list
• int integer1, integer2, sum;
• Variables must be declared before used
Department of Information AUGUST
3
8
---cont
• Variable names
 Valid identifier
 Series of characters (letters, digits, underscores)
 Cannot begin with digit
 Case sensitive
• Assignment operator =
 Assigns value on left to variable on right
 Binary operator (two operands)
• Example:
• sum = variable1 + variable2;
• Add the values of variable1 and variable2
• Store result in sum

3
Department of Information AUGUST 9
1 // Fig. 2.5: fig02_05.cpp
2 // Addition program that displays the sum of two numbers.
3 #include <iostream.h> // allows program to perform input and output
4
5 // function main begins program execution
6 int main()
7 {
8 // variable declarations
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: "; // prompt user for data
14 std::cin >> number1; // read first integer from user into number1
15
16 cout << "Enter second integer: "; // prompt user for data
17 cin >> number2; // read second integer from user into number2
18
19 sum = number1 + number2; // add the numbers; store result in sum
20
21 cout << "Sum is " << sum << std::endl; // display sum; end line
22
23 return 0; // indicate that program ended successfully
24
25 } // end function main

Enter first integer: 45


Enter second integer: 72
Sum is 117

4
Department of Information AUGUST 0
Variables and Memory Concept
• Variable names
• Correspond to actual locations in computer's memory
• Every variable has name, type, size and value
• When new value placed into variable, overwrites old value
• Writing to memory is destructive
• Reading variables from memory nondestructive
• Example
• int number1= 45;
• int number2= 72;
• int sum = 0;
• sum = number1 + number2;
• Value of sum is overwritten Values of number1 and number2
remain intact
4
Department of Information AUGUST 1
Memory locations after calculating and
storing the sum of number1 and number2.

4
Department of Information AUGUST 2
Data Types
• When you define a variable in C++, you must tell
the compiler what kind of variable it is
 Tell the data type

• Data Type: set of values together with a set of


operations
• C++ data can be classified into three categories:
 Simple data type
 Structured data type
 Pointers

4
Department of Information AUGUST 3
Basic Data Types
• Three categories of simple data
• Integral: integers (numbers without a decimal)

• Floating-point: decimal numbers

• Enumeration type: user-defined data type

4
Department of Information AUGUST 4
---Simple Data Types: int
• Type int
• Represent integers or whole numbers
• Some rules to follow:
 Plus signs do not need to be written before the number
 Minus signs must be written when using negative #’s
 Decimal points cannot be used
 Commas cannot be used
 Leading zeros should be avoided (octal or base 8 #’s
• Signed - negative or positive
• Unsigned - positive

4
Department of Information AUGUST 5
--Simple Data Types: double, float
• Type double, float
• used to represent real numbers
• many programmers use type float
• avoid leading zeros, trailing zeros are ignored
• long double

4
Department of Information AUGUST 6
---Simple Data Types: char
• Type char
• used to represent character data
• a single character which includes a space
• 1 byte, enough to hold 256 values
• Must be enclosed in single quotes eg. ‘d’
• Escape sequences treated as single char
• ‘\n’ newline
• ‘\’’ apostrophe
• ‘\”’ double quote
• ‘\t’ tab
• (0-255) or as a member of the ASCII set
• E.g. the lowercase letter "a" is assigned the value 97

4
Department of Information AUGUST 7
---Simple Data Types
• Strings
• Used to represent textual information
• string constants must be enclosed in double
quotation marks eg. “Hello world!”
• empty string “”
• new line char or string “\n”
• “the word \”hello\”” (puts quotes around “hello” )
• String variables use:
#include “apstring.cpp”
• use quotes for user supplied libraries

4
Department of Information AUGUST 8
Type Size Values

unsigned short int 2 bytes 0 to 65,535

short int(signed short 2 bytes -32,768 to 32,767


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

long int(signed long 4 bytes -2,147,483,648 to

int) 2,147,483,647
int
2 bytes -32,768 to 32,767

unsigned int 2 bytes 0 to 65,535

signed int 2 bytes -32,768 to 32,767

char 1 byte 256 character values

float 4 bytes 3.4e-38 to 3.4e38

double 8 bytes 1.7e-308 to 1.7e308

long double 10 1.2e-4932 to 1.2e4932


bytes
3.4 Operator
• symbols that take one or more arguments
(operands) and operates on them to produce a
result
• A unary operator requires one operand.
• A binary operator requires two operands.
• Some of Operators
 Arithmetic operators
 Assignment operator
 Increment/Decrement operators
 Relational operators
 Logical operators
 Conditional Operator

5
Department of Information AUGUST 0
3.4.1Arithmetic Operators
• Arithmetic operators perform mathematical
operations like addition, subtraction and
multiplication
• Addition +
• Subtraction -
• Multiplication *
• Division /
• Mod %
• Note
• No exponentiation operator
• Single division operator
• Operators are overloaded to work with more than one
type of object
5
Department of Information AUGUST 1
// CPP Program to demonstrate the Binary Operators
#include <iostream>
using namespace std;

int main()
{
int a = 8, b = 3;

// Addition operator
cout << "a + b = " << (a + b) << endl;

OF-FTI-ALL -18 5 52
Department of Information AUGUST 2
// Subtraction operator
cout << "a - b = " << (a - b) << endl;

// Multiplication operator
cout << "a * b = " << (a * b) << endl;

// Division operator
cout << "a / b = " << (a / b) << endl;
// Modulo operator
cout << "a % b = " << (a % b) << endl;

return 0;
}
OF-FTI-ALL -18 5 53
Department of Information AUGUST 3
5
Department of Information AUGUST 4
Arithmetic Operators and Precedence
• Consider m*x + b which of the following is it equivalent to
• (m * x) + b
• Operator precedence tells how to evaluate expressions
• Standard precedence order
• () Evaluate first, if nested innermost
done first
• */% Evaluate second. If there are several,
then evaluate from left-to-right
 +- Evaluate third. If there are several,
then evaluate from left-to-right

5
Department of Information AUGUST 5
3.4.2 Assignment Operator
• The assignment operator is used for storing a value at some memory
location (typically denoted by a variable).
• Assignment operators are used to store the result of mathematical
operation in one of the operand which is at left side of the operator.
• Assignment operator =
• Assigns value on left to variable on right
• A literal constant Eg x=8
• A variable Eg y=x
• An expiration Eg x=y+3
• Binary operator (two operands)
• Example:
• int a= 5;
• float b= 9.66;
• char ch=‘d’;
• int m, n, p;
• m = n = p = 100;
5
Department of Information AUGUST 6
Compound assignment operators
 Compound assignment operator is the combination of the assignment
operator with other operators like arithmetic and bit wise operators.

 The assignment operator has a number of variants, obtained by combining


it with other operators.

OF-FTI-ALL -18 5 57
Department of Information AUGUST 7
5
Department of Information AUGUST 8
Examples

5
Department of Information AUGUST 9
3.4.3 Relational Operators
• In order to evaluate a comparison between two
expressions, we can use the relational operator.
• (==, !=, > , <, >=, <=).
• The result of a relational operator is a bool value that
can only be true or false according to the result of the
comparison.
• E.g.: (7 = = 5) would return false or returns 0 (5 > 4)
would return true or returns 1
• The operands of a relational operator must evaluate to a
number.
• Characters are valid operands since they are
represented by numeric values.
• For E.g.: ‘A’ < ‘F’ would return true or 1. it is like (65 <
70)
--- cont
Operator Name Example

== Equality 5 == 5 // gives 1

!= Inequality 5 != 5 // gives 0

< Less Than 5 < 5.5 // gives 1

<= Less Than or Equal 5 <= 5 // gives 1

> Greater Than 5 > 5.5 // gives 0

>= Greater Than or Equal 6.3 >= 5 // gives 1

Relational operators
3.4.4 Logical Operators(!, &&, ||)
• Like the relational operators, logical operators
evaluate to 1 or 0.
 Logical negation (!) is a unary operator, which
negates the logical value of its operand. If its operand
is non zero, it produce 0, and if it is 0 it produce 1.
 Logical AND (&&) produces 0 if one or both of its
operands evaluate to 0 otherwise it produces 1.
 Logical OR (||) produces 0 if both of its operands
evaluate to 0 otherwise, it produces 1.

6
Department of Information AUGUST 2
--- con

6
Department of Information AUGUST 3
3.4.5 Increment/Decrement Operators:
(++) and (--)
• The auto increment (++) and auto decrement (--)
operators provide a convenient way of, respectively,
adding and subtracting 1 from a numeric variable.
• E.g.: if a was 10 and if a++ is executed then a will
automatically changed to 11.
• Increment operator: increment variable by 1
• Decrement operator: decrement variable by 1

6
Department of Information AUGUST 4
Prefix and Postfix:
• The prefix type is written before the variable.
• Eg (++ myAge), whereas the postfix type appears after
the variable name (myAge ++).
• Prefix and postfix operators can not be used at
once on a single variable:
• Eg: ++age-- or --age++ or ++age++ or - - age - - is
invalid
• The prefix operator is evaluated before the
assignment, and the postfix operator is evaluated
after the assignment.

6
Department of Information AUGUST 5
--- cont
• ++count; or count++; increments the value of count
by 1
• --count; or count--; decrements the value of
count by 1
• If x = 5; and y = ++x;
 After the second statement both x and y are 6
• If x = 5; and y = x++;
 After the second statement y is 5 and x is 6

6
Department of Information AUGUST 6
---cont

6
Department of Information AUGUST 7
6
Department of Information AUGUST 8
6
Department of Information AUGUST 9
Conditional Operator (?:)
• The conditional operator takes three operands. It has the general form:
Syntax: operand1 ? operand2 : operand3
• First operand1 is a relational expression and will be evaluated. If the result of
the evaluation is non zero (which means TRUE), then operand2 will be the
final result. Otherwise, operand3 is the final result.
• E.g.: General Example Z=(X=Y) the value of Y will be assigned to Z.
• E.g.: int m=1,n=2,min; min = (m < n ? m : n); The value stored in min is 1.
E.g.: (7 = = 5 ? 4: 3) returns 3 since 7 is not equal to 5

OF-FTI-ALL -18 7 70
Department of Information AUGUST 0
Type conversion
• A value in any of the built-in types can be
converted
• Called type cast
• Syntax
• (<data – type> )value; or <data – type> (value);
• E.g
• (int) 3.14 // converts 3.14 to an int to give 3
• (long) 3.14 // converts 3.14 to a long to give 3L
• (double) 2 // converts 2 to a double to give 2.0
• (char) 122 // converts 122 to a char whose code is
122
• (unsigned short) 3.14 // gives 3 as an unsigned short

7
Department of Information AUGUST 1
Type conversion

• Some times the compiler does the type casting – implicit


type cast
• E.g
• double d = 1; // d receives 1.0
• int i = 10.5; // i receives 10

7
Department of Information AUGUST 2
3.5 Statements
• Statements are fragments of the C++ program
that are executed in sequence. The body of any
function is a sequence of statements. For
example:
• Roughly equivalent to sentences in natural
languages
• Forms a complete unit of execution.
• Terminating the expression with a semicolon (;)

7
Department of Information AUGUST 3
----- Statements
• C++ includes the following types of
statements:
• 1) expression statements;
• 2) compound statements;
• 3) selection statements;
• 4) iteration statements;
• 5) jump statements;
• 6) declaration statements;

7
Department of Information AUGUST 4
Expression
• Combine literals, variables, and operators to form
expressions
• Expression is segments of code that perform
computations and return values .
• Expressions can contain: Example:
– a number literal, 3.14
– a variable, count
– a function call, sum(x, y)
– an operator between two expressions (binary operator), a
+b
– an operator applied to one expression (unary operator), -
discount
– expressions in parentheses. (3.14-amplitude)

7
Department of Information AUGUST 5
---cont
expression statements in c++
Assignment expressions
Any use of ++ or –
Function calls
Object creation expressions

7
Department of Information AUGUST 6
Examples

7
Department of Information AUGUST 7
1. Write a program that asks your age.
• #include <iostream>
• using namespace std;
• int main()
•{
• int age;
• cout << "Enter your age Please: ";
• cin >> age;
• cout << "Your Age is : " << age;
• return 0;
•} 7
Department of Information AUGUST 8
2.Write a C++ program to add two
numbers.

7
Department of Information AUGUST 9
3. Program to find a perimeter
and Area of a rectangle

8
Department of Information AUGUST 0
Exercise
1. Cascading Insertion and Extraction
operators
Write a C++ program to accept 2 numbers and
display the addition of the 2 numbers. For the
output format see below:
Out put
Enter two numbers :: 23 93
23+93=116

8
Department of Information AUGUST 1
Program : Cascading and
extraction operator in c++.

8
Department of Information AUGUST 2
Exercise
2. Write a program in C++ to calculate
the volume of a cylinder.

Volume of a cylinder Formula: V = pi*h*r^2


Where pi=3.14

8
Department of Information AUGUST 3
Program : in C++ to calculate the volume
of a cylinder.

8
Department of Information AUGUST 4
Exercise
3. Write a program in C++ to swap two numbers
Using Temporary Variable. For the output
format see below:

8
Department of Information AUGUST 5
Program : Swap two numbers Using
Temporary Variable

8
Department of Information AUGUST 6
Thank You

? 8
Department of Information AUGUST 7

You might also like