You are on page 1of 6

HELLO WORLD

#include <iostream>

Using namespace std

Int main () {

Cout << “hello world!”;

Return 0;

COUT
#include <iostream>

Using namespace std

Int main () {

Cout << “hi”;

Cout << “my name is”;

Return 0;

COUT END LINE (\)

Include <iostream>

Using namespace std

Int main () {

Cout << “hello \n”; cout << “hello” << endl; cout << “hello \n\n”;

Return 0; }

COMMENT (/)

Include <iostream>

Using namespace std

Int main () {

//this is a comment int main() {

Cout << “hi”; cout << "Hello World!"; // This is a comment

Return 0; }
/* The code below will print the words Hello World!
to the screen, and it is amazing */
cout << "Hello World!";

VARIABLE

#include <iostream>

using namespace std;

int main() {

int myNum = 15;

cout << myNum;

return 0;

int main() {

int myNum;

myNum = 15;

cout << myNum;

return 0;

int myNum = 5; // Integer (whole number without decimals)


double myFloatNum = 5.99; // Floating point number (with decimals)
char myLetter = 'D'; // Character
string myText = "Hello"; // String (text)
bool myBoolean = true; // Boolean (true or false)
ADD
int x = 5;
int y = 6;
int sum = x + y;
cout << sum;

int x, y, z;
x = y = z = 50;
cout << x + y + z;
int x = 5, y = 6, z = 50;
cout << x + y + z;

"constant", which means unchangeable and read-only):


const int myNum = 15; // myNum will always be 15
myNum = 10; // error: assignment of read-only variable 'myNum'

CIN – INPUT
int x;
cout << "Type a number: "; // Type a number and press enter
cin >> x; // Get user input from the keyboard
cout << "Your number is: " << x; // Display the input value
for cin it takes the user input , it is written after cout as first
cout types the output and cin now takes in the number
CALCULATOR
int x, y;
int sum;
cout << "Type a number: ";
cin >> x;
cout << "Type another number: ";
cin >> y;
sum = x + y;
cout << "Sum is: " << sum;

C++ Boolean Data Types


bool isCodingFun = true;
bool isFishTasty = false;
cout << isCodingFun; // Outputs 1 (true)
cout << isFishTasty; // Outputs 0 (false)

The char data type is used to store a single character. The character must
be surrounded by single quotes, like 'A' or 'c':
Char letter = ‘d’;
Cout << letter;

String
String vocab = “hello”;
Cout << “vocab”;
C++ Operators
int x = 100 + 50;

int sum1 = 100 + 50; // 150 (100 + 50)


int sum2 = sum1 + 250; // 400 (150 + 250)
int sum3 = sum2 + sum2; // 800 (400 + 400)

Assignment Operators
The addition assignment operator (+=) adds a value
int x = 10;
x += 5;

= x=5 x=5

+= x += 3 x=x+3

-= x -= 3 x=x-3

*= x *= 3 x=x*3

/= x /= 3 x=x/3

%= x %= 3 x=x%3

&= x &= 3 x=x&3


|= x |= 3 x=x|3

^= x ^= 3 x=x^3

>>= x >>= 3 x = x >> 3

<<= x <<= 3 x = x << 3

Comparison Operators
int x = 5;
int y = 3;
cout << (x > y); // returns 1 (true) because 5 is greater than 3

Operator Name Example

== Equal to x == y

!= Not equal x != y

> Greater than x>y

< Less than x<y

>= Greater than or equal to x >= y


<= Less than or equal to x <= y

Logical Operators
int main() {
int x = 5;
int y = 3;
cout << (x > 3 && x < 10); // returns true (1) because 5 is greater than 3
AND 5 is less than 10
return 0;
}

Operator Name Description Example

&& Logical and Returns true if both statements are x < 5 && x < 10
true

|| Logical or Returns true if one of the statements x < 5 || x < 4


is true

You might also like