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

Output:: Prepared By: Mohammad H. Ryalat

The document provides examples and explanations of C++ programming concepts such as variables, data types, operators, conditional statements, and input/output. It includes 15 exercises with sample code and expected output to demonstrate concepts like declaring and initializing variables, arithmetic and logical operators, conditional operators, input with cin, output with cout, and boolean logic. The document is intended to teach C++ programming fundamentals through examples and exercises prepared by Mohammad H. Ryalat.

Uploaded by

AnasA.Qatanani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Output:: Prepared By: Mohammad H. Ryalat

The document provides examples and explanations of C++ programming concepts such as variables, data types, operators, conditional statements, and input/output. It includes 15 exercises with sample code and expected output to demonstrate concepts like declaring and initializing variables, arithmetic and logical operators, conditional operators, input with cin, output with cout, and boolean logic. The document is intended to teach C++ programming fundamentals through examples and exercises prepared by Mohammad H. Ryalat.

Uploaded by

AnasA.Qatanani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

AlBalqa Applied University

Prince Abdullah bin Ghazi Faculty of Information Technology


Examples Programming with C++ 19/6/2013
Ex1
What is the output for the following code?

#include <iostream.h>
void main ( )
{
int a, b;
a = 10;
b = 4;
a = b;
b = 7;
cout << "a:";
cout << a;
cout << " b:";
cout << b;
}

Output: a:4 b:7

Ex2
Read the following.
a = 2 + (b = 5);

is equivalent to:

b = 5;
a = 2 + b;

So, the value of a will be 7.


Ex3
What is the output for the following code?
int a, b=3;
a = b;
a+=2;
cout << a;

Output: 5

Prepared by: Mohammad H. Ryalat


1
Ex4
What is the output for the following code?

int B=3;
int A=++B;
cout<<A<<"\t"<<B;
Output: 4 4

int B=3;
int A=B++;
cout<<A<<"\t"<<B;
Output: 3 4
Ex5
Read the following.
Suppose that a=2, b=3 and c=6,

(a == 5) // evaluates to false since a is not equal to 5.


(a*b >= c) // evaluates to true since (2*3 >= 6) is true.
(b+4 > a*c) // evaluates to false since (3+4 > 2*6) is false.
((b=2) == a) // evaluates to true.

Be careful! The operator = (one equal sign) is not the same as the operator == (two equal
signs), the first one is an assignment operator (assigns the value at its right to the variable
at its left) and the other one (==) is the equality operator that compares whether both
expressions in the two sides of it are equal to each other. Thus, in the last expression
((b=2) == a), we first assigned the value 2 to b and then we compared it to a, that also
stores the value 2, so the result of the operation is true.

Ex6
Read the following.
!(5 == 5) // evaluates to false because the expression at its right (5 == 5) is true.
!(6 <= 4) // evaluates to true because (6 <= 4) would be false.
!true // evaluates to false
!false // evaluates to true.

Ex7
Read the following.
Conditional operator ( ? )

7==5 ? 4 : 3 // returns 3, since 7 is not equal to 5.


7==5+2 ? 4 : 3 // returns 4, since 7 is equal to 5+2.
5>3 ? a : b // returns the value of a, since 5 is greater than 3.
a>b ? a : b // returns whichever is greater, a or b.

Prepared by: Mohammad H. Ryalat


2
Example:
int a,b,c;

a=2;
b=7;
c = (a>b) ? a : b;

cout << c;
Output: 7

Ex8
Read the following.
int i;
float f = 3.14;
i = (int) f;
cout<< i<<"\t"<<f;
Output: 3 3.14

Ex9
Read the following.
sizeof()
int a = sizeof (char);
int b = sizeof (long);
short c = 27;
int d = sizeof(c);
cout<<a<<"\t"<<b<<"\t"<<d;
Output: 1 4 2

Ex10
Read the following.
short is equivalent to short int and long is
equivalent to long int. The following two
variable declarations are equivalent:
short Year;
short int Year;

Ex11
Read the following.
int x =11;
cout<<x<<"\t"<< 50;
Output: 11 50

Prepared by: Mohammad H. Ryalat


3
Ex12
Read the following.
cin>>

You can use cin to request more than one datum input from the user:

cin >> a >> b;

is equivalent to:

cin >> a;
cin >> b;

Ex13
Read the following.
char m1 = 97, y =90;
cout<<m1<<"\t";
char m2 = ++y + 7;
cout << (int)m1;
cout<<"\t"<< m2;
Output: a 97 b

Ex14
Read the following.
bool bValue = true;
cout << bValue << endl; Output
cout << !bValue << endl; 1
0
0
bool bValue2 = false;
1
cout << bValue2 << endl;
cout << !bValue2 << endl;

Ex15
Which one is a legal identifier (correct variable name) and which one
is illegal identifier (incorrect variable name)?
1) int sum;
2) intm;
3) int cats=5, dogs=5;

5) int clouds=5, int trees;


6) int my variable name;
7) int length=3; int width=4;

9) int void = 5;
10) int nAngle;
11) int 3some;

Prepared by: Mohammad H. Ryalat


4
12) int meters_of_pipe;
13) int length, width=5;
Solutions
Errors happen in the followings:
2) You must add a space between int and m.

5) Compiler will complain about the int before trees.


6) Variable names can not contain spaces.

9) void is a keyword.
11) Variable names can not start with a number.

Ex16
Read the following.
One of the most common uses for boolean variables is inside if
statements:

bool bValue = true;


if (bValue)
cout << "bValue was true" << endl;
else
cout << "bValue was false" << endl;
Output:
bValue was true

Dont forget that you can use the logical not operator to reverse a boolean
value:

bool bValue = true;


if (!bValue)
cout << "The if statement was true" << endl;
else
cout << "The if statement was false" << endl;
Output:
The if statement was false

Prepared by: Mohammad H. Ryalat


5

You might also like