| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

Coding Basics

Page history last edited by Lucas Wojciechowski 15 years, 7 months ago

Comments

  • Describe operation of code for other coders

  • Ignored by compiler


/*

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

  • Returns a value for a function

  • When 0 is returned for main(), the program ends


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

  • Operators are symbols used to manipulate variables

  • Perform mathematical operations

Equals

=

Multiply

*

Addition

+

Subtraction

-

Division

/

 

Input

  • Allows user to input data to the program and store it in a variable

  • Input operator is ">>"

  • "Cin" command prompts for and stores data on in a command line application


cin >> variable;


Code: Working with Varibles

 

If/Then Statements

  • Executes code only if a statement is true


if (condition = true) statement;


  • Conditional operators are used to define conditions

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

  • Loops through a block of code until a condition is met


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

  • A function can return a value and be used in expressions

  • Many functions are included with C++ compiler

    • Called library functions

 

Comments (0)

You don't have permission to comment on this page.