EECE 1313 LAB 3
17 OCTOBER 2016
NAME:
MATRIC NO.:
Once you have variables declared, you will do operations. An expression is the sequence of
operands and operators to reduce in to one value. C++ provides you a lot of operators as
shown below.
-Assignment operator (=)
Assignment operator is used to assign a value to a variable.
Example:
int x;
int y;
x=10; //assign 10 to x variable
y=x; //assign x to y variable
-Arithmetic operators (+, -,*, /, %)
Arithmetic operators are used to perform arithmetic operations on variables.
Sign Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% Division of modulus
1. Write a C++ program to produce the output as shown below:
Contents of x before Expression Value of Expression Contents of x
after
5 | x++ | 5 | 6
5 | x-- | 5 | 4
5 | ++x | 6 | 6
5 | --x | 4 | 4
In C++, there are two conditional statements for making decisions. The two conditional
statements are if else and switch case.
if/else statement
If statement will evaluate the condition. If it is true, it will execute the statements that follow
it, otherwise, it will execute the statements in else block.
if statement without else:
if (condition) {
Statement
Statement
}
or
if statement with else:
if (condition) {
Statement
Statement
}
else {
Statement
Statement
}
2. Write a C++ program that prompts the user to input three integer values and find the
greatest value of the three values.
Put simply, loop enables your program to execute the block of code repeatedly. In C++, there
are for, while, and do while loops that are discussed below.
For loop
for (start value; condition; mincrement){
statement1
statement2
}
The code in for loop will be executed from the start value until the condition is met. The
increment will be added continuously to the start value until the condition is met. Example:
int i;
for (i = 1; i <=10; i++)
count<<"\C++ programming"; //The words C++ programming will
//be printed 10 times
While Loop
The while loop executes code repeatedly if the condition is still true.
while(condition){
statement
statement
---------
Example:
int i = 1;
while (i <= 10)
{
cout << "\nC++";
i = i + 1;
}
3. Write a C++ program that will print the following pattern:
*******
******
*****
****
***
**
*
*PLEASE SUBMIT YOUR WORK AT THE END OF THE LAB SESSION