Break and Continue Kyewords (Importance and How to use)


Two keywords that are very important to looping are break and continue.
The break command will exit the most immediately surrounding loop regardless of what the conditions of the loop are. Break is useful if we want to exit a loop under special circumstances. For example, let's say the program we're working on is a two-person checkers game. The basic structure of the program might look like this:

while (true) 
{
    take_turn(player1);
    take_turn(player2);
}

This will make the game alternate between having player 1 and player 2 take turns. The only problem with this logic is that there's no way to exit the game; the loop will run forever! Let's try something like this instead:

while(true)
{
    if (someone_has_won() || someone_wants_to_quit() == TRUE)
    {break;}
    take_turn(player1);
    if (someone_has_won() || someone_wants_to_quit() == TRUE)
    {break;}
    take_turn(player2);
}

This code accomplishes what we want--the primary loop of the game will continue under normal circumstances, but under a special condition (winning or exiting) the flow will stop and our program will do something else.

Continue is another keyword that controls the flow of loops. If you are executing a loop and hit a continue statement, the loop will stop its current iteration, update itself (in the case of for loops) and begin to execute again from the top. Essentially, the continue statement is saying "this iteration of the loop is done, let's continue with the loop without executing whatever code comes after me." Let's say we're implementing a game of Monopoly. Like above, we want to use a loop to control whose turn it is, but controlling turns is a bit more complicated in Monopoly than in checkers. The basic structure of our code might then look something like this:

for (player = 1; someone_has_won == FALSE; player++)
    {
        if (player > total_number_of_players)
        {player = 1;}
        if (is_bankrupt(player))
        {continue;}
        take_turn(player);
    }


This way, if one player can't take her turn, the game doesn't stop for everybody; we just skip her and keep going with the next player's turn.

0 comments:

Post a Comment

Your Comment Here!.....

Categories

10th 3i InfoTech Academic Books Accenture ACIO ActiveX ADT Agricultural AIEEE Air Force Algebraic Amdocs Android Answers Application Development Aptitude Aptitude Questions Architectures ASP ATOS B.Sc B.Tech.B.E. Bank Exam BCA BE Board Exam Books break-continue Business Plan C C Programming C# C++ Campus campus interview Candidate profile Capgemini Career CDS Certification CET Challenge Circle Cisco class code Cognizant communication Company Company Profile Competitive Exams computer Computer Networks concentric circles constructor Course Credit Suisse CSS CTS Data Structure DBMS DC Deloitte difficult interview questions dimensions Distributed Computing do while dotNet Download ds Dynamic Web Development e-Admit card Educational engineering entertainment Even Odd Events exam schedule exception for loop fresher GATE general Discussion general knowledge Get Placed Government Job Hall Ticket HCL how to answer How to Prepare HR HR Interview HSC hypertext preprocessor IB IBM IBPS IIT Indian Army Information infosys Intelligence Bureau Internship interview Experience interview questions Interview Tips IntroC IntroC# IntroJava IntroPHP IT J2EE J2ME Java JavaScript jobs Language Books Language Tutorial Languages limit number of objectsJava Limit the number of objects being created in JAVA Linux Linux Administrator Linux Developer Logical Questions loops M Tech M.E. M.Tech M.Tech AND B.Tech Management Management Skills Matrices MBA mca ME microsoft mistakes Mixture Mobile Computing mock questions mock test MySql naukri NDA OOP opening Operating System Oracle paper Persistent PHP php programming php string variables PL/SQL Placement placement guide Placement Paper Placement Process preparing for placement presentation probability Problems Professional program Programming Project Engineer project idea Projects Puzzle qualities Question of the day Questions Quiz Question Recruitment Recruitment Pattern Requirement Result Resume Reviews Screen Sizes Scripting Session Skills Software Software Engineering solved papers Source Code Speed time and distance SQL SSC story Stress Interview Study Material study tips submit resume Synonym TCS Tech Mahindra tech news Technical Books Technical Interview Testing thank you letter Thought Time Table TutC++ Unix questions asked in aptitude and inteviews for MCA UPSC verbal Web Designing Web Developer Website Development What to read while loop Wipro Writing Resume

Receive Quality Tutorials Straight in your Inbox by submitting your Email ID below.

Categories

10th 3i InfoTech Academic Books Accenture ACIO ActiveX ADT Agricultural AIEEE Air Force Algebraic Amdocs Android Answers Application Development Aptitude Aptitude Questions Architectures ASP ATOS B.Sc B.Tech.B.E. Bank Exam BCA BE Board Exam Books break-continue Business Plan C C Programming C# C++ Campus campus interview Candidate profile Capgemini Career CDS Certification CET Challenge Circle Cisco class code Cognizant communication Company Company Profile Competitive Exams computer Computer Networks concentric circles constructor Course Credit Suisse CSS CTS Data Structure DBMS DC Deloitte difficult interview questions dimensions Distributed Computing do while dotNet Download ds Dynamic Web Development e-Admit card Educational engineering entertainment Even Odd Events exam schedule exception for loop fresher GATE general Discussion general knowledge Get Placed Government Job Hall Ticket HCL how to answer How to Prepare HR HR Interview HSC hypertext preprocessor IB IBM IBPS IIT Indian Army Information infosys Intelligence Bureau Internship interview Experience interview questions Interview Tips IntroC IntroC# IntroJava IntroPHP IT J2EE J2ME Java JavaScript jobs Language Books Language Tutorial Languages limit number of objectsJava Limit the number of objects being created in JAVA Linux Linux Administrator Linux Developer Logical Questions loops M Tech M.E. M.Tech M.Tech AND B.Tech Management Management Skills Matrices MBA mca ME microsoft mistakes Mixture Mobile Computing mock questions mock test MySql naukri NDA OOP opening Operating System Oracle paper Persistent PHP php programming php string variables PL/SQL Placement placement guide Placement Paper Placement Process preparing for placement presentation probability Problems Professional program Programming Project Engineer project idea Projects Puzzle qualities Question of the day Questions Quiz Question Recruitment Recruitment Pattern Requirement Result Resume Reviews Screen Sizes Scripting Session Skills Software Software Engineering solved papers Source Code Speed time and distance SQL SSC story Stress Interview Study Material study tips submit resume Synonym TCS Tech Mahindra tech news Technical Books Technical Interview Testing thank you letter Thought Time Table TutC++ Unix questions asked in aptitude and inteviews for MCA UPSC verbal Web Designing Web Developer Website Development What to read while loop Wipro Writing Resume