Koşullu İfadeler JavaKomut dosyası: if, else ve else if

JavaKomut Dosyası Koşullu İfadeleri

Temel olarak üç tür koşullu ifade vardır: JavaSenaryo.

  1. if Bildirimi: 'İf' ifadesi, bir koşulu temel alarak kodu çalıştırır.
  2. if…else Bildirimi: if…else ifadesi iki kod bloğundan oluşur; Bir koşul doğru olduğunda ilk kod bloğunu çalıştırır, koşul yanlış olduğunda ikinci kod bloğunu çalıştırır.
  3. if…else if…else İfadesi: Birden fazla koşulun test edilmesi gerektiğinde ve hangi koşulun doğru olduğuna bağlı olarak farklı kod bloklarının yürütülmesi gerektiğinde if…else if…else ifadesi kullanılır.

Koşullu İfadeler nasıl kullanılır?

Koşullu ifadeler, farklı koşullara dayalı olarak yürütme akışına karar vermek için kullanılır. Koşul doğruysa bir işlemi, koşul yanlışsa başka bir işlemi gerçekleştirebilirsiniz.

Koşullu İfadeleri Kullan JavaSenaryo

If ifadesi

Sözdizimi:

if(condition)
{
	lines of code to be executed if condition is true
}

Sen kullanabilirsiniz if Yalnızca belirli bir durumu kontrol etmek istiyorsanız bildirimi kullanın.

Bunu kendiniz deneyin:

<html>
<head>
	<title>IF Statments!!!</title>
	<script type="text/javascript">
		var age = prompt("Please enter your age");
		if(age>=18)
		document.write("You are an adult <br />");
		if(age<18)
		document.write("You are NOT an adult <br />");
	</script>
</head>
<body>
</body>
</html>

If…Else ifadesi

Sözdizimi:

if(condition)
{
	lines of code to be executed if the condition is true
}
else
{
	lines of code to be executed if the condition is false
}

Sen kullanabilirsiniz If….Else iki koşulu kontrol etmeniz ve farklı bir kod kümesini yürütmeniz gerekiyorsa.

Bunu kendiniz deneyin:

<html>
<head>
	<title>If...Else Statments!!!</title>
	<script type="text/javascript">
		// Get the current hours
		var hours = new Date().getHours();
		if(hours<12)
		document.write("Good Morning!!!<br />");
		else
		document.write("Good Afternoon!!!<br />");
	</script>
</head>
<body>
</body>
</html>

If…Else If…Else ifadesi

Sözdizimi:

if(condition1)
{
	lines of code to be executed if condition1 is true
}
else if(condition2)
{
	lines of code to be executed if condition2 is true
}
else
{
	lines of code to be executed if condition1 is false and condition2 is false
}

Sen kullanabilirsiniz If….Else If….Else ikiden fazla koşulu kontrol etmek istiyorsanız bildirimi.

Bunu kendiniz deneyin:

<html>
<head>
	<script type="text/javascript">
		var one = prompt("Enter the first number");
		var two = prompt("Enter the second number");
		one = parseInt(one);
		two = parseInt(two);
		if (one == two)
			document.write(one + " is equal to " + two + ".");
		else if (one<two)
			document.write(one + " is less than " + two + ".");
		else
			document.write(one + " is greater than " + two + ".");
	</script>
</head>
<body>
</body>
</html>