0% found this document useful (0 votes)
11 views7 pages

Edunext 12

Các câu lệnh cơ bản trong Java bao gồm if-else, switch-case và các vòng lặp. If-else kiểm tra điều kiện boolean và thực thi một khối lệnh. Switch-case chọn khối lệnh dựa trên giá trị biến. Các vòng lặp lặp lại khối lệnh cho đến khi điều kiện không còn đúng.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views7 pages

Edunext 12

Các câu lệnh cơ bản trong Java bao gồm if-else, switch-case và các vòng lặp. If-else kiểm tra điều kiện boolean và thực thi một khối lệnh. Switch-case chọn khối lệnh dựa trên giá trị biến. Các vòng lặp lặp lại khối lệnh cho đến khi điều kiện không còn đúng.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Các câu lệnh cơ bản trong Java khá giống bên C như: if-else, switch-case, các vòng

lặp

- If-else:
Mệnh đề if-else được dùng khi có 1 điều kiện.
Mệnh đề if-else kiểm tra dạng boolean (true-false) của điều kiện đó.
Nếu true thì sẽ thực hiện các câu lệnh của mệnh đề if
Nếu false thì sẽ thực hiện các câu lệnh của mệnh đề false

SYNTAX:

if(điều kiện){
//khối câu lệnh được thực thi nếu điều kiện đúng;
}
else{
//Khối câu lệnh được thực thi nếu câu lệnh sai;
}

Vd:
if (a > b){
System.out.println("a > b ");
}
else {
System.out.println("a <= b");
}

- switch-case
Mệnh đề switch-case trong java được sử dụng để thực thi 1 hoặc nhiều khối
lệnh từ nhiều điều kiện.
Switch-case được sử dụng khi có quá nhiều cấu trúc rẽ nhánh if-else mà các
nhánh if-else này có điểm tương đồng , ngang bằng nhau.
Các giá trị trong switch phải là các giá trị rời rạc, trong case phải là các giá
trị hằng số khi được biên dịch và phải có cùng data type với biến trong
switch.
SYNTAX:
switch (biến){
case value-1:
//Những câu lệnh được thực thi;
//break;
case value-2:
//Những câu lệnh được thực thi;
//break;
case value-3:
//Những câu lệnh được thực thi;
//break;
default: //Những trường hợp còn lại

//Những Câu lệnh được thực thi;


break;
}

Vd:
Int slot;
switch(slot){
case 1:
System.out.println("7:30-9:50");
break;
case 2:
System.out.println("10:00-12:20");
break;
case 3:
System.out.println("12:50-15:10");
break;
case 4:
System.out.println("15:20-17:40");
break;
default:
System.out.println("FPTU have 4 slots for student learning!");
break;
}

- Vòng lặp( for , while, do-while)


Vòng lặp được sử dụng khi muốn  thực thi một số việc giống nhau,
được lặp đi lặp lại nhiều lần cho đến khi tìm được kết quả.
Có 2 kiểu sử dụng các vòng lặp:
 Biết trước số lần lặp : thường dùng for
 Ko biết trước số lần lặp : thường dùng while, do-while
o (while sẽ kiểm tra điều kiện trước khi thực hiện câu lệnh đầu
tiên, do-while sẽ thực hiện câu lệnh đầu tiên trước rồi mới
kiểm tra điều kiện)

SYNTAX:
for (biến chặn đầu ;biến chặn đuôi ; bước nhảy) {
Các câu lệnh được thực hiện;
}
Vd:
for (int i = 0; i < 10; i++) {
System.out.println(" " + i);
}
While ( điều kiện ){
// điều kiện còn đúng thì còn làm
Các câu lệnh được thực hiện;
}
Vd:
int a = 1;
while( a < 10){
System.out.println("" + a);
a ++;
}

do{
//Điều kiện còn đúng thì còn làm
Các câu lệnh được thực hiện;
}while(điều kiện);

2.3
System.in: This is the standard input stream that is used to read characters
from the keyboard or any other standard input device.
System.out: This is the standard output stream that is used to produce the
result of a program on an output device like the computer screen.
Here is a list of the various print functions that we use to output statements:

print(): This method in Java is used to display a text on the console. This text
is passed as the parameter to this method in the form of String. This method
prints the text on the console and the cursor remains at the end of the text at
the console. The next printing takes place from just here.
Syntax:
System.out.print(parameter);
Example:

// Java code to illustrate print()


import java.io.*;

class Demo_print {
public static void main(String[] args)
{

// using print()
// all are printed in the
// same line
System.out.print("GfG! ");
System.out.print("GfG! ");
System.out.print("GfG! ");
}
}
Output:

GfG! GfG! GfG!


println(): This method in Java is also used to display a text on the console. It
prints the text on the console and the cursor moves to the start of the next
line at the console. The next printing takes place from the next line.
Syntax:
System.out.println(parameter);
Example:

// Java code to illustrate println()

import java.io.*;

class Demo_print {
public static void main(String[] args)
{

// using println()
// all are printed in the
// different line
System.out.println("GfG! ");
System.out.println("GfG! ");
System.out.println("GfG! ");
}
}
Output:

GfG!
GfG!
GfG!
printf(): This is the easiest of all methods as this is similar to printf in C.
Note that System.out.print() and System.out.println() take a single argument,
but printf() may take multiple arguments. This is used to format the output in
Java.
Example:

// A Java program to demonstrate working of printf() in Java


class JavaFormatter1 {
public static void main(String args[])
{
int x = 100;
System.out.printf(
"Printing simple"
+ " integer: x = %d\n",
x);

// this will print it upto


// 2 decimal places
System.out.printf(
"Formatted with"
+ " precision: PI = %.2f\n",
Math.PI);

float n = 5.2f;

// automatically appends zero


// to the rightmost part of decimal
System.out.printf(
"Formatted to "
+ "specific width: n = %.4f\n",
n);

n = 2324435.3f;

// here number is formatted from


// right margin and occupies a
// width of 20 characters
System.out.printf(
"Formatted to "
+ "right margin: n = %20.4f\n",
n);
}
}
Output:

Printing simple integer: x = 100


Formatted with precision: PI = 3.14
Formatted to specific width: n = 5.2000
Formatted to right margin: n = 2324435.2500
System.err: This is the standard error stream that is used to output all the
error data that a program might throw, on a computer screen or any standard
output device.
This stream also uses all the 3 above-mentioned functions to output the error
data:
print()
println()
printf()
Example:

// Java code to illustrate standard


// input output streams

import java.io.*;
public class SimpleIO {

public static void main(String args[])


throws IOException
{

// InputStreamReader class to read input


InputStreamReader inp = null;

// Storing the input in inp


inp = new InputStreamReader(System.in);

System.out.println("Enter characters, "


+ " and '0' to quit.");
char c;
do {
c = (char)inp.read();
System.out.println(c);
} while (c != '0');
}
}
Input:

GeeksforGeeks0
Output:

Enter characters, and '0' to quit.


G
e
e
k
s
f
o
r
G
e
e
k
s
0

You might also like