> Java > java지도 시간 > 본문

Java의 제어문

王林
풀어 주다: 2024-08-30 15:22:59
원래의
502명이 탐색했습니다.

Java의 제어 문은 Java의 한 문에서 다른 문으로의 제어 흐름을 결정하는 데 도움이 되는 문입니다. Java의 제어문에는 다양한 유형이 있습니다. 이 기사에서는 제어문의 다양한 측면을 몇 가지 예제와 함께 살펴보겠습니다. 다양한 종류의 제어문은 다음과 같습니다.

무료 소프트웨어 개발 과정 시작

웹 개발, 프로그래밍 언어, 소프트웨어 테스팅 등

의사결정문:

  • if-else, 중첩된 if-else 문
  • Switch Case문

반복/루핑 문:

  • 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(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의 반복/루핑 절입니다.

아. 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입니다.

베. While 루프

while 루프에서는 조건이 true인 동안 명령문이 실행됩니다. 구문은 다음과 같습니다.

구문:

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 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!