Friday, August 29, 2014

Pre-increment Vs. Post-Increment, every programming student must know.

Most C/ C++ programmers are completely clear about the behavior of the ++ operator [and --] and the result of expressions that employs it. However, what if the return value is unused? Does it matter whether the code uses ++i or i++? This article looks at the finer points of compiler code generation and why that choice might matter.

Although any C programmer could easily learn another programming language and quickly become proficient in it, C continues to be the dominant language for embedded software programming. One reason for this is the large reservoir of expertise in the language, and most embedded C programmers understand the low-level nuts and bolts of the language. However, as embedded developers are always concerned with writing efficient code and minimizing overheads, being “proficient” is not enough.

There are still facets that may come as a surprise, even to those with years of experience.
A case in point is the pre-increment/post-increment problem. Not long ago, I was working on a conference paper about using C++ for embedded applications. I sent the paper out to the embedded compiler and programming language experts on the Mentor Embedded development tools team and asked them to review my work. As expected, I got some detailed and helpful feedback, but I was left wondering about one particular suggestion. I had written something like this:

  for (i=0; i<10; i++)
  ...

This seemed straightforward, but I was told that in terms of C++ style, it would be better to write this:
  for (i=0; i<10; ++i)
  ...


i++ and ++i

We are all familiar with the ++ operator (and its sibling --). Applying this operator increments the variable. Well, that is what appears to happen. Let’s look more closely.
++ is really a pair of operators: pre-increment and post-increment. The former increments the value of a variable and returns the resulting value; the latter increments the value of the variable and returns the value prior to the increment. So, if a variable x initially contains the value 3, the following expressions yield different results:

  y = ++x; // y==4, x==4
  y = x++; // y==3, x==4


The effect on x is the same in both cases, but y gets a different value.
This is all straightforward. The programmer needs to be careful to use the correct operator to achieve the required result in an expression.

Program Tracing #1

What is the output of this program?

class Increment
{
public static void main(String arg[])
{
int g = 3;
System.out.print(++g * 8);
}
}


a) 25
b) 24
c) 32
d) 33

What is a Pseudocode?

"Pseudocode is an artificial and informal language that helps programmers develop algorithms."

Pseudocode programs are not actually executed on computers. Rather, they help the programmer "think out" a program before attempting to write it in a programming language, such as Java or C++. Pseudocode normally describes only executable statements -- the actions that are performed when the program is converted from pseudocode to any programming language and is run. Declarations are not executable statements. For example, the declaration

            int i;

tells the compiler the type of variable i and instructs the compiler to reserve space in memory for the variable. This declaration does not cause any action -- such as input, output or a calculation -- to occur when the program is executed. Some programmers choose to list variables and mention the prpose of each at the beginning of a pseudocode program.

For Viral Stuff and Trending News please visit www.fooviral.com

Thursday, August 28, 2014

The do-while Loop Structure

The do-while repetition structure is similar to the while structure. In the while structure, the program tests the loop-continuation condition at the beginning of the loop, before performing the body of the loop. The do-while structure tests the loop-continuation condition after performing the body of the loop; therefore, the loop body always executes at least once. When a do-while structure terminates, execution continues with the statement after the while clause. Note that it is necessary to use braces in the do-while structure if there is only one statement in the body. However, most programmers include the braces, to avoid confusion between the while and do-while structure. For example,

while ( condition )

normally is the first line of a while structure. A do-while structure with no braces around a single-statement body appears as

do
      statement
while ( condition );

which can be confusing. Reader may misinterpret the last line- while (condition ); - as a while structure containing an empty statement (the semicolon by itself). Thus, to avoid confusion, the do-while structure with one statement often is written as follows:

do{
  statement
} while ( condition );


Example:

int i = 1;
do{
   cout << (i++) << endl;
}while (i <= 10);

will display the following pattern:
1
2
3
4
5
6
7
8
9
10

Do it yourself:

Problem #1:
Write a program to display the following pattern using do-while loop structure:
0 3 6 9 12 15 18 21

Answer:
int i = 0;
do{
  cout << i << " ";
  i+=3;
}while ( i <= 21);


Problem #2:
Translate the following code snippet using a do-while structure:

int i=1;
while ( i <= 5){
   cout << i << " ";
   i++;
}

Answer:
int i=1;
do{
  cout << i << " ";
  i++;
}while (i <=5);

*************************************************

Lab Activity No. 2
Midterm

Problem:
Write a program to display a multiplication table using do-while loop.

Expected output:
12345678910
2468101214161820
36912151721242730
481216202428323640

Note: I will check your program on Tuesday next week.


Check some Viral Stuff and Trending News.

Wednesday, August 27, 2014

The for Repetition Structure

The for repetition structure handles all of the details of counter-controlled repetition. The first line is sometimes called the for structure header. Notice that the for structure "does it all": It specifies each of the items needed for counter-controlled repetition  with a control variable. If there is more than one statement in the body of the for structures, braces ( { and } ) are required to define the body of the loop.

for ( int counter=1; counter <= 10; counter++ )

where:

  1. for is a keyword
  2. counter is the control variable
  3. 1 is the initial value of control variable
  4. 10 is the final value of control variable
  5. counter <= 10 is the loop-continuation condition
  6. counter++ is the increment of control variable

The general format of the for structure is
  
  for (expression1; expression2; expression3 )
        statement

where expression1 names the loop's control variable and provides its initial value, expression2 is the loop-continuation condition (containing the control variable's final value) and expression3 modifies the value of the control variable, so that the loop-continuation condition eventually becomes false. In most cases, the for structure can be represented with an equivalent while structure, with expression1, expression2 and expression3 placed as follows:

expression1;
while ( expression2 ) {
   statement;
   expression3;
}