The document provides an overview of file structure and basic input/output in C++. It explains common elements like #include directives, namespaces, main functions, and variables. It also covers conditional statements, loops, and input/output streams for basic console applications.
3. #include<iostream>
#Include
means read me before you compile and do what I 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.
< and >
just enclosures for the compiler to read between and import
accordingly.
*stream
<iostream> is a library for basic input and out put controls since C++
alone contains no facilities for IO.
4. #include<iostream>
• 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
5. 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
6. 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.
7. Cout<<“Welcome C++”;
• The cout object is used to display text.
• It uses the << symbols, known as "insertion
operators", to indicate what to output.
22. Conditional operator ( ? )
• 7==5 ? 4 : 3 // returns 3, since 7 is not equal
to 5.
• 7==5+2 ? 4 : 3 // returns 4, since 7 is equal to
5+2.
• 5>3 ? a : b // returns the value of a, since 5 is
greater than 3.
• a>b ? a : b // returns whichever is greater, a
or b.
23. Basic Input/Output
• // i/o example
• #include <iostream>
• 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";
• return0;
• }
24. cin and strings
• // cin with strings
• #include <iostream>
• #include <string>
• 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 << "I like "<< mystr << " too!n";
• return0;
• }
25. Conditional structure: if and else
• if(x > 0)
• cout << "x is positive";
• else if(x < 0)
• cout << "x is negative";
• else
• cout << "x is 0";
26. The while loop
• // custom countdown using while
• #include <iostream>
• using namespacestd;
• intmain ()
• {
• intn;
• cout << "Enter the starting number > ";
• cin >> n;
• while(n>0) {
• cout << n << ", ";
• --n;
• }
• cout << "FIRE!n";
• return0;
• }
27. The do-while loop
• // number echoer
• #include <iostream>
• using namespacestd;
• intmain ()
• {
• unsigned longn;
• do{
• cout << "Enter number (0 to end): ";
• cin >> n;
• cout << "You entered: "<< n << "n";
• } while(n != 0);
• return0;
• }
28. The for loop
• // countdown using a for loop
• #include <iostream>
• using namespacestd;
• intmain ()
• {
• for(intn=10; n>0; n--) {
• cout << n << ", ";
• }
• cout << "FIRE!n";
• return0;
• }