What We Know About Programming

we learned:
  1. About the main function, how it encloses the program in braces.
  2. That statements are separated with semicolons; like that.
  3. About declaring variables as ints or doubles.
  4. About assigning values to variables using assignment statements
  5. About ouputing text or data using cout.
  6. That comments make programs easier to understand.
  7. About compiling and running a program
  8. We know how to create output using a 'cout' statement
  9. We know how to input data from the keyboard using a cin statement.
  10. You should understand the syntax of an if statement.
  11. Understand the role of the conditional expression in the if statement.
  12. If the conditinal statement in an if statement evaluates to 'true' the following statement is executes.
  13. This statement is probably a compound statement (many statements grouped between opening and closing braces).
  14. If statements can occur within if statements. Its important to keep track of which goes to where.
  15. Good style says that you create both opening and closing braces at the same time. You label the closing brace with a comment and you increase the level of indentation within any pair of braces.
Some rules for braces:
  1. The number of opening braces is always equal to the number of closing ones.
  2. Ignoring everything but braces, where an opening brace is followed by a closing one they open and close the same compound statement. If we join then and mentally remove them that is all that is required to match up all the braces.
  3. There should never be a point in the program where there have been more closing braces than opening ones..
Look at the following sequences of braces. Confirm that there are equal numbers of both types.
{_ { _} _{ _{_ }_ }_ {_ }_ }
Match up the braces. Indicate "depth" (how many more opening ones than closing)

Look at the following program:

// File: eg1 week2.cpp

#include <iostream.h>

void main()
int i;
cout <<"enter number ";

cin >>i;


cout<<"You entered a big number "<<endl;

cout<<"You entered a small number "<<endl;


}// +ve
}// end of program

You can cut and paste it into your compiler if you want. It is very important that you understand what will happen if you enter a 3, am 8 or a 12.

New This Week: While Loops

A while loop in theory consists of the word 'while', a logical (or boolean) expression and a (compound) stements that will continue to be executed while the logical expression is true. In practice we usually have the following steps.
  1. Before the loop we usually set the values of the variables used in the logical expression.
  2. Then the 'while' with the logical expression and the opening brace.
  3. Next usually comes the meat of the loop. The calculations (assignemnt statements) or whatever that must be done many times. Often it will use the variables which are used in the logical expression.
  4. At some stage one or more variable used to control the loop is changed in such a way that the conditinal expression will at a desired time become nflase and we can escape from the loop.
The simplest loop looks something like this: // File: eg4 week2.cpp

#include <iostream.h>

void main()
int counter;
counter = 1; //set up loop
while (counter<101)
cout <<counter<< endl;// the repeated task

counter=counter+1;//update the counter so that eventually the conditional statement will be false
}//end of counter loop
}// end of program

Paste this program in and run it. You can now write a program to repeat a takes 100 times or a 1000,000 times or as many times as is necessary. If you go to the text file 'handout'. You will see some examples of loop

Example 0:Basic loop

Make sure yuou can identify the parts of the loop and what they do.

Example 1: Triangular Numbers

We met triangular numbers : 1, 1+2, 1+2+3, 1+2+3+4 when we were studying algorithms. Here is a way to calculate them (not an efficient way but still... An inportant technique is called tracing. To understand how this works you could add a cout stements in the loop that shows what happens to i,j and sum in the loop. The when you are done "comment it out".

Example 2 Factors

This program prints out all the factors of the number that you enter,. It has an "if" in the loop. Check the braces! Check that you understand how it works. 28 is a 'perfect' number. Try entering this. I bet that you have had a teacher who made you find all the factors of numbers and that this program would have been useful!

Example 3 Fibonacci numbers

Classic programming problem! Add some extra couts to see how it works.

Example 4 Modelling Epidemics

This is a simple example of how real computer modelling works.

Minor Additions

Equals in conditional statement

In conditinal statements a double equals signs is used to test if two quantities are equal, so (i == 3) is true if and only if i contains the value 3. Similarly != means "not equal to", so (i != 3) is true unless i contains the value 3.

Integer Division

Divide an integer by an integer and you aget an integer -the whole number of times that one number goes into the other. 9/4 will give 2. There is also a remainder or 'mod' operator % that gives the remainder so 12 % 5 gives 2 because 5 goes into 12 twice REMAINDER 2. If a number exactly divides another then the remainder is zero and we can test for this as:
if((i%j)==0)cout<<"J is a factor of i "<

Next Week: Arrays

It turns out that instead of allocating memory by declaring variables one at a time we can get a block of varaible all of the same type and the same name except numbered. So the variable declaration
int i[5];
means that I have declared 5 integers called i[0], i[1], i[2], i[3] and i[4]. There is a good reason that they start numbering at 0. However, errors resulting from this are estimated to cause the industry more than a billion dollars per year. The number refering to the store is called an 'index'. Often a variable or an expression is used when accessing or assigning to members of an array. The following example puts square numbers in an array. int i[5];
int count;
count = 0;

Text file handout
Home Page of RDScience