Control statements and Loops

Source: Internet
Author: User

If statement

if (a>b) {   printf ("a>b");} else if (a<b)
{
printf ("A<b");
}else{
printf ("a==b");
}

Switch statement

Switch (expr1),expr1 is an integer expression, an integer expression can be an int primitive type or an integer wrapper type, because,Byte,short,char can be implicitly converted to int, so, These types, as well as these types of wrapper types, are also available.   So the arguments passed to the switch and case statements should be int, short , char , or Byte, as well as an enum. long,string can not act on Swtich.

The parameter type of switch in JDK 1.7 can be a string type.

Switch(a) { Casea==1: printf ("a==1");  Break;  Casea==2: printf ("a==2");  Break; default: printf ("a is other");}

For loop

 for (;;) {  ......}

While loop

 while (condition (_bool type)) {...}

First judge, then execute, condition is true to execute while content

Do...while Cycle

 Do {   ...........}  while (Condition (_bool type))

Perform the second judgment first, the condition is true then loop again, otherwise jump out

Goto statement

How to use:

Make a tag in one place of the code first, with the tag name not limited to it, plus one after the tag name:

When you want to jump to the marker position, use the goto tag name; Yes, like a force change

#include <stdio.h>intMainvoid){    /*a number in addition to more than 3 2, in addition to more than 5 3 in addition to 7 more than 2, to find this number*/    intA=3; intb=5; intC=7; intx=2; LOOP1:if(%5!=3) {x=x+3; GotoLoop1; } LOOP2:if(%7!=2) {x=x+3; GotoLoop2; } printf ("The number is %d", x); return 0;}

1, when you need to exit the loop in a multiple loop, break can only exit the current loop and use Goto to jump directly.
2, as a condition, direct jump.

The principle of the Goto statement can be consulted by the reader.

1) The Goto statement can only be used to goto the same function, but not from a function to goto another function.

2) When using a goto statement to goto within the same function, Goto should start at the end of a small function within the function, and the destination label of the Goto should be the beginning of another small function within the function.

3) It is not possible to goto another position from a position in a complex execution state, such as jumping out of multiple nested loops.

4) should avoid jumping like two directions. This is most likely to lead to "noodle code".

Break,continue,return keywords

Break and continue are generally placed in loops and in switch statements

Break refers to jumping out of a loop, just this loop.

Continue refers to skipping this cycle.

There is no loop in switch, but similar to a loop, continue is skipping the case to perform the next one, and break simply jumps out of the switch statement.

Return: The scope of application is much wider.

Role 1: The return, the program can be ended, can not take parameters, if your program runs here, you do not run back, you can add a return statement, you will not be executed
Function 2: Returns the value of the specified type, when defining the function is not an int af (); int is the return value, and the return is followed by the returned value, and the function is executed to assign a value to the other variable, whose value is the value after return.

Control statements and Loops

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.