Results 1 to 5 of 5

Thread: Visual C++ to end a loop & exit from program?

  1. #1
    Join Date
    Jan 2009
    Posts
    18

    Visual C++ to end a loop & exit from program?

    Hi,

    I want to give user an option to quit the running loop & exit the program whenever he wants. Well i have a function to accept data from user in loop & want to give option to user so he can stop the loop & get out of the function or the program!

    I don't know how to achieve this?

  2. #2
    Join Date
    Jan 2009
    Posts
    38

    Re: Visual C++ to end a loop & exit from program?

    HEy you can put your condition in else block where you can define if the input == quit or no. 404 etc. then put your exit condition
    Notice one thing if you are accepting the value in your function as an integer or variable?

    I think this way you can end your loop easily!

    Hope this helps!

  3. #3
    Join Date
    Jan 2009
    Posts
    44

    Re: Visual C++ to end a loop & exit from program?

    On occasion, it may be necessary to end a program (or section of a program) earlier than its normal termination. Below are examples of methods of interrupting or terminating programs or loops.

    1. USE return 0;
    It returns a value of 0 to the IDE indicating that the program reached normal termination.

    2. USE exit( );
    the exit( ) function also ends a program before its normal termination. It requires the Standard Library header file, stdlib.h. The format is exit(value);
    where value is an integer variable or value.
    Using exit(1); returns a value of 1 to the IDE indicating that an error must have occurred. This process is often used for error trapping.

    3. USE break;

    The break statement gets you out of a loop. No matter what the loop's ending condition, break immediately says "I'm outta here!" The program continues with the next statement immediately following the loop. Break stops only the loop in which it resides. It does not break out of a "nested loop" (a loop within a loop).

    I recommend you use break; statement to terminate the loop!

  4. #4
    Join Date
    Jan 2009
    Posts
    22

    Re: Visual C++ to end a loop & exit from program?

    A Loop with an Exit Condition

    I hope this will make you understand the concept in much batter way to exit or terminate the loop!

  5. #5
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Visual C++ to end a loop & exit from program?

    The solution is simple, and is based on the fact that Visual Basic supports as many as three different kinds of loops: For, Do, and While. Each kind of loop supports a corresponding Exit keyword (Exit For, Exit Do, and Exit While), thus you can rewrite the code as follows:

    Code:
    Dim i As Integer = 1
    Do While i <= 10
       For j As Integer = 1 To 20
          If Evaluate(i, j) = 0 Then Exit Do
          ' Do something here
       Next
       i += 1
    Loop
    You can use the same technique when you have up to three nested loops.

    Incidentally, you can't adopt this technique in C#, because its break statement doesn't have the same "semantics power" of the Exit keyword in VB.

Similar Threads

  1. How to eliminate the bugs in a program of visual basic?
    By sRIPRIYA in forum Software Development
    Replies: 4
    Last Post: 03-01-2011, 10:46 PM
  2. What are the steps to execute Visual basic program?
    By Asis in forum Software Development
    Replies: 3
    Last Post: 02-01-2011, 08:22 AM
  3. How to use the do-while loop statement in java program?
    By KALIDA in forum Software Development
    Replies: 5
    Last Post: 27-01-2010, 07:53 PM
  4. Calculator program using visual basic
    By Panchu in forum Software Development
    Replies: 3
    Last Post: 24-11-2009, 07:19 PM
  5. Replies: 3
    Last Post: 22-09-2009, 09:38 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,557,091.72160 seconds with 16 queries