Comments
/*
This is a multi-line comment
because it can go over multiple lines
*/
//This is a single line comment
Header
-
Tells C++ to open a specific header for use in the program
-
Headers are collections of functions that can be used in the program
-
Common one in command line programming is "iostream" which allows you to use the cin and cout commands that print information to the command line.
-
The namespace defines a certain set of headers to chose from.
#include <header name>
using namespace std;
Main()
-
Main() is always the first function to be run, and generally when Main ends, the program ends.
-
Entire function should be enclosed in { } brackets.
int main() {
Output
-
Information is "printed" on the screen so that the user can read it.
-
"Cout" command displays the information in a terminal application.
-
"<<" is the output operator, it always follows an output command.
-
Multiple outputs can be separated by output operators
-
"\n" will print a new line
cout << "Some message";
cout << "Some message and..." << variable
Code: Hello World
Return
Return 0;
Declaring Variables
-
Defines a variable and its properties
-
Multiple declarations can be separated by commas
-
Cannot have the same name as a C++ keyword
-
Must always start with a letter (no numbers)
-
May include an underscore
-
Names are case sensitive
int varName;
- This code defines varName as an interger
Operators with Variables
Equals
|
=
|
Multiply
|
*
|
Addition
|
+
|
Subtraction
|
-
|
Division
|
/
|
Input
cin >> variable;
Code: Working with Varibles
If/Then Statements
if (condition = true) statement;
Greater Than
|
<
|
Less Than
|
>
|
Greater Than or Equal To
|
<=
|
Less Than or Equal To
|
>=
|
Equal To
|
==
|
Not Equal To
|
!=
|
Code: If/Then Statements
The For Loop
for (x=0; x>=5; x++) {
-
The above code defines x as 0, says that it should run until x is greater than or equal to 5, and it should add 1 to x each time the loop runs.
-
The ++ operator will add 1 to a variable
-
The -- operator will subtract 1 from a variable
Code: Absolute Value
Declaring Functions
Comments (0)
You don't have permission to comment on this page.