c++ (cpp) tutorial part iii _ search a reply

Upload: bhagwansingh22

Post on 04-Jun-2018

230 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 C++ (CPP) Tutorial Part III _ Search a Reply

    1/13

    1/15/2014 C++ (CPP) Tutorial Part III | Search A Reply

    http://www.searchareply.com/cpp-tutorial-part-iii/

    SEARCH... Search

    SearchA Reply

    NavigationTechnologySoftwareSocial MediaContact Us

    TechnologySoftwareSocial MediaContact Us

    C++ (CPP) Tutorial Part IIIBy Lucy| on Mar 13, 2013 | 0 CommentSoftware

    Search for:

    Search

    Who s Online7 visitors online now4 guests, 3 bots, 0 members

    Tweet

    0

    0

    0

    Like

    http://www.searchareply.com/wp-content/uploads/2013/03/C++-Programming_Search-a-reply.jpghttp://www.searchareply.com/wp-content/uploads/2013/03/C++-Programming_Search-a-reply.jpghttp://www.searchareply.com/wp-content/uploads/2013/03/C++-Programming_Search-a-reply.jpghttp://www.searchareply.com/wp-content/uploads/2013/03/C++-Programming_Search-a-reply.jpghttp://www.searchareply.com/wp-content/uploads/2013/03/C++-Programming_Search-a-reply.jpgmailto:?to=&subject=C%2B%2B+%28CPP%29+Tutorial+Part+III&body=%26nbsp%3B%0D%0A%0D%0AControl+Structures%3A%0D%0A%0D%0A+A+program+is+usually+not+limited+to+a+linear+sequence+of+instructions.+During+its+process+it+may+bifurcate%2C+repeat%0D%0Acode+or+take+decisions.+For+that+purpose%2C+C%2B%2B+provides+control+structures+that+serve+to+specify%20-%20http://www.searchareply.com/cpp-tutorial-part-iii/http://www.searchareply.com/category/social-media/http://www.searchareply.com/category/software/http://www.searchareply.com/category/technology/http://www.printfriendly.com/print?url=http%3A%2F%2Fwww.searchareply.com%2Fcpp-tutorial-part-iii%2Fmailto:?to=&subject=C%2B%2B+%28CPP%29+Tutorial+Part+III&body=%26nbsp%3B%0D%0A%0D%0AControl+Structures%3A%0D%0A%0D%0A+A+program+is+usually+not+limited+to+a+linear+sequence+of+instructions.+During+its+process+it+may+bifurcate%2C+repeat%0D%0Acode+or+take+decisions.+For+that+purpose%2C+C%2B%2B+provides+control+structures+that+serve+to+specify%20-%20http://www.searchareply.com/cpp-tutorial-part-iii/http://twitter.com/search?q=http%3A%2F%2Fwww.searchareply.com%2Fcpp-tutorial-part-iii%2Fhttps://twitter.com/intent/tweet?original_referer=http%3A%2F%2Fwww.searchareply.com%2Fcpp-tutorial-part-iii%2F&text=C%2B%2B%20(CPP)%20Tutorial%20Part%20III%20-%20&tw_p=tweetbutton&url=http%3A%2F%2Fwww.searchareply.com%2Fcpp-tutorial-part-iii%2Fhttp://www.searchareply.com/wp-content/uploads/2013/03/C++-Programming_Search-a-reply.jpghttp://www.searchareply.com/category/software/http://www.searchareply.com/author/menaka/http://www.searchareply.com/contact-us/http://www.searchareply.com/category/social-media/http://www.searchareply.com/category/software/http://www.searchareply.com/category/technology/http://www.searchareply.com/contact-us/http://www.searchareply.com/category/social-media/http://www.searchareply.com/category/software/http://www.searchareply.com/category/technology/http://www.searchareply.com/http://pinterest.com/searchareply/http://www.twitter.com/searchareplyhttp://www.facebook.com/searchareply
  • 8/13/2019 C++ (CPP) Tutorial Part III _ Search a Reply

    2/13

    1/15/2014 C++ (CPP) Tutorial Part III | Search A Reply

    http://www.searchareply.com/cpp-tutorial-part-iii/ 2

    Control Structures:A program is usually not limited to a linear sequence ofinstructions. During its process it may bifurcate, repeatcode or take decisions. For that purpose, C++ provides controlstructures that serve to specify what has to be doneby our program, when and under which circumstances.

    With the introduction of control structures we are going to haveto introduce a new concept: the compoundstatementor block. A block is a group of statements which are separatedby semicolons (;) like all C++statements, but grouped together in a block enclosed in braces:{ }:{ statement1; statement2; statement3; }Most of the control structures that we will see in this sectionrequire a generic statement as part of its syntax. Astatement can be either a simple statement (a simple instructionending with a semicolon) or a compound

    statement (several instructions grouped in a block), like the onejust described. In the case that we want thestatement to be a simple statement, we do not need to encloseit in braces ({}). But in the case that we want thestatement to be a compound statement it must be enclosedbetween braces ({}), forming a block.

    Conditional structure: if and else:The if keyword is used to execute a statement or block only if acondition is fulfilled. Its form is:if (condition) statement

    Where condition is the expression that is being evaluated. If thiscondition is true, statement is executed. If it isfalse, statement is ignored (not executed) and the programcontinues right after this conditional structure.For example, the following code fragment prints x is 100 only ifthe value stored in the x variable is indeed 100:if (x == 100)cout

  • 8/13/2019 C++ (CPP) Tutorial Part III _ Search a Reply

    3/13

    1/15/2014 C++ (CPP) Tutorial Part III | Search A Reply

    http://www.searchareply.com/cpp-tutorial-part-iii/ 3

    cout

  • 8/13/2019 C++ (CPP) Tutorial Part III _ Search a Reply

    4/13

    1/15/2014 C++ (CPP) Tutorial Part III | Search A Reply

    http://www.searchareply.com/cpp-tutorial-part-iii/ 4

    (n>0) remains being true.The whole process of the previous program can be interpretedaccording to the following script (beginning inmain):

    1. User assigns a value to n2. The while condition is checked (n>0). At this point there aretwo posibilities:* condition is true: statement is executed (to step 3)* condition is false: ignore statement and continue after it (tostep 5)3. Execute statement:cout n;cout

  • 8/13/2019 C++ (CPP) Tutorial Part III _ Search a Reply

    5/13

    1/15/2014 C++ (CPP) Tutorial Part III | Search A Reply

    http://www.searchareply.com/cpp-tutorial-part-iii/ 5

    return 0;}Enter number (0 to end): 12345You entered: 12345Enter number (0 to end): 160277You entered: 160277Enter number (0 to end): 0You entered: 0The do-while loop is usually used when the condition that has todetermine the end of the loop is determined withinthe loop statement itself, like in the previous case, where theuser input within the block is what is used todetermine if the loop has to end. In fact if you never enter thevalue 0 in the previous example you can beprompted for more numbers forever.

    The for loop:Its format is:for (initialization; condition; increase) statement;

    and its main function is to repeat statement while conditionremains true, like the while loop. But in addition, thefor loop provides specific locations to contain an initializationstatement and an increase statement. So thisloop is specially designed to perform a repetitive action with acounter which is initialized and increased on eachiteration.It works in the following way:1. initialization is executed. Generally it is an initial valuesetting for a counter variable. This is executedonly once.

    2. condition is checked. If it is true the loop continues,otherwise the loop ends and statement is skipped(not executed).3. statement is executed. As usual, it can be either a singlestatement or a block enclosed in braces { }.4. finally, whatever is specified in the increase field is executedand the loop gets back to step 2.Here is an example of countdown using a for loop:// countdown using a for loop#include using namespace std;

    int main (){for (int n=10; n>0; n) {cout

  • 8/13/2019 C++ (CPP) Tutorial Part III _ Search a Reply

    6/13

    1/15/2014 C++ (CPP) Tutorial Part III | Search A Reply

    http://www.searchareply.com/cpp-tutorial-part-iii/ 6

    between them must be written. For example we could write: for(;n Condition

    n++,i > Increase

    n starts with a value of 0, and i with 100, the condition is n!=i(that n is not equal to i). Because n is increased byone and i decreased by one, the loops condition will becomefalse after the 50th loop, when both n and i will beequal to 50.

    Jump statements:The break statement:Using break we can leave a loop even if the condition for its endis not fulfilled. It can be used to end an infiniteloop, or to force it to end before its natural end. For example,we are going to stop the count down before itsnatural end (maybe because of an engine check failure?):// break loop example#include using namespace std;

    int main (){int n;for (n=10; n>0; n){cout

  • 8/13/2019 C++ (CPP) Tutorial Part III _ Search a Reply

    7/13

    1/15/2014 C++ (CPP) Tutorial Part III | Search A Reply

    http://www.searchareply.com/cpp-tutorial-part-iii/ 7

    }return 0;}10, 9, 8, 7, 6, 5, 4, 3, countdown aborted!

    The continue statement:The continue statement causes the program to skip the rest ofthe loop in the current iteration as if the end of thestatement block had been reached, causing it to jump to thestart of the following iteration. For example, we aregoing to skip the number 5 in our countdown:// continue loop example#include using namespace std;int main (){for (int n=10; n>0; n) {if (n==5) continue;cout

  • 8/13/2019 C++ (CPP) Tutorial Part III _ Search a Reply

    8/13

    1/15/2014 C++ (CPP) Tutorial Part III | Search A Reply

    http://www.searchareply.com/cpp-tutorial-part-iii/ 8

    The exit function:exit is a function defined in the cstdlib library.The purpose of exit is to terminate the current program with aspecific exit code. Its prototype is:void exit (int exitcode);The exitcode is used by some operating systems and may beused by calling programs. By convention, an exitcode of 0 means that the program finished normally and anyother value means that some error or unexpectedresults happened.

    The selective structure: switch:The syntax of the switch statement is a bit peculiar. Its objectiveis to check several possible constant values for anexpression. Something similar to what we did at the beginningof this section with the concatenation of several ifand else if instructions. Its form is the following:switch (expression){case constant1:

    group of statements 1;break;case constant2:group of statements 2;break;...default:default group of statements}

    It works in the following way: switch evaluates expression andchecks if it is equivalent to constant1, if it is, itexecutes group of statements 1 until it finds the breakstatement. When it finds this break statement theprogram jumps to the end of the switch selective structure.If expression was not equal to constant1 it will be checkedagainst constant2. If it is equal to this, it will executegroup of statements 2 until a break keyword is found, and thenwill jump to the end of the switch selectivestructure.Finally, if the value of expression did not match any of the

    previously specified constants (you can include asmany case labels as values you want to check), the programwill execute the statements included after thedefault: label, if it exists (since it is optional).

    Both of the following code fragments have the same behavior:switch example :switch (x) {case 1:cout

  • 8/13/2019 C++ (CPP) Tutorial Part III _ Search a Reply

    9/13

    1/15/2014 C++ (CPP) Tutorial Part III | Search A Reply

    http://www.searchareply.com/cpp-tutorial-part-iii/ 9

    case 2:cout

  • 8/13/2019 C++ (CPP) Tutorial Part III _ Search a Reply

    10/13

    1/15/2014 C++ (CPP) Tutorial Part III | Search A Reply

    http://www.searchareply.com/cpp-tutorial-part-iii/ 10

    Functions (I):Using functions we can structure our programs in a moremodular way, accessing all the potential that structuredprogramming can offer to us in C++.A function is a group of statements that is executed when it iscalled from some point of the program. Thefollowing is its format:type name ( parameter1, parameter2, ) { statements }where: type is the data type specifier of the data returned by thefunction. name is the identifier by which it will be possible to call thefunction. parameters (as many as needed): Each parameter consists ofa data type specifier followed by anidentifier, like any regular variable declaration (for example: intx) and which acts within the function asa regular local variable. They allow to pass arguments to thefunction when it is called. The differentparameters are separated by commas. statements is the functions body. It is a block of statementssurrounded by braces { }.Here you have the first function example:// function example#include using namespace std;int addition (int a, int b){int r;r=a+b;return (r);

    }int main (){int z;z = addition (5,3);cout

  • 8/13/2019 C++ (CPP) Tutorial Part III _ Search a Reply

    11/13

    1/15/2014 C++ (CPP) Tutorial Part III | Search AReply

    http://www.searchareply.com/cpp-tutorial-part-iii/ 1

    z= addition(5,3);The parameters and arguments have a clear correspondence.Within the main function we called to additionpassing two values: 5 and 3, that correspond to the int a and intb parameters declared for function addition.

    At the point at which the function is called from within main, thecontrol is lost by main and passed to functionaddition. The value of both arguments passed in the call (5 and3) are copied to the local variables int a and intb within the function.Function addition declares another local variable (int r), and bymeans of the expression r=a+b, it assigns to rthe result of a plus b. Because the actual parameters passed fora and b are 5 and 3 respectively, the result is 8.The following line of code:return (r);finalizes function addition, and returns the control back to thefunction that called it in the first place (in this case,main). At this moment the program follows it regular course

    from the same point at which it was interrupted by thecall to addition. But additionally, because the return statementin function addition specified a value: thecontent of variable r (return (r);), which at that moment had avalue of 8. This value becomes the value ofevaluating the function call.

    You can download the tutorial from this link:

    http://www.searchareply.com/wp-content/uploads/2013/03/C++-Tutorial_Search-a-reply.pdf

    Share this story:

    Tweet 0

    0Like

    0

    Tags:Beginners tutorialC TutorialC++ ProgrammingCPP Tutorialfor beginnersTutorial guide

    Recent Posts

    Youtube to bring inoffline videoviewing feature

    Related Posts

    C ProgrammingInterview Questionsand Answers Part

    http://www.searchareply.com/c-programming-interview-questions-and-answers-part-iii/http://www.searchareply.com/c-programming-interview-questions-and-answers-part-iii/http://www.searchareply.com/c-programming-interview-questions-and-answers-part-iii/http://www.searchareply.com/youtube-to-bring-in-offline-video-viewing-feature/http://www.searchareply.com/youtube-to-bring-in-offline-video-viewing-feature/http://pinterest.com/pin/create/button/http://www.searchareply.com/tag/tutorial-guide/http://www.searchareply.com/tag/cpp-tutorial-for-beginners/http://www.searchareply.com/tag/c-programming/http://www.searchareply.com/tag/c-tutorial/http://www.searchareply.com/tag/beginners-tutorial/http://www.searchareply.com/wp-content/uploads/2013/03/C++-Tutorial_Search-a-reply.pdfhttp://twitter.com/search?q=http%3A%2F%2Fwww.searchareply.com%2Fcpp-tutorial-part-iii%2Fhttps://twitter.com/intent/tweet?original_referer=http%3A%2F%2Fwww.searchareply.com%2Fcpp-tutorial-part-iii%2F&text=C%2B%2B%20(CPP)%20Tutorial%20Part%20III&tw_p=tweetbutton&url=http%3A%2F%2Fwww.searchareply.com%2Fcpp-tutorial-part-iii%2F
  • 8/13/2019 C++ (CPP) Tutorial Part III _ Search a Reply

    12/13

    1/15/2014 C++ (CPP) Tutorial Part III | Search A Reply

    http://www.searchareply.com/cpp-tutorial-part-iii/ 12

    Author Description

    No Responses to C++ (CPP) Tutorial Part III

    Leave a ReplyYour email address will not be published. Required fields aremarked *

    Name *

    Email *

    Website

    Comment

    Oct 3, 2013 - 0Comment

    An app toremember your

    dreams

    Oct 3, 2013 - 0Comment

    Soon Mobiles willPredicts theEarthquakes

    Oct 3, 2013 - 0Comment

    III

    Mar 22, 2013 - 0Comment

    C ProgrammingInterview Questionsand Answers Part II

    Mar 22, 2013 - 0Comment

    C Programming

    Interview Questionsand Answers Part I

    Mar 22, 2013 - 0Comment

    http://www.searchareply.com/c-programming-interview-questions-and-answers-part-i/http://www.searchareply.com/c-programming-interview-questions-and-answers-part-i/http://www.searchareply.com/c-programming-interview-questions-and-answers-part-ii/http://www.searchareply.com/c-programming-interview-questions-and-answers-part-ii/http://www.searchareply.com/c-programming-interview-questions-and-answers-part-iii/http://www.searchareply.com/soon-mobiles-will-predicts-the-earthquakes/http://www.searchareply.com/soon-mobiles-will-predicts-the-earthquakes/http://www.searchareply.com/an-app-to-remember-your-dreams/http://www.searchareply.com/an-app-to-remember-your-dreams/
  • 8/13/2019 C++ (CPP) Tutorial Part III _ Search a Reply

    13/13

    1/15/2014 C++ (CPP) Tutorial Part III | Search A Reply

    HomeContact UsHomepage

    2013. All Rights Reserved. Search A Reply

    Post Comment

    Notify me of follow-up comments by email.

    Notify me of new posts by email.

    http://www.searchareply.com/homepage/http://www.searchareply.com/contact-us/http://www.searchareply.com/