The difference between the Break,continue,return

Source: Internet
Author: User
the difference between the Break,continue,returnThese 3 keywords have been used recently, so I'd like to summarize his usage.

1 break directly out of the current loop, starting from outside the current loop, ignoring any other statements in the loop body and cyclic condition testing. He can only jump out of the loop, if your cycle is nested loops, then you need to follow your nesting level, and gradually use the break to jump out.

2 Continue is also the termination of the current cycle, but he does not jump out of the loop, but continue to judge the loop condition to execute the statement. He can only end a process in the loop, but cannot terminate the loop to continue.

3 The return statement can be used to enable the executing branch program to be returned to the calling method. Puzzling

Say return

Return terminates the operation of the current function and returns the action to the caller.
If it is in the main function, it indicates that the operation is returned to the operating system.

Return is not a value that must be returned.
void func (void)
{
......
......
Return
}


Of course, even if you do not write the return, the function is returned to the caller after execution. Writing return is a clear style that can prevent some unexpected mistakes. So the book only says should write, not must write.
If you meet a certain condition to exit, you can return, or you can not write this code, when the program execution to "}" will automatically return, the problem in fact, you go to the machine to try to know. However, as a good programming habit, each function has a return statement is true, which is more readable, and conducive to the maintenance of the program ~ ~ ~ ~

The return instruction is returned with a specified data to the calling function.

Another function is to end the execution of the function ...

The operation in a child function cannot change a variable already defined in main ()
int Add (int a, int b)
{
int c;
C=a+b;
}
Main ()
{
int a=1,b=2, c=0
Add (a,b);
printf ("/nc=%d/n", c);
}
You look at the final result or c=0;
because the computer executes a program when he sees only main (),
He is very loyal from the first sentence of main () to the last sentence,
in the middle of the add () call;
when the main () function is hung
that is, stopping the system puts main () in a block of memory X,
when the system divides a memory y into the Add () function;
that is, the ABC in main () and the ABC in Add ();
is not the same thing;
They are just equal in value;
The system first passes the value of a;b to add;
and then runs;
C=3
Then the system does not find a return in Add, and
the child function finishes the operation;
The result in memory Y is not given to main ();
This is the end of the add run;
The system wakes up main () in memory X;
start the next sentence;
The add function does everything in vain;
He didn't give his results to the main () function;
So c is still 0;
Add (int a, int b)
{
int c;
C=a+b;
return c;
}
Main ()
{
int a=1, b=2, c=0
C=add (a,b),
printf ("/nc=%d/n", c),
/* can also write */
printf ("/nc=%d/n", Add (a,b));
}
wants to change something in main () by using a child function; The
has two methods: 1 return; 2 through pointer;
Here I'm not going to say it. C # There is no pointer to me is the first to learn the first;
we use the most is the pointer; urn;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.