Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 21-02-2009
Member
 
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?
Reply With Quote
  #2  
Old 21-02-2009
Member
 
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!
Reply With Quote
  #3  
Old 21-02-2009
Member
 
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!
Reply With Quote
  #4  
Old 21-02-2009
Member
 
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!
Reply With Quote
  #5  
Old 21-02-2009
Member
 
Join Date: Apr 2008
Posts: 2,001
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Visual C++ to end a loop & exit from program?"
Thread Thread Starter Forum Replies Last Post
How to eliminate the bugs in a program of visual basic? sRIPRIYA Software Development 4 03-01-2011 10:46 PM
What are the steps to execute Visual basic program? Asis Software Development 3 02-01-2011 08:22 AM
How to use the do-while loop statement in java program? KALIDA Software Development 5 27-01-2010 07:53 PM
Calculator program using visual basic Panchu Software Development 3 24-11-2009 07:19 PM
How do we write exit procedure or function in javascript to exit middle of the program? Jevin Software Development 3 22-09-2009 09:38 AM


All times are GMT +5.5. The time now is 11:07 AM.