Suppose I'm running a loop and the loop should continue unless I get a garbage value.
So what will be the terminal condition. How can garbage value be checked in if condition?

How do you know if a value is a "garbage" value?

Then you need to figure out what the heck a "garbage" value is before you can tell the computer how to recognize one. You can't write code to do something you yourself are unable to do manually.

So it's impossible to find out the garbage value as in dynamic arrays on paper?

The word "garbage" is meaningless outside the context of your problem domain. You need to define what it means to your program. For example, if the domain of the program is to read numbers from 0 to 10, then anything that's not a number and any number outside the range of 0-10 constitutes garbage.

So...what input does your program expect, and what input falls outside of that expectation? You want to identify and allow only the expected input and treat everything else as garbage.

Then checking for garbage values is as simple as this:

int value;

if (!(cin >> value)) {
        The stream begins with garbage

    // Restore the stream to a good state

    // Clear the entire line since you don't know where the garbage ends
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

Formatted input will validate the contents of the stream for you, and probably far more completely than if you tried to validate integers manually.

is there any pattern to find out garbage value????????
this is our homework today ,i think no, there is not any possible fixed pattern but our sir clamied it ....

is there any pattern to find out garbage value????????

The term "garbage" is meaningless without context. What's garbage for one program could be perfectly valid for another program. So step 1 is to clearly and unambiguously define what garbage means to your program. Once that's done, you can absolutely create a pattern matcher to detect legit values and everything else is garbage.

You can't define what is a garbage value any more than you can write down all the wrong answers to a question. You decide what constitutes a correct value and try to define a pattern that matches all correct values. Anything else is garbage. A pattern for an integer might be "an optional leading plus or minus sign followed by any number of digits." If you can't handle an arbitrarily large number of digits then your pattern becomes "an optional leading plus or minus sign followed by (as an example) ont to nine digits."

There are web sites that can help you with the patterns, such as regexpr and regular expression tester.

commented: I get it. Only right answers, everything else is garbage (except in politics where all is garbage.) +12