You are on page 1of 27

Structure of a Program

C++ Basic
My First Program




int main()
{




}

My First Program




int main()
{
Welcome to ENGR 112 !



}

My First Program




int main()
{
cout Welcome to ENGR 112 !



}

My First Program




int main()
{
cout Welcome to ENGR 112 !



}

My First Program




int main()
{
cout << Welcome to ENGR 112 !



}

My First Program




int main()
{
cout << Welcome to ENGR 112 ! ;



}

My First Program

#include <iostream>


int main()
{
cout << Welcome to ENGR 112 ! ;



}

My First Program

#include <iostream>


int main()
{
std :: cout << Welcome to ENGR 112 ! ;



}

My First Program

#include <iostream>
using namespace std;

int main()
{
cout << Welcome to ENGR 112 ! ;



}

My First Program

#include <iostream>
using namespace std;

int main()
{
cout << Welcome to ENGR 112 ! ;


return 0;
}

My First Program

#include <iostream>
using namespace std;

int main()
{
cout << Welcome to ENGR 112 ! ;


return 0;
}

Welcome to ENGR 112!
General form of a C++ program
// Program description
#include directives
using namesapce std;
int main()
{
variable declarations
executable statements
return 0; //ending the program
}
Important Notes about C++
C++ is a case sensitive language
C++ contains many reserved words
Each keyword has a predefined purpose in the
language.
We shall cover the following keywords in this class:
bool, break, case, char, const, continue,
do, default, double, else, false, float,
for, if, int, long, namespace, return,
short, static, struct, switch, true,
void, while
using namespace std; Otherwise you have to use std::cout
C++ is case sensitive. Cout is different than cout.
1 // Fig. 1.2: fig01_02.cpp
2 // A first program in C++.
3 #include <iostream>
4 using namespace std;
5 /* function main begins program execution */
6 int main()
7 {
8 cout <<Welcome to ENGR 112!\n;
9
10 return 0; // indicate that program ended successfully
11
12 } // end function main

Welcome to ENGR 112!
Single-line comments. Ignored
by Compiler
Preprocessor directive which tells the
pre-processor to include input/output
stream header file <iostream>.
Function main appears exactly
once in every C++program..
Function main returns an integer value.
Left brace { begins function body.
Corresponding right brace } ends function body.
Statement is a simple or compound
expression for computer to perform, end with
a semicolon(;)
Name cout belongs to namespace
std.
Stream insertion operator. cout is a standard output stream
Keyword return is one of several means to exit function;
value 0 indicates program terminated successfully.
Program Output
1 #include <iostream>
2 using namespace std;
3 int main()
4 {
5 cout << Welcome to ENGR 112 !\n;
6 return 0; // indicate that program ended successfully
7 } // end function main

Program Output
1 #include <iostream>
2 using namespace std;
3 int main()
4 {
5 cout << \n;
6 return 0; // indicate that program ended successfully
7 } // end function main

Welcome to C++!
C++ comments
Comments are explanatory notes;
Increase the program readability
They are ignored by the compiler

There are two ways to include comments in a
program:
// single line comment: starts with
// double forward slash signs to the end of
// that line.

/* Block Comment: A slash followed by an
asterisk marks the start of a multiple line
comment. It ends with an asterisk followed by
a slash. */

What we should add if we take out using namespace std?
1 // Fig. 1.4: fig01_04.cpp
2 // Printing a line with multiple statements.
3 #include <iostream>
4 using namespace std;
5 // function main begins program execution
6 int main()
7 {
8 cout <<Welcome ";
9 cout <<to ENGR 112!";
10
11 return 0; // indicate that program ended successfully
12
13 } // end function main


Welcome to ENGR 112!
Unless new line '\n' is explicitly
specified, the text continues on
the same line.
1 / / Fig. 1.5: fig01_05.cpp
2 / / Printing multiple lines with a single statement
3 #include <iostream>
4 using namespace std;
5 / / function main begins program execution
6 int main()
7 {
8 cout << Welcome to ENGR 112";
9
10 return 0; // indicate that program ended successfully
11
12 }/ / end function main

Welcome
to

ENGR 112
1 / / Fig. 1.5: fig01_05.cpp
2 / / Printing multiple lines with a single statement
3 #include <iostream>
4 using namespace std;
5 / / function main begins program execution
6 int main()
7 {
8 cout << Welcome\ nto\ n\ nENGR 112\ n";
9
10 return 0; // indicate that program ended successfully
11
12 }/ / end function main

Welcome
to

ENGR 112
Using newline characters to print
on multiple lines.
1 / / Fig. 1.5: fig01_05.cpp
2 / / Printing multiple lines with a single statement
3 #include <iostream>
4 using namespace std;
5 / / function main begins program execution
6 int main()
7 {
8 cout << "Welcome<<endl<<to\ n;
9 cout << endl<<ENGR 112<<endl;

10 return 0; // indicate that program ended successfully
11
12 }/ / end function main

Welcome
to

ENGR 112
23
Escape Sequences




Escape
Sequen
Description Example Output
\n Newline. Position the screen cursor
to the beginning of the next line.
cout<<Hello \n World; Hello
World
\t Horizontal tab. Move the screen
cursor to the next tap stop.
cout<<Hello \t World; Hello World
\r Carriage return. Position the
screen cursor to the beginning of
the current line.
cout<<Hello \r Word; Word
\a Alert. Sound the system bell cout<<Hello \a; Beep
\\ Backslash. Used to print backslash
character
cout<<hello \\ World; Hello \ World
\ Double quote. Used to print double
quote character
cout<<\hello\ ; hello
Program Errors
Syntax errors
Violation of the grammar rules of the language
Discovered by the compiler
Error messages may not always show correct location of
errors
Run-time errors
Error conditions detected by the computer at run-time
Logic errors
Errors in the programs algorithm
Most difficult to diagnose
Computer does not recognize an error
Program Errors
1 /* Please find out the ERRORS,
2 Enjoy*/
3 #include (isotream);
4 using name space std
5 // function main begins program execution
6 int main{ }
7 {
8 cout >> C++ ,
9 cout >> is fun!\n ,
10
11 return ; // indicate that program ended successfully
12
13 } // end function main


Program Errors
1 /* Program without Errors,
2 Enjoy*/
3 #include <iostream>
4 using namespace std;
5 // function main begins program execution
6 int main()
7 {
8 cout << C++ ;
9 cout << is fun!\n ;
10
11 return 0; // indicate that program ended successfully
12
13 } // end function main


C++ is fun!
What makes a bad program?
Writing Code without detailed analysis and
design

Repeating trial and error without
understanding the problem

Writing tricky and dirty programs

You might also like