首頁  >  文章  >  Java  >  Java中的控制語句

Java中的控制語句

王林
王林原創
2024-08-30 15:22:59504瀏覽

Java 中的控制語句是有助於決定從 Java 中的一個語句到另一語句的控制流的語句。 Java 中的控制語句有多種型別。在本文中,我們將觀察控制語句的不同面向及其一些範例。不同類型的控制語句是:

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

決策聲明:

  • if-else,巢狀 if-else 語句
  • 切換大小寫語句

重複/循環語句:

  • For 循環
  • 對於每個循環
  • While 循環
  • 執行 while 迴圈

跳躍語句:

  • 休息
  • 繼續
  • 轉到
  • 回傳

這些是Java中用作流程控制語句的上述語句,它們分為決策語句、重複或循環語句和跳躍語句。

Java 中的決策語句

在決策語句中,我們將看到 if-else 和嵌套的 if-else 語句以及 Switch case 語句。我們還將看到顯示此類語句的有效性和執行情況的編碼範例和範例輸出。

1. If else 語句

if-else 語句以條件方式運作。文法如下:

文法:

if(condition)
Statement 1
else
Statement 2

範例:

在第一個 if-else 範例中,我們將查看使用者輸入的數字是否大於 100。如果數字大於 100,則會相應地顯示輸出。

代碼:

import java.io.*;
public class Example1
{
public static void main(String args[])throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("ENTER A NUMBER");
int n= Integer.parseInt(br.readLine());
if (n>100)
System.out.println("NUMBER ENTERED IS GREATER THAN 100");
else
System.out.println("NUMBER ENTERED IS LESS THAN 100");
}
}

輸出:

Java中的控制語句

Java中的控制語句

在下面的程式中,我們輸入兩個數字。每當我們輸入 250 作為數字時,程式顯示它大於 100,每當我們輸入 65 作為數字時,程式顯示該數字小於 100。

2.嵌套的 If else 語句

嵌套的if-else語句中,有多個if條件,最後有一個print語句。文法如下:

文法:

if (condition1)
if(condition2)
if (condition3)
Statement 1

範例:

在嵌套的 if 語句中,我們使用兩個或三個 if-else 語句檢查條件,然後最終得出結論。我們進一步檢查該數量是否大於200;如果大於,我們將其列印為大於 200。

代碼:

import java.io.*;
public class Example2
{
public static void main(String args[])throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("ENTER A NUMBER");
int n= Integer.parseInt(br.readLine());
if (n>100)
{
if(n>200)
System.out.println("NUMBER ENTERED IS GREATER THAN 200");
}
}
}

輸出:

Java中的控制語句

Java中的控制語句

我們輸入兩個都大於200的數字,我們發現正確的輸出是兩個數字都大於200。

3. Switch Case 語句

在switch case中,有多個case,從中選出一個。文法如下:

文法:

switch(Variable)
case 1:
case 2:
case 3:
case n:

範例:

在這個範例中,我們將輸入一個數字,程式將傳回使用者傳回的數字。這是在 BlueJ 程式設計介面中運行的 switch case 語句的簡單範例。

代碼:

import java.io.*;
public class Example3
{
public static void main(String args[])throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("ENTER A NUMBER BETWEEN 1 TO 5");
int n= Integer.parseInt(br.readLine());
switch(n)
{
case 1: System.out.println("NUMBER ENTERED IS 1");
break;
case 2: System.out.println("NUMBER ENTERED IS 2");
break;
case 3: System.out.println("NUMBER ENTERED IS 3");
break;
case 4: System.out.println("NUMBER ENTERED IS 4");
break;
case 5: System.out.println("NUMBER ENTERED IS 5");
break;
}
}
}

輸出:

Java中的控制語句

在上面的程式碼中,我們輸入數字為4,程式回傳輸入的數字是4。

Java 中的重複/循環語句

以下是 Java 中的重複/循環子句:

A. For 迴圈

在for迴圈中,迴圈依照使用者初始化的次數進行。以下是語法。

文法:

for(initialization, condition, update)
Statement 1

範例:

在 for 迴圈範例中,我們將列印從 3 到 10 的奇數。我們對對應的程式使用 for 迴圈。

代碼:

import java.io.*;
public class Example4
{
public static void main(String args[])throws IOException
{
System.out.println("Odd numbers from 3 to 10 are as follows");
for(int  i=3; i<10; i+=2)
{
System.out.println(i);
}
}
}

輸出:

Java中的控制語句

在上面的程式中,我們看到從3到10開始的奇數,印出來的數字是3,5,7,9。

B. While 迴圈

在while迴圈中,當條件為真時執行語句。文法如下:

文法:

while(Condition)
Statement 1

範例:

使用 while 循環,我們現在要找出一個數字的逆數。這個程式很強大,可以找到任何整數的倒數。

代碼:

import java.io.*;
public class Example5
{
public static void main(String args[])throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("ENTER A NUMBER");
int n= Integer.parseInt(br.readLine());
int digit, rev=0;
while(n>0)
{
digit= n%10;
rev= (rev*10) +digit;
n=n/10;
}
System.out.println("Reverse number is " +rev);
}
}

Output:

Java中的控制語句

In the above program, we find the reverse of a particular number. The number entered is 635, and the reverse of the number is 536, as displayed on the output screen.

Jumping Statements in Java

The jumping statements in java are explained below.

Break Statement

There can be in the for loop in the break statement while loop or in switch case. Following is the syntax.

Syntax:

for(Statements)
break;
while(Statements)
break;

Example:

In this example, we will see a menu-driven program, and we see the break statement’s application.

Code:

import java.io.*;
public class Example6
{
public static void main(String args[])throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("ENTER A NUMBER BETWEEN 1 TO 5");
int n= Integer.parseInt(br.readLine());
switch(n)
{
case 1: System.out.println("NUMBER ENTERED IS 1");
break;
case 2: System.out.println("NUMBER ENTERED IS 2");
break;
case 3: System.out.println("NUMBER ENTERED IS 3");
break;
case 4: System.out.println("NUMBER ENTERED IS 4");
break;
case 5: System.out.println("NUMBER ENTERED IS 5");
break;
default: System.out.println("Number entered is not between 1 to 5");
break;
}
}
}

Output:

Java中的控制語句

The above code is very similar to the code used in the switch case statements. The break statement is generally used in the switch case statement. The break statement is also used in the if-else condition where the if-else statements need to be terminated. The above program asks for the number entered between 1 to 5. If the number is not between 1 to 5, then there is a default print that the number entered is not between 1 to 5. In the above case, we enter the number as 65, and it prints accordingly that the number entered is not between 1 to 5.

Conclusion – Control Statement in Java

In this article, we come across the control statements in Java that are present. We take a look at the Looping statements, the Conditional statements, and others that are present. We also look at the programming aspects of the statements and how the statements are used inside the code. Control statements are quite used in Java, and they are present in every other programming language. They are used significantly throughout all programs for smooth execution.

以上是Java中的控制語句的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn