square-rightshorthand if & nested if

พลวัชร์ กระกรกุล 630710131

Shorthand if

การเขียนคำสั่ง if-else แบบสั้นๆ เรียกว่า Shorthand if หรือ Ternary Operator

ซึ่ง ternary operator จะประกอบด้วย operator ทั้งหมด 3 ตัว สามารถใช้แทนโค้ดหลายบรรทัดให้อยู่บรรทัดเดียวได้ มักใช้แทนคำสั่ง if-else แบบง่ายๆ

เปรียบเทียบความแตกต่างของ Shorthand if แต่ละภาษา

syntax ของภาษา PHP , C และ Java จะมีความคล้ายกันแต่ Python จะมีความแตกต่าง ดังตัวอย่างข้างล่าง

(condition) ? expressionTrue : expressionFalse;

เครื่องหมาย ? ใช้สำหรับสร้าง Ternary condition โดยตรวจสอบเงื่อนไขแล้วส่งค่ากลับตามผลลัพธ์ของเงื่อนไขนั้น

variable คือ ชื่อของตัวแปรที่ใช้เก็บค่า

condition คือ การเช็คเงื่อนไขเราที่กำหนด

expressionTrue คือ ถ้าเงื่อนไขเป็น true จะใช้ค่านี้

expressionFalse คือ ถ้าเงื่อนไขเป็น false จะใช้ค่านี้แทน

ในทุกๆ ภาษาเราจะเห็นว่ามันมีการเขียนคำสั่ง if-else แบบสั้นๆ ซึ่งจะช่วยให้การสร้างเงื่อนไข แบบ if...else ทำได้ง่ายและสะดวก ซึ่งการเขียนคำสั่ง if-else ทั้งแบบปกติหรือแบบสั้นๆ ก็จะได้ผลลัพธ์ที่เหมือนกัน แตกต่างกันที่รูปแบบการเขียน

Original

<?php
	$strGender = "M";
	$strGenderText = "";
	if($strGender == "M")
	{
		$strGenderText = "Male";
	}
	else
	{
		$strGenderText = "Female";
	}

	echo "Your gender = ".$strGenderText;
?>

Shorthand if

Output

Your gender = Male

Nested if

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

อธิบายแบบเข้าใจง่ายๆ คือ การสร้างเงื่อนไขคำสั่ง if-else ที่ซ้อนอยู่ภายในเงื่อนไขคำสั่ง if ได้

เปรียบเสมือนมีด่านเข้าเมือง 3 ชั้น ถ้าผ่านชั้นแรกไปได้ ก็จะไปต่อที่ชั้น 2 และ 3

เปรียบเทียบความแตกต่างของ Nested if แต่ละภาษา

การใช้คำสั่ง Nested if ในภาษา PHP , C , Java และ Python มีโครงสร้างที่คล้ายกัน แต่ก็ต่างกันเพียงเล็กน้อยตามไวยากรณ์(syntax) ของแต่ละภาษา ดังนี้

  • PHP : ใช้ { } สำหรับบล็อกคำสั่ง และวงเล็บ ( ) สำหรับเงื่อนไข

  • C : คล้าย PHP แต่ต้องใช้ ; ปิดท้ายคำสั่ง

  • Java : เหมือน C แต่มีคุณสมบัติการเขียนแบบเชิงวัคถุ

  • Python : ใช้เป็นการย่อหน้าแทน { } และก็ไม่ต้องใส่วงเล็บ ( ) รอบเงื่อนไข

Nested if

Output

Above 10 but not above 20

Video Clip

Presentation

Reference

syntax PHP: thaicreate. 2017, February 10. Syntax(PHP). thaicreate. thaicreatearrow-up-right

syntax C: (n.d.). C Short Hand If Else. w3schools. w3schoolsarrow-up-right

syntax Java: (n.d.). Java Short Hand If...Else (Ternary Operator). w3schools. w3schoolsarrow-up-right

syntax python: dev. 2023, May 31. Basic syntax of if-else shorthand. dev. devarrow-up-right

PHP Shorthand if: thaicreate. 2017, February 10. PHP แบบ Short if/else (Shorthand). thaicreate. thaicreatearrow-up-right

C Shorthand if: (n.d.). C Short Hand If Else. w3schools. w3schoolsarrow-up-right

Java Shorthand if: (n.d.). Java Short Hand If...Else (Ternary Operator). w3schools. w3schoolsarrow-up-right

Python Shorthand if: (n.d.). Python If ... Else. w3schools. w3schoolsarrow-up-right

Nested if: (n.d.). Nested if-else คืออะไร. expert-programming-tutor. expert-programming-tutorarrow-up-right

Nested If Else Statement: geeksforgeeks. 2024, April 10.Nested If Else Statement in Programming. geeksforgeeks. geeksforgeeksarrow-up-right

PHP Nested if: (n.d.). PHP Nested if Statement. w3schools. w3schoolsarrow-up-right

C Nested if: เบนจามิน วอล์คเกอร์. 2024, August 8. PHP Nested if Statement. guru99. guru99arrow-up-right

Java Nested if: mindphp. 2020, October 13. Java - Decision Making. mindphp. mindphparrow-up-right

Python Nested if: (n.d.). Python If ... Else. w3schools. w3schoolsarrow-up-right

Last updated