introduction to c++ // program description #include directives int main() { constant declarations...
TRANSCRIPT
![Page 1: Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return](https://reader035.vdocuments.site/reader035/viewer/2022062309/56649edb5503460f94beb5c9/html5/thumbnails/1.jpg)
Introduction to C++
![Page 2: Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return](https://reader035.vdocuments.site/reader035/viewer/2022062309/56649edb5503460f94beb5c9/html5/thumbnails/2.jpg)
// Program description
#include directives
int main()
{
constant declarations
variable declarations
executable statements
return 0;
}
![Page 3: Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return](https://reader035.vdocuments.site/reader035/viewer/2022062309/56649edb5503460f94beb5c9/html5/thumbnails/3.jpg)
• Keywords appear in blue in Visual C++. • Each keyword has a predefined purpose in the
language. • Do not use keywords as variable and constant
names!!• The complete list of keywords is on page 673 of
the textbook.• We shall cover the following keywords in this class: bool, break, case, char, const, continue, do, default, double, else, extern, false, float, for, if, int, long, namespace, return, short, static, struct, switch, typedef, true, unsigned, void, while
![Page 4: Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return](https://reader035.vdocuments.site/reader035/viewer/2022062309/56649edb5503460f94beb5c9/html5/thumbnails/4.jpg)
The Corresponding C++ Program#include <iostream>using namespace std;int main(){
int first, second, sum; cout << "Peter: Hey Frank, I just learned how to add” << “ two numbers together."<< endl; cout << "Frank: Cool!" <<endl; cout << "Peter: Give me the first number."<< endl; cout << "Frank: "; cin >> first; cout << "Peter: Give me the second number."<< endl; cout << "Frank: "; cin >> second; sum = first + second; cout << "Peter: OK, here is the answer:"; cout << sum << endl; cout << "Frank: Wow! You are amazing!" << endl;
return 0;}
![Page 5: Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return](https://reader035.vdocuments.site/reader035/viewer/2022062309/56649edb5503460f94beb5c9/html5/thumbnails/5.jpg)
Identifiers appear in black in Visual C++.– An identifier is a name for a variable, constant,
function, etc. – It consists of a letter followed by any sequence of
letters, digits, and underscores. – Examples of valid identifiers: First_name, age, y2000, y2k
– Examples of invalid identifiers: 2000y– Identifiers cannot have special characters in them. For
example: X=Y, J-20, ~Ricky,*Michael are invalid identifiers.
– Identifiers are case-sensitive. For example: Hello, hello, WHOAMI, WhoAmI, whoami are unique identifiers.
![Page 6: Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return](https://reader035.vdocuments.site/reader035/viewer/2022062309/56649edb5503460f94beb5c9/html5/thumbnails/6.jpg)
• Comments appear in green in Visual C++.• Comments are explanatory notes; they are ignored by
the compiler.• There are two ways to include comments in a program:
// A double slash marks the start of a //single line comment.
/* A slash followed by an asterisk marks the start of a multiple line comment. It ends with an asterisk followed by a slash. */
![Page 7: Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return](https://reader035.vdocuments.site/reader035/viewer/2022062309/56649edb5503460f94beb5c9/html5/thumbnails/7.jpg)
• Compiler directives appear in blue in Visual C++. • The #include directive tells the compiler to include
some already existing C++ code in your program.• The included file is then linked with the program. • There are two forms of #include statements: #include <iostream> //for pre-defined files
#include "my_lib.h" //for user-defined files
![Page 8: Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return](https://reader035.vdocuments.site/reader035/viewer/2022062309/56649edb5503460f94beb5c9/html5/thumbnails/8.jpg)
C++ is a free-format language, which means that:
• Extra blanks (spaces) or tabs before or after identifiers/operators are ignored.
• Blank lines are ignored by the compiler just like comments.
• Code can be indented in any way. • There can be more than one statement on
a single line.• A single statement can continue over
several lines.
![Page 9: Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return](https://reader035.vdocuments.site/reader035/viewer/2022062309/56649edb5503460f94beb5c9/html5/thumbnails/9.jpg)
In order to improve the readability of your program, use the following conventions:
• Start the program with a header that tells what the program does.
• Use meaningful variable names. • Document each variable declaration with a
comment telling what the variable is used for. • Place each executable statement on a single line. • A segment of code is a sequence of executable
statements that belong together.– Use blank lines to separate different segments
of code.– Document each segment of code with a
comment telling what the segment does.
![Page 10: Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return](https://reader035.vdocuments.site/reader035/viewer/2022062309/56649edb5503460f94beb5c9/html5/thumbnails/10.jpg)
• Writing Code without detailed analysis and design
• Repeating trial and error without understanding the problem
• Debugging the program line by line, statement by statement
• Writing tricky and dirty programs
![Page 11: Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return](https://reader035.vdocuments.site/reader035/viewer/2022062309/56649edb5503460f94beb5c9/html5/thumbnails/11.jpg)
100 little bugs in the code,100 bugs in the code,
fix one bug, compile it again,101 little bugs in the code.
101 little bugs in the code …Repeat until BUGS = 0
—The Internet Joke Book
![Page 12: Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return](https://reader035.vdocuments.site/reader035/viewer/2022062309/56649edb5503460f94beb5c9/html5/thumbnails/12.jpg)
// my second program in C++ #include <iostream>using namespace std; int main (){ cout << "Hello World! "; cout << "I'm a C++ program"; return 0;}
int main () { cout << " Hello World! "; cout << " I'm a C++ program "; return 0; }
![Page 13: Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return](https://reader035.vdocuments.site/reader035/viewer/2022062309/56649edb5503460f94beb5c9/html5/thumbnails/13.jpg)
// defined constants: calculate circumference #include <iostream>using namespace std; #define PI 3.14159#define NEWLINE '\n‘ int main (){ double r=5.0; // radius double circle; circle = 2 * PI * r; cout << circle; cout << NEWLINE; return 0;}
• 31.4159
![Page 14: Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return](https://reader035.vdocuments.site/reader035/viewer/2022062309/56649edb5503460f94beb5c9/html5/thumbnails/14.jpg)
+ addition
- subtraction
* multiplication
/ division
% modulo
C++ Oprators
![Page 15: Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return](https://reader035.vdocuments.site/reader035/viewer/2022062309/56649edb5503460f94beb5c9/html5/thumbnails/15.jpg)
expression is equivalent to
value += increase;value = value +
increase;
a -= 5; a = a - 5;
a /= b; a = a / b;
price *= units + 1;price = price *
(units + 1);
![Page 16: Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return](https://reader035.vdocuments.site/reader035/viewer/2022062309/56649edb5503460f94beb5c9/html5/thumbnails/16.jpg)
expression is equivalent to
value += increase; value = value + increase;
a -= 5; a = a - 5;
a /= b; a = a / b;
price *= units + 1; price = price * (units + 1);
Compound assignment (+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=)
![Page 17: Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return](https://reader035.vdocuments.site/reader035/viewer/2022062309/56649edb5503460f94beb5c9/html5/thumbnails/17.jpg)
• // compound assignment operators• #include <iostream>• using namespace std;• • int main ()• {• int a, b=3;• a = b;• a+=2; // equivalent to a=a+2• cout << a;• return 0;• }
![Page 18: Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return](https://reader035.vdocuments.site/reader035/viewer/2022062309/56649edb5503460f94beb5c9/html5/thumbnails/18.jpg)
c++;
c+=1;
c=c+1;
![Page 19: Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return](https://reader035.vdocuments.site/reader035/viewer/2022062309/56649edb5503460f94beb5c9/html5/thumbnails/19.jpg)
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
(7 == 5) // evaluates to false.
(5 > 4) // evaluates to true.
(3 != 2) // evaluates to true.
(6 >= 6) // evaluates to true.
(5 < 5) // evaluates to false.
![Page 20: Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return](https://reader035.vdocuments.site/reader035/viewer/2022062309/56649edb5503460f94beb5c9/html5/thumbnails/20.jpg)
1
2
3
4
!(5 == 5) // evaluates to false because the expression
at its right (5 == 5) is true.
!(6 <= 4) // evaluates to true because (6 <= 4) would
be false.
!true // evaluates to false
!false // evaluates to true.
!(5 == 5) // evaluates to false because the expression at its
right (5 == 5) is true.
!(6 <= 4) // evaluates to true because (6 <= 4) would be false.
!true // evaluates to false
!false // evaluates to true.
( (5 == 5) && (3 > 6) ) // evaluates to false ( true && false ).
( (5 == 5) || (3 > 6) ) // evaluates to true ( true || false ).
![Page 21: Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return](https://reader035.vdocuments.site/reader035/viewer/2022062309/56649edb5503460f94beb5c9/html5/thumbnails/21.jpg)
operator asm equivalent description
& AND Bitwise AND
| OR Bitwise Inclusive OR
^ XOR Bitwise Exclusive OR
~ NOTUnary complement (bit
inversion)
<< SHL Shift Left
>> SHR Shift Right
![Page 22: Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return](https://reader035.vdocuments.site/reader035/viewer/2022062309/56649edb5503460f94beb5c9/html5/thumbnails/22.jpg)
If (condition) statement
If (x == 100) cout << "x is 100";
![Page 23: Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return](https://reader035.vdocuments.site/reader035/viewer/2022062309/56649edb5503460f94beb5c9/html5/thumbnails/23.jpg)
if (x == 100){ cout << "x is "; cout << x;}
if (x == 100) cout << "x is 100";Else cout << "x is not 100";
if (x > 0) cout << "x is positive";else if (x < 0) cout << "x is negative";else cout << "x is 0";
![Page 24: Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return](https://reader035.vdocuments.site/reader035/viewer/2022062309/56649edb5503460f94beb5c9/html5/thumbnails/24.jpg)
While (expression) statement
#include <iostream>using namespace std;
int main (){ int n;
cout << "Enter the starting number > "; cin >> n;
while (n>0) { cout << n << ", "; --n;
} cout << "FIRE!\n";
return 0;}
![Page 25: Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return](https://reader035.vdocuments.site/reader035/viewer/2022062309/56649edb5503460f94beb5c9/html5/thumbnails/25.jpg)
• do statement while (condition);
![Page 26: Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return](https://reader035.vdocuments.site/reader035/viewer/2022062309/56649edb5503460f94beb5c9/html5/thumbnails/26.jpg)
int main (){ unsigned long n; do { cout << "Enter number (0 to end): "; cin >> n; cout << "You entered: " << n << "\n"; } while (n != 0); return 0;}
![Page 27: Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return](https://reader035.vdocuments.site/reader035/viewer/2022062309/56649edb5503460f94beb5c9/html5/thumbnails/27.jpg)
• for (initialization; condition; increase) statement;
int main (){
for (int n=10; n>0; n--) {
cout << n << ", "; }
cout << "FIRE!\n"; return 0;
}