Friday, February 2, 2007

Introduction to C++ Programming [Part 4]

Previous Related Posts: Part1 Part2 Part3

Operators

An operator is a symbol that causes the compiler to take an action. Operators act on one or more operands. There are several types of operators.

· Arithmetic operators

· Assignment operators

· Relational operators

· Logical operators

Arithmetic operators

Operator

Description

Example

+

Addition

3 + 20 evaluates to 23

-

Subscription

10 – 4 evaluates to 6

*

Multiplication

2 * 13 evaluates to 26

/

Division

15 / 2 evaluates to 7

%

Modulus

17 % 3 evaluates to 2

++

Increment

i++ is equivalent to i = i + 1

--

decrement

i-- is equivalent to i = i - 1

++ and -- are unary operators, meaning that they operate on a single operand. Each of these operators has two versions, post and pre. Let’s try to understand this by the following example.

e.g.:

//post increment
x = 10;
y = 20;
z = (x++) + y; //z evaluates to 30
a = x + y; // a evaluates to 31

//pre increment
x = 10;
y = 20;
z = (++x) + y; //z evaluates to 31
a = x + y; // a evaluates to 31

Operator precedence

Precedence is the order in which a program performs the operations in a formula. Each operator has a precedence value. These precedence values are used to evaluate expressions. If one operator has precedence over another operator, it is evaluated first. The following table shows the operator precedence in the decreasing order.


( )

Prefix ++ --

* / %

+ -

< <= > >=

== !=

||

&&


e.g.:

z = 2 + 5 * 3; //17
z = (2 + 5) * 3; //21

Assignment operators

Operator

Example

=

a = 5;

*=

a *= 5; //equivalent to a = a * 5;

+=

a += 5; //equivalent to a = a + 5;

-=

a -= 5; //equivalent to a = a - 5;

/=

a /= 5; //equivalent to a = a / 5;

%=

a %= 5; //equivalent to a = a % 5;

Relational operators

Operator

Description

==

Equal to

<=

Less than or equal to

>=

Greater than or equal to

<

Less than

>

Greater than

!=

Not equal to

Logical operators

Operator

Equivalent to

Example

&&

AND

(x > 5 ) && (x <>

||

OR

(x <> 25) //x is less than 5 or greater than 25

!

NOT

!(x > 5) //equivalent to (x <= 5)


Escape Characters

The C++ compiler recognizes some special characters for formatting. The following table shows the most common ones. You put these into your code by typing the backslash (called the escape character), followed by the character.

Character

Description

\n

new line

\t

tab

\b

backspace

\"

double quote

\'

single quote

\?

question mark

\\

backslash

\a

alert

e.g.:

cout << “This is a test. \n”;
cout << “Now alarm \a will ring\n.”;

Control Structures

If Statement

If statement is used to execute a certain set of statements based on an expression. There are several forms of if statement. Most commonly used forms are listed below. You don’t need to use braces if there is only one statement.

if (expression)
{
//statements
}

if (expression)
{
//statements
}
else
{
//statements
}

if (expression)
{
//statements

}
else if (expression2)
{
//statements

}

e.g.:

if (x != 0)
{
cout <<”x is not equal to zero\n”;
}
else
{
cout <<”x is equal to zero\n”;
}

Conditional (Ternary) operator:

The conditional operator (?:) is C++'s only ternary operator; that is, it is the only operator to take three terms. The conditional operator takes three expressions and returns a value.

Syntax:

(expression1) ? (expression2) : (expression3)

This line is read as "If expression1 is true, return the value of expression2; otherwise, return the value of expression3." Typically, this value would be assigned to a variable.

e.g.:

int x = 5;
int y = 10;
int z = (x > y) ? y : x; //since x > y is false, value of x is assigned to z.

While loop

A condition is tested, and if it is true, the body of the while loop is executed. When the conditional statement fails, the entire body of the while loop is skipped. It is possible that the body of a while loop will never execute. The while statement checks its condition before executing any of its statements, and if the condition evaluates false, the entire body of the while loop is skipped.

Syntax:

while (condition)
{
//statements
}

e.g.:

int iCount = 0;
while (iCount < 2)
{
cout << “Count is “ << iCount <<"\n";

iCount++;
}

Output:

Count is 0
Count is 1
continue
and break statements:

Usually break statement is used to break out of any loop based on a certain condition and continue statement is used to skip the current iteration based on a certain condition.

e.g1. (break):

int iCount = 0;
while (iCount < 2)
{
cout << “Count is “ << iCount <<"\n";
if ( iCount == 0)
{
cout <<”Breaking out of the loop\n”;
break;
}
iCount++;
}

Output:

Count is 0
Breaking out of the loop

e.g2. (continue):

int iCount = 0;
while (iCount < 3)
{
cout << “Count is “ << iCount <<"\n";
iCount++;
if ( iCount == 1)
{
cout <<”Continue with next loop\n”;
continue;
}

cout << ”Next count is “ <<iCount << "\n";

}

Output:

Count is 0

Continue with next loop
Count is 1
Next count is 2
Count is 2
Next count is 3

Do-While Loop

The do-while loop executes the body of the loop before its condition is tested and ensures that the body always executes at least once.

Syntax:

do
{
//statements
}
while (condition);

e.g.:

int iCount = 0;
do
{
cout << “Count is “ << iCount << "\n";

iCount--;
}
while (iCount > 0);

Output:

Count is 0

for Loop

You'll often find yourself setting up a starting condition, testing to see if the condition is true, and incrementing or otherwise changing a variable each time through the loop. In such situations, for loops are more convenient way to iterate. A for loop combines the three steps into one statement. The three steps are initialization, test, and increment.

Syntax:

for (initialization; test; increment/decrement)

{

//statements

}

e.g.:

for (int iCount = 0; iCount < 2; iCount++)

{

cout << “Count is “ << iCount <<"\n";

}


Output:
Count is 0
Count is 1

switch Statement

switch statement is a more convenient way of branching than if statement, when you want to branch to a set of statements based on several possible values.

Syntax:

switch (expression)
{
case value1:
//
statements
break;
case value2:
//statements
break;

default:
//statements
}


Expression is any legal C++ expression. If one of the case values matches the expression, execution jumps to those statements and continues to the end of the switch block, unless a break statement is encountered. If nothing matches, execution branches to the optional default statement. It is important to note that if there is no break statement at the end of a case statement, execution will fall through to the next case statement. This is sometimes necessary, but usually is an error.

e.g.:

int iScore; //assume iScore is assigned a value through cin

cout << “Score is “ << endl;

switch (iScore)

{

case 5:

cout << “Performance is average” << endl;

break;

case 10:

cout << “Performance is good” << endl;

break;

default:

cout << “Invalid Score” < < endl;

}

1 comment:

Blogger said...

If you need your ex-girlfriend or ex-boyfriend to come crawling back to you on their knees (no matter why you broke up) you must watch this video
right away...

(VIDEO) Why your ex will NEVER come back...