Showing posts with label Unit 5. Show all posts
Showing posts with label Unit 5. Show all posts

Unit 5 - Compiles And Run Program

Posted on Saturday, May 8, 2010 by B[H] | 0 comments
Labels:

To run a C++ program, we must first create it, then compile it, then link it with other modules ( or other compiled programs), and finally run it. You can create a C++ program using the C++ editor. The editor is like a word processor that allows you to
type, edit and save your program. The program you create is called the source module.


After you have created the source program, you compiled it using the C++ compiler. The compiler translates your C++ instructions into a machine-readable form. The compiled program is called the object module.
Besides compiling the source module into an object module, the compiler also generates information necessary for linker. The linker links the object module generated by the compiler and any other object modules, your program may request into a final executable module.


Photobucket

Unit 5 - Repetition (or Iteration) structure

Posted on by B[H] | 0 comments
Labels:

The repetition (or iteration) structure permits a sequence of the instructions to be executed repeatedly until certain condition is reached. The repetition structure or loop in C++ comes in three forms while, do-while and for.

a) The while loop

The while loop repeats the body of the loop as long as the loop condition holds. The basic form of the while statement is as below:

while (expression) statement;



In this loop, the expression is first evaluated. If it is true (not zero), the statement (which can be a block is executed; if it is (zero), the statement is bypassed.


Photobucket

Photobucket





b) The do-while loop


The do-while loop executes a statement as long as the loop condition holds. The basic form of the do-while statement as below:

Do
statement
while (expression) ;



This do-while loop is quite similar to the while loop except that the expression is evaluated after the statement is executed. This means the statement in the do-while will be executed at least once. In the while statement, the statement will not be executed if the expression is false.

Photobucket

Photobucket



c) The for Loop

The for loop repeats the body of the loop as long as the loop condition holds. The basic form of the for loop as below:

for (initialization; condition test; incrementation)
{
statements;
}


• The initialization part typically refers to the initial value (if any) given to a loop variable or counter. But it can also include the initialization of any other variable. This initialization part is carried out just once at the beginning of the loop.
• The expression part determines whether the loop execution should be continued. If the expression is zero (false), the for loop is terminated, if it is not zero (true), the for statement is executed.
• The incrementation part typically increments (or decrements) the loop counter variable. This is done after the for statement is executed. This part, like the initialization part, can also include the incrementation of other variables.

Photobucket

The example 5.9, given is a very short program and it is easy for us to understand the for loop.
First, an integer variable is declared. Then, in the initialization part, the variable, iNum is set to 1. For the condition checking, iNum is checked to see whether it is equal to or less than 10. In each cycle of the loop, iNum is incremented by 1. Once iNum reaches 10, the loop exits. We can see that the program calculates the square of the first ten natural numbers.

Program output:

1 4 9 16 25 36 49 64 81 100

Unit 5 - Logical Structures And Basic Instructions (statement)

Posted on by B[H] | 0 comments
Labels:

C++ has a set of rich and powerful control structures (statements) that makes it a popular language. Control structure generally fall into four categories but in this unit we have to know only three of them, which are:

i. sequence structure
ii. selection structure
iii. repetition or iteration structure



a-Sequence structure

The sequence control structure is the simplest of all the structures. The program instructions are executed one by one, starting from the first instruction and ending in the last instruction as in the program segment.


Photobucket



b-Selection Structure

The selection structure allows to be executed non-sequentially. It allows the comparison of two expressions, and based on the comparison, to select a certain course of action. In C++ , there are three types of selection statements. They are:
i. if statement
ii. if-else statement
iii. Switch statement.

i. if Statement

The basic form of the if statement is:

If (expression) statement


In this form, the expression is first evaluated. If the expression evaluates to non-zero (meaning TRUE), the statement is executed; if it evaluates to zero (meaning FALSE), the statement following the if statement is executed. Again, it has one entry point and one exit point.

Photobucket

Photobucket



ii. if – else statement

The basic form of the if-else statement is :

Photobucket

In this form, the expression is first evaluated. If it evaluates to non-zero, statement_1 is executed, otherwise (i.e, if it evaluates to zero) statement_2 is executed. The execution of the statements are mutually exclusive, meaning, either statement_1 is executed or statement_2, but not both. The statement can, of course, take the form of blocks.

Photobucket

Photobucket

The if – else statement in the Example5.4 is to checks the value of iNum to be not equals to zero and also that the modulus value of the variable iNum is equal to zero. If condition is true, then the message ‘Even Number’ is printed on the screen. If condition is not true, then the message ‘Odd Number or Number is 0’ is printed on screen.



iii-Switch statement

The switch statement is the multiple branch decision statement is sometimes called the multiple-choice statement. The general form of the switch statement is :

Photobucket

The expression evaluates to an integer or character constants and statement sequence is a block of statements.

Photobucket

example:

Photobucket

The sample output of the program:

Please key in grade (A-F) : C
Minimun marks is 40

In the example above, A,B,C and D are the possible values that can be assigned to the variable cGrade.
In each case of the constants, A to D, a statement or sequence of statements would be executed.



You would have noticed that every line has statement under it called “break”. The break is the only thing that stops a case statement from continuing all the way down through each case label under it. In fact, if you do not put break in, the program will keep going down in the case statements, into other case labels, until it reaches the end of a break.
However, the default part is the codes that would be executed if there were no matching case of constant’s values.

Unit 5 - Introduction To Logical Structure

Posted on by B[H] | 0 comments
Labels:

Logical structure or program control structures refer to the order of execution of instructions in a program. So far, in all our examples, the instructions were executed sequentially one by one, from the top downwards. However, most real life problems require some kind of decision making, which involves comparing values, and based on the comparison, to take a certain course of action. Hence, C++ provides structure that will allow the non-sequential execution of program instructions. This means that an instruction or a whole block of instructions can be executed, repeated or skipped.

Unit 5 - Logical Structures And Basic Instructions

Posted on by B[H] | 0 comments
Labels:

General Objective : To Understand the logical structure types and basic instruction
refers to the order of execution of instructions in program.

Specific Objectives : At the end of the unit you will be able to :

- describe the sequence structure
- describe the selection structure
- describe the Repetition structure
- explain the program control structures
- write the simple program using program control structure