วิชาภาษาจาวา (java)

คำสั่ง if else ภาษาจาวา (java)

คำสั่ง if else เป็นคำสั่งที่ใช้ในการต้องการให้คำสั่งที่หลุดจากคำสั่ง if แล้วสามารถกระทำต่อในคำสั่ง else ได้ ซึ่งโครงสร้างที่ใช้ในการเขียนก็จะเป็นเช่นเดียวกับคำสั่ง if เพียงแค่ว่ามีการเพิ่ม else เข้ามา เพียงแค่นั้นเอง

ตัวอย่างคำสั่ง if else

class Testif 
{
	public static void main(String[] args) 
	{
		int x = 16;
		int y = 17;
		if (x > y) {
			System.out.println("printif");
		}
			else {
			System.out.println("printelse");
		}
	}
}

ในการใช้งานคำสั่ง if else ให้สังเกต {} ดี ๆ นะครับ ในบ้างครั้งเมื่อสั่งให้โปรแกรมทำงานแล้ว โปรแกรมแสดง error ขึ้นมา ให้เราทำการสังเกตว่าเรา {} ถูกต้องแล้วหรือยัง

 

slot gacor hari ini slot qris slot gacor maxwin slot online slot gacor gampang menang slot gacor hari ini slot online slot slot slot guci777 guci777 fafatogel fafatogel fafatogel fafatogel agen138 slot agen138 slot data hk bola slot bolaslot99 bola slot agen138 link alternatif koin138 link alternatif agen138 slot ag138 slot slot gacor slot thailand