C++ programs typically go through six phases: edit, preprocess, compile, link, load and execute. The following discussion explains a typical C++ program development environment.
Creating a Program
Phase 1 consists of editing a file with an editor program, normally known simply as an editor . You type a C++ program (typically referred to as source code) using the editor, make any necessary corrections and save the program on a secondary storage device, such as your hard drive. C++ source code file names often end with the .cpp, .cxx, .cc or .C extensions (note that C is in uppercase) which indicate that a file contains C++ source code. See the documentation for your C++ compiler for more information on file-name extensions. C++ software packages for Microsoft Windows such as Microsoft Visual C++ (microsoft.com/express) have editors integrated into the programming environment. You can also use a simple text editor, such as Notepad in Windows, to write your C++ code.
Preprocessing a C++ Program
In Phase 2, you give the command to compile the program. In a C++ system, a preprocessor program executes automatically before the compiler’s translation phase begins (so we call preprocessing Phase 2 and compiling Phase 3). The C++ preprocessor obeys commands called preprocessor directives, which indicate that certain manipulations are to be performed on the program before compilation. These manipulations usually include other text files to be compiled, and perform various text replacements. The most common preprocessor directives are discussed in the early chapters; a detailed discussion of preprocessor features appears in Appendix E, Preprocessor.
Compiling a C++ Program
In Phase 3, the compiler translates the C++ program into machine-language code—also
referred to as object code.
Linking
Phase 4 is called linking. C++ programs typically contain references to functions and data
defined elsewhere, such as in the standard libraries or in the private libraries of groups of programmers working on a particular project. The object code produced by the C++
compiler typically contains “holes” due to these missing parts. A linker links the object code
with the code for the missing functions to produce an executable program (with no missing
pieces). If the program compiles and links correctly, an executable image is produced.
Loading
Phase 5 is called loading. Before a program can be executed, it must first be placed in memory. This is done by the loader, which takes the executable image from disk and transfers it to memory. Additional components from shared libraries that support the program are also loaded.
Execution
Finally, the computer, under the control of its CPU, executes the program one instruction at a time. Some modern computer architectures can execute several instructions in parallel.
Condition
We now introduce a simple version of C++’s if statement that allows a program to take alternative action based on whether a condition is true or false. If the condition is true, the statement in the body of the if statement is executed. If the condition is false, the body statement is not executed. We’ll see an example shortly. Conditions in if statements can be formed by using the equality operators and relational operators summarized in Fig. 2.12. The relational operators all have the same level of precedence and associate left to right. The equality operators both have the same level of precedence, which is lower than that of the relational operators, and associate left to right. The following example (Fig. 2.13) uses six if statements to compare two numbers input by the user. If the condition in any of these if statements is satisfied, the output statement associated with that if statement is executed.
#include <iostream>
using namespace std;
int main()
{
int num1,num2;
cout<<"Enter Two integers:";
cin>>num1>>num2;
if(num1==num2)
cout<<num1<<"=="<<num2<<endl;
if(num1!=num2)
cout<<num1<<"!="<<num2<<endl;
if(num1<num2)
cout<<num1<<"<"<<num2<<endl;
if(num1>num2)
cout<<num1<<">"<<num2<<endl;
if(num1>=num2)
cout<<num1<<">="<<num2<<endl;
if(num1<=num2)
cout<<num1<<"<="<<num2<<endl;
system("pause");
return 0;
}
Loops
Why Is Repetition Needed? Repetition allows you to efficiently use variables. Can input, add, and average multiple numbers using a limited number of variables. For example, to add five numbers: Declare a variable for each number, input the numbers and add the variables together. Create a loop that reads a number into a variable and adds it to a variable that contains the sum of the numbers.
The general form of the while statement is:
while is a reserved word.
Statement can be simple or compound.
Expression acts as a decision maker and is usually a logical expression.
While is a pre-test loop.
Statement is called the body of the loop.
The parentheses are part of the syntax .
Infinite loop: continues to execute endlessly. Avoided by including statements in loop body that assure exit condition is eventually false.
Case 1: Counter-Controlled while Loops
If you know exactly how many pieces of data need to be read, while loop becomes a counter-controlled loop with loop control variable (LCV)
#include<iostream.h>
int main()
{
int total, //sum of grades
gradeCounter, //number of grades entered
grade, //one grade
average; //average of grades
//initialization phase
total = 0; //clear total
gradeCounter = 1; //prepare to loop
//processing phase
while (gradeCounter <= 10) //loop 10 times
{
cout << "Enter grade: "; //prompt for input
cin >> grade; //input grade
total = total + grade; //add grade total
gradeCounter = gradeCounter +1; //increment counter
}
//termination phase
average = total/10; //integer division
cout << "Class average is " << average <<endl;
return 0; //indicate program ended successfully
}
Sentinel variable is tested in the condition
Loop ends when sentinel is encountered
while(score!=-1)
{
cout<<"Good Bye "<<endl;
cin>>score;
}
A flag-controlled while loop uses a bool variable to control the loop.
The flag-controlled while loop takes the form:
boolean done=false;
while(!done)
{
//read some input
if(condition)
done=true;
else
{
//process input
}
}
do...while Looping (Repetition) Structure
General form of a do...while:
do
{
statements;
}
while(condition)
The statement executes first, and then the expression is evaluated.
To avoid an infinite loop, body must contain a statement that makes the expression false.
The statement can be simple or compound.
This loop is post-test loop.
Loop always iterates at least once.
For(Loops)
for Looping (Repetition) Structure: -
The general form of the for statement is:
for(initialization;test condition;increment)
{
//Body of loop
}
Example:-
for(int x=0;x<=10;x++)
{
cout<<x;
}
The initial statement,loop condition, and update statement are called for loop control statements.
Initial statement usually initializes a variable (called the for loop control, or indexed, variable)
In C++, for is a reserved word. If test is false the first time it is evaluated, the body of the loop will not be executed.The any update expression can increment or decrements by amount.Variables used in the initialization section should not be modified in the body of the loop.
int sum = 0, num;for (num = 1; num <= 10; num++)
{
sum += num;
cout << "Sum of numbers 1 – 10 is ";
cout<< sum << endl;
}
No comments:
Post a Comment