You are on page 1of 32
File Structure using C++ Simple Example #include using namespace std; int main(){ cout<<"Welcome C++"5 return @5 4. 2 3 4 5 6 7 8 #include v #Include = means read me before you compile and do what | say essentially. = means that you're signaling the compiler to include some kind of file or library that is needed in order for some function to execute in your source file. v = just enclosures for the compiler to read between and import accordingly. v *stream "= is a library for basic input and out put controls since C++ alone contains no facilities for 10. #include * The #include is a "preprocessor" directive that tells the compiler to put code from the header called iostream into our program before actually creating the executable. ¢ By including header files, you gain access to many different functions. * For example, the cout function requires iostream using namespace std; * This line tells the compiler to use a group of functions that are part of the standard library std). ¢ By including this line at the top of a file, you allow the program to use functions such as cout int main() * This line tells the compiler that there is a function named main, and that the function returns an integer, hence int. * The "curly braces" ({ and }) signal the beginning and end of functions and other code blocks. Cout<<“Welcome C++”; ¢ The cout object is used to display text. ¢ It uses the << symbols, known as "insertion operators", to indicate what to output. Declaring Variables in C++ Name Description Size* Range* base jigned: -128 to 127 ni character or small integer. tytn [ane 128 427 snort aE Signed: -32768 to 32767 (snore) Short Integer. Portes [insigned: 0 to 65535 72147483648 to in. hnteger. leoytes _fass7aeaca7 linsigned: 0 to 4294967295 Jscmed: -7147483648 to on tine tional hig eager: loytes atavasaea? finsigned: 0 to 4794967205 boo Bookan vale, ean ake are oF wa vanes lice Irv or fale Float Floating point number, lsbytes —_[a/- 3.de 47-38 (=? aight) ioubte [Double precision floating point number. bytes —[a/=1.7e #/- 308 (~15 cighs) tong double |Long double precision floating point number. [Sbytes [+/- 1.7e +/- 308 (~15 digits) Variables in C++ // operating with variables include using namespacestd; intmain () { // declaring variables: inta, b; Int result; // process: // print out the result: cout << result; // terminate the program return0; } Scope of variables include using namespace std; int Integer; char acharacter; char string [20]; unsigned int Number0£Sons; aint main () { unsigned short Age; float ANumbexr, AnothexOne; cout << “Enter your age:* cin >> Age; Global warlables Initialization of variables * inta=0; * int a (0); Initialization of variables // initialization of variables #include using namespace std; intmain () { inta=5; // initial value =5 Int b(2); // initial value = 2 Int result; // initial value undetermined az=a+3; result =a -b; cout << result; returnO; } Introduction to strings // my first string #include #include using namespace std; Int main () { string mystring = "This is a string"; cout << mystring; Return 0; } Introduction to strings * string mystring = "This is a string"; * string mystring ("This is a string"); Constants (#define) // defined constants: calculate circumference #include using namespace std; #define PI 3.14159 #define NEWLINE '\n' Int main () { Double r=5.0; // radius Double circle; circle = 2 * PI *r; cout << circle; cout << NEWLINE; Return 0; } Declared constants (const) * const int pathwidth = 100; * const char tabulator = '\t'; Operators: Assignment (=) // assignment operator #include using namespacestd; intmain () { inta, b; //a:?, b:? a=10; //a:10, b:? b= 4; //a:10, b:4 a=b; //a:4, b:4 b=7; //a:4, b:7 cout << "a:"; cout < using namespacestd; intmain () { inta, b=3; a=b; at=2; // equivalent to a=a+2 cout << a; returnO; } Increase and decrease (++, --) Example 1 Example 2 B=3; B=3; lA=++B; |A=B++; // A contains 4, B contains 4/// A contains 3, B contains 4 Conditional operator ( ? ) // conditional operator #include using namespacestd; intmain () { inta,b,c; a=2; b=7; c=(a>b) ?a:b; cout << c; returnO; } Conditional operator ( ? ) 7==5 ?4:3 // returns 3, since 7 is not equal to5. 7==54+2 24:3 // returns 4, since 7 is equal to 542, 5>3 ?.a:b //returns the value of a, since 5 is greater than 3. a>b ?.a:b // returns whichever is greater, a orb. Basic Input/Output // i/o example #include using namespacestd; intmain () { inti; cout << "Please enter an integer value: "; cin >> i; cout << "The value you entered is "<< i; cout <<" and its double is "<< i*2 << '".\n"; returnO; } cin and strings // cin with strings #include #include using namespacestd; intmain () { string mystr; cout << "What's your name? "; getline (cin, mystr); cout << "Hello "<< mystr << ".\n"; cout << "What is your favorite team? "; getline (cin, mystr); cout <<"! like "<< mystr <<" too!\n"; return0; } Conditional structure: if and else if(x > 0) cout << "x is positive"; else if(x < 0) cout << "x is negative"; else cout << "xis 0"; The while loop // custom countdown using while #include using namespacestd; intmain () { intn; cout << "Enter the starting number >"; cin >>n; while(n>0) { cout < using namespacestd; intmain () { unsigned longn; do{ cout << "Enter number (0 to end): "; cin >> n; cout << "You entered: "<< n<<"\n"; } while(n != 0); returnO; } The for loop // countdown using a for loop #include using namespacestd; intmain () { for(intn=10; n>0; n--) { cout < using namespacestd; intmain () { intn; for(n=10; n>0;n-) { cout< using namespacestd; intmain () { for(intn=10; n>0; n--) { if(n==5) continue; cout < using namespace std; Int main () { Int n=10; loop: cout <0) goto loop; cout << "FIRE!\n"; returnO; }

You might also like