The Break Statement
(break statement java, break statement in c, break statement python, break statement matlab, break statement in perl, use of break statement in c language, break statement c++ tutorial, break statement in c example)
(break statement java, break statement in c, break statement python, break statement matlab, break statement in perl, use of break statement in c language, break statement c++ tutorial, break statement in c example)
The break declaration cuts off the flow
of command. We have glimpsed in switch declaration that when a true case is
discovered, the flow of control moves through every declaration down ward. We
desire that only the declarations of factual case should be performed and the
remaining should be skipped. For this reason, we use the break declaration. We
compose the shatter declaration after the declarations of a case. Therefore,
when a factual case is found and its declarations are executed then the break
declaration cuts off the flow of command and control leaps out of the witch
statement. If we want to do the identical task for two cases, like in preceding
demonstration for ‘A’ and ‘a’, then we don't put break statement after the
first case. We write both the situations ( or the situations may be more than
two) line by line then write widespread statements to be executed for these
cases. We write the shatter declaration after these widespread declarations. We
should use the break statement is necessary after the declarations of each
case. The shatter declaration is necessary in swap structure, without it the
swap structure becomes illogical. As without it all the declaration will execute
after first agree case is found.
The break statement
is also used in decision structures other than switch structure. We have seen that in while, do-while and for loops, we have to violate some
condition explicitly to terminate the loop before its complete repetitions. As
in a program for guessing a character, we make a variable myNum greater than 5 to violate the while condition and exit the loop if the correct character is
guessed before five tries. In these loops, we can use the break statement
to exit a loop. When a break statement is encountered in a loop, the
loop terminates immediately. The control exits the inner most loop if there are
nested loops. The control passes to the statement after the loop. In the
guessing character example, we want that if the character is guessed in first
or second attempt, then we print the message
‘Congratulations, You guess is correct’ and exit the loop. We can do this by
using a break statement with an if statement. If the character is
guessed, we print the message. Afterwards, the break statement is
executed and the loop terminates. So we can write this as follows.
if ( c == ‘z’ ) // c
is input from user
{
cout
<< “Great, Your guess is correct” ; break;
}
Thus, break statement can be used
to jump out of a loop very quickly.
No comments:
Post a Comment