Java flow control of (a) conditions

Java flow control of (a) ---- Conditions

Conditional statements + loop, FIG direct rejection rejection code!

Conditional statements

Java want to execute the corresponding statement when a condition is true.

If a single case

int a=6;
if (a==6)
{
    System.out.println("n=6");
}
System.out.println("已跳出循环");

If the statement is a single statement, braces may be omitted. But proposed to retain braces, conducive to reading, not easily confused.

Statement outside the loop will execute.

Single if / else case

int a=6;
if (a==6)
{
    System.out.println("n=6");
}
else 
{
    System.out.println("n!=6");
}
System.out.println("循环外");

Not you die, I die.

if / else multi-drop case

Scanner s = new Scanner(System.in);
System.out.println("请输入一个整数:");
int n = s.nextInt();
if (n==3)
{
    System.out.println("n=3");
}
else if(n>3)
{
    System.out.println("n>3");
}
else 
{
    System.out.println("n<3");
}
System.out.printf("循环之外,都会输出 %d ",n);

The choice of many. .

Guess you like

Origin www.cnblogs.com/summerday152/p/11844315.html