#include <iostream> #include <vector> <string> <fstream> using namespace std; // to replace std:cout int main(){ cout // or std::cout cout << "Hello World" << endl; // similar to system.out.println return 0; // always end in return 0 } terminal g++ -std=c++11 ctut.cpp // compile ./a.out // run variables and data types variables start with letter, contain letters, numbers, and _ const doubleb PI = 3.1415926535; // constant cannot be changed uppercase name char myGrade = 'A'; // one byte bool isHappy = true; // or false int myAge = 39; float favNum = 3.141592; // accurate up to 6 decimal places double otherfavNum = 1.6180339887; // accurate up to 16 digits cout << "Favorite Number" << favNum << endl; // Favorite Number3.14159 // // // // // //
other types include
short int : at least 16 bits long int : at least 32 bits long long int : at least 64 bits unsigned int : same size as signed version long double : not less then double
cout << "Size of int " << sizeof(myAge)
<< endl; // tells how many bytes an int is // can do for char, bool, flo at, double int largestInt = 2147483647; cout << "Largest int " << largestInt << endl; // 2147483647 int largestInt = 2147483648; cout << "Largest int " << largestInt << endl; // -2147483648 // out of bounds of data type + - * / % ++ -cout << "5 + 2 = " << 5+2 << endl; // 5 + 2 = 7 int five = 5;
int greetngOption = 2; switch(greetingOption){ case 1 : // if value of greetingOption is 1 cout << "bonjour" << endl; break; // end switch statement, if don't do this checks other options case 2 : // if value of greetingOption is 2 cout << "Hola" << endl; break; case 3 : // if value of greetingOption is 3 cout << "Hallo" << endl; break; default : cout << "Hello" << endl; // default statement if no other match // prints out Hola ternary operator
variable = (condition) ? true : false
int largestNum = (5>2) ? 5 : 2; arrays int myFavNums[5]; int badNums[5] = {4, 13, 14, 24, 34}; cout << "Bad Number 1: " << badNums[0] << endl; // 4 multi dimensional arrays char myName[5][5] = {{'D', 'e', 'r', 'e', 'k'}, {'B', 'a', 'n', 'a', 's'}}; cout << "2nd letter in 2nd array " << myName[1][1] << endl; // a myName[0][2] = 'e'; // first array, 3rd letter cout << "New Value " << myName[0][2] << endl; // e forloop for(int i = 1; i <= 10; i++){ cout << i << endl; } // print 1 2 3 4... for(int j = 0; j < 2; j++){ // two arrays for(int k = 0; k <5; k++){ // 5 places in arrays cout << myName[j][k]; } cout << endl; } // prints Deeek Banas while loops int randNum = (rand() % 100)