Upload
alicia-pierce
View
218
Download
2
Tags:
Embed Size (px)
Citation preview
CHAPTER 2PART #1C++ PROGRAM STRUCTURE
1st Semester 2014/2015
1King Saud University College of Applied studies and Community ServiceCsc 1101By: Asma AlosaimiEdited By: Rabab Almobty, Fatimah Alakeel, Noor Alhareqi,Nouf Almunyif
What’s a C++ program ?
A C++ program is a text file containing a sequence of C++ commands put together according to the laws of C++ grammar.
This text file is known as the source file Source file carries the extension .CPP The point of programming in C++ is to write a
sequence of commands that can be converted into a machine-language program that actually does what we want done.
2
How do I program? To write a program, you need two
specialized computer programs. One (an editor) is what you use to write your code as you
build your .CPP source file. The other (a compiler) converts your source file into a
machine-executable .EXE file that carries out your real-world commands
Nowadays, tool developers generally combine compiler and editor into a single package — a development environment. After you finish entering the commands that make up your program, you need only click a button to create the executable file.
3
The best way to learn to
program is by writing
programs !
C++ Program StructurePage 4
// first c++ program
#include <iostream> // Preprocessor Commands
int main( ) // main function
{ // Declaration section – Declare needed variables
…...
// Input section – Enter required data
…..
// Processing section – Processing Statements
…...
// Output section – Display expected results
…....
return 0;
}// end main
comment
C++ is case
sensitive
Comments are used to describe what your code does and aid reading your code.
C++ compiler ignores them. Comments are made using
// for single line comment, which comments to the end of the line.
or /* */ for multi line comment, everything inside of it is considered a comment . The comment begins after the first /*. It ends just before the first */.
Examples:/* This comment begins at this line.This line is included in this commentIt ends at this line. */
// This comment starts here and ends at the end of this line
5
A - Comments
Every program should begin with a comment describes the purpose of a program ,the author and any other necessary information
B – Preprocessor Directive
#include < iostream >
is a directive to the C++ preprocessor.
Lines beginning with # are processed by the preprocessor before the program is compiled.
#include < iostream > tells the preprocessor to include the contents of the input/output stream header (< iostream >) in the program.
This header must be included for any program that outputs data to the screen or inputs data from the key board using C++’s stream input/output.
6
C – main function
int main() The main function is a part of every C++ program. C++ programs contain one or more functions, one of which
must be main. Every program in C++ begins executing at the function
main. The keyword int to the left of main indicates that main
“returns” an integer (whole number) value. The left brace { must begin the body of every function . A corresponding right brace } must end each function’s
body.
7
A keyword is a word in code that is reserved by C++ for a specific use.
D – Exit
return 0;
is included at the end of every main function.
The keyword return is one of several means we’ll use to exit a function.
When the return statement is used at the end of main as shown here, the value 0 indicates that the program has terminated successfully.
8
First Program in C++: Printing a Line of Text
9
// hello world programe
#include <iostream>
int main( ) { // main function
//No Declaration, Input ,Processing sections
// Output section
std::cout << “Hello World \n"; // Display a message
return 0;
}// end main Hello WorldThe output
HELLO WORLD !
Output Statement
std::cout << “Hello World \n”;
The entire line is called a statement.
Each statement in C++ needs to be terminated with semicolon (;)
This instructs the computer to perform an action, namely to print on the screen the string of characters marked by the quotation marks.
10
Preprocessor directives (like #include) do not end with a semicolon.
Output Statement
The std:: before cout is required when we use names that we’ve brought into the program by the preprocessor directive #include<iostream>.
The notation std::cout specifies that we are using a name, in this case cout, that belongs to “namespace” std.
We can avoid writing the std:: by writing using namespace std; as shown in following.
11
First Program in C++: Printing a Line of Text
12
// hello world programe
#include <iostream>
using namespace std;
int main( ) { // main function
//No Declaration, Input ,Processing sections
// Output section
cout << “Hello World \n";
return 0;
}// end main
Hello WorldThe output
HELLO WORLD !
Output Statement
The << operator is referred to as the stream insertion operator. When this program executes, the value to the operator’s right, the right operand, is inserted in the output stream.
The right operand’s characters normally print exactly as they appear between the double quotes. However, the characters \n are not printed on the screen. The backslash (\) is called an escape character. It indicates that a “special” character is to be output.
13
Output Statement
When a backslash is encountered in a string of characters, the next character is combined with the backslash to form an escape sequence. The escape sequence \n means new line. It causes the cursor (i.e., the current screen position indicator) to move to the beginning of the next line on the screen.
14
Printing a Single Line of Text with Multiple Statements
15
// Preprocessor Commands
#include <iostream>
using namespace std;
int main( ) // main function
{
// Output section
cout << “Hello“
cout << “World\n";
return 0;
}// end main
The output Hello World
Printing Multiple Lines of Text with a Single Statement
16
// Preprocessor Commands
#include <iostream>
using namespace std;
int main( ) // main function
{
// Output section
cout << “Hello \n World \n";
return 0;
}// end mainThe output
Hello World
Steps to Write, Compile, and Run a Program
Editor Source code or file (.cpp)
Compile file Object code or file (.obj)
Linker Executable code or file (.exe)
Loader Executable code is loaded into memory ready for execution
17
18Reference : C++ How to Program (8th Edition) , By: Deitel& Deitel