Follow
Publications: 0 | Followers: 0

FirstSteps

Publish on Category: Birds 268

C++FirstSteps
C++ First Steps
Overview- General Data Types- Categories of Words- The Three S’s- Define Before Use- End of Statement- My First Program
Data Type
adescriptionof data,defining a set ofvalid valuesandoperationsList of General Data Types:-Integer:numbers without a decimal point-Float:numbers with a decimal point-Character:a single character-String:a sequence of 0 or more characters
Data Type
Q: What is meant by “defines operations”?A: a Data Type defines which operators may be used on values of the type and how they work.2 + 3result5 definition of+onintegers'2'+'3'result'e'definition of+oncharacters"2" + "3" result "23" definition of+onstrings2 + "3" resulterrornodefn. ofinteger + string
Categories of Words
The words and symbols found in a C++ program fall into the following categories:- Reserved Words (Keywords)- Identifiers- Literals- Comments- Compiler Directives
Categories of Words
Reserved Words(Keywords)Words that already have a defined meaning in C++including operators and symbolsEx: + * / < <= << >> ( [
Categories of Words
IdentifiersWords that are created and defined by the programmer.-Must start with aletterorunderscore (_)- The remainder may beletters,digitsor theunderscore (_)- Note: that meansno spaces- Identifiers are CaseSensitive- Must not be a Reserved Word
Categories of Words
Literalsadatavaluewith an implieddata typeExamples:5integer literal: digits with nodecimal point5.0float literal: digits with a decimal point'1'character literal: exactly one character between single quotes."a is12.0"string literal: 0 or more characters between double quotes. Must be on one line.
Categories of Words
Escape CharactersTheASCII Tabledefines the binary codes for 256 possible characters.Some characters are not found on a keyboard but are needed in some programming situations.C++ provides Escape Characters for specifying such character literals.
Categories of Words
Escape CharactersInside quotation marks (literals), Escape Characters are identified by a backslash\followed by a single characterThe single character indicateswhichspecial characterTogetherthese two characters are interpreted asonecharacter
Categories of Words
Escape Characters\nNewline (go down to the next line)\tTab\’Single Quote (needed inside a char literal)\”Double Quote (needed inside a string literal)\\Backslash(there are more)
Categories of Words
Escape Characterscout<<"Hello\t\"World\"\n\n\\BS\\\n";cout<<"Hello\t\"World\"\n\n\\BS\\\n";Prints:
Categories of Words
CommentsTwo forms:Block Comment:anything between/*and*/May include multiple lines.Line Comments:anything after//to the end of the line
Categories of Words
Comments/* this is a block comment *//*This is also a block comment*//** traditionally more like this*/// this is a line comment
Categories of Words
Compiler DirectivesPurpose: to direct the compiler to do something with the Source codebeforetranslation begins.Startswith#followed by a pre-defined word (no space), usually followed by something more.
Categories of Words
Compiler DirectivesExample:#include <iostream>#includeinstructs the compiler to “copy/paste” a file into the Source code. It is followed by the name of the file.When the file name is between<arrows>, this means “look in the C++ standard library”.
Categories of Words
Compiler DirectivesExample:#include <iostream>iostreamis a C++ library that definescout,cin,thesystem()function and many other things.It must be included in any source file that uses those items defined in it.
Categories of Words
Compiler DirectivesCompiler Directives are usually placed at thetopof a source file(not considering comments).
The Three S’s
When learning new statements in C++,we will discuss three major aspects:SyntaxSemanticsStyle
The Three S’s
SyntaxRulesdefined by the language for writing statementsViolation of these rules causes a Syntax Error and the compiler is unable to translate the code.
The Three S’s
SemanticsThemeaningof a statement: what itdoeswhen executed.
The Three S’s
StyleRulesfor writing statements that make the code easier for a programmer to understandViolation of these rules hasno effecton the program.
The Three S’s
StyleExamples:- Writing meaningful comments- Creating descriptive identifier names- Indentation and spacing- Breaking a statement up over multiple lines (or not).
The Three S’s
Warning to the Snakes!(Python programmers)Indentation, spacing, and end-of-line affect the Syntax and Semantics of statements in PythonIn C++ THEY DO NOT!**
Define Before Use
When compiling a source file, C++ begins with the first line and proceeds down, left to right.Any identifier must bedefinedbefore it can be used, including those defined in standard libraries.Example:#include<iostream>must beabovethe use of any identifiers it defines (ex:cout, system())Violation of this rule results in the syntax error “identifier undefined”
End of Statement
In C++ the semicolon;is used to indicateEnd of Statement
First C++ Program
// my first program#include <iostream>using namespacestd;intmain() {cout<< "Hello world!\n";system("pause");return 0;}
First C++ Program
// my first program#include <iostream>using namespacestd;intmain() {cout<< "Hello world!\n";system("pause");return 0;}Comment- has no effect on the program
First C++ Program
// my first program#include <iostream>using namespacestd;intmain() {cout<< "Hello world!\n";system("pause");return 0;}Compiler Directive- this program uses theiostreamlibrary in C++. It is needed to define the wordcout.
First C++ Program
// my first program#include <iostream>usingnamespacestd;intmain() {cout<< "Hello world!\n";system("pause");return 0;}Instructs the compiler to use the "standard namespace". Without this,coutand other words wouldstill be"undefined".
First C++ Program
// my first program#include <iostream>using namespacestd;intmain(){cout<< "Hello world!\n";system("pause");return 0;}main()- beginning of the program instructions. Executionalwaysstarts here.
First C++ Program
// my first program#include <iostream>using namespacestd;intmain(){cout<< "Hello world!\n";system("pause");return 0;}Begin and End of main()'s block. Note the{may be on the end of theintmain()line, or on a line by itself.
First C++ Program
// my first program#include <iostream>using namespacestd;intmain() {cout<< "Hello world!\n";system("pause");return 0;}coutis used to print on the screen in a Console Application
First C++ Program
// my first program#include <iostream>using namespacestd;intmain() {cout<<"Hello world!\n";system("pause");return 0;}The<<operator is used to separate individual pieces of data to be printed. Here, only one string literal is printed.
First C++ Program
// my first program#include <iostream>using namespacestd;intmain() {cout<<"Hello world!\n";system("pause");return 0;}AString Literalbegins and ends with a double quote. This isliterallywhat is printed on the screen.
First C++ Program
// my first program#include <iostream>using namespacestd;intmain() {cout<< "Hello world!\n";system("pause");return 0;}The\nmakes the cursor go down tothe next line after printing the!
First C++ Program
// my first program#include <iostream>using namespacestd;intmain() {cout<< "Hello world!\n";system("pause");return 0;}This is a request to the OS (Windows) to execute thepausecommand, which prints "Hit any key to continue..." then waits for the user to respond.
First C++ Program
// my first program#include <iostream>using namespacestd;intmain() {cout<< "Hello world!\n";system("pause");return 0;}return 0;causes the program to exit main(), and so exits the program. It should always be the last line in main().
Vocabulary
Vocabulary

0

Embed

Share

Upload

Make amazing presentation for free
FirstSteps