logo

הצהרת מתג TypeScript

משפט המתג TypeScript מבצע משפט אחד ממספר תנאים. הוא מעריך ביטוי על סמך הערך שלו שיכול להיות בוליאני, מספר, בייט, קצר, int, ארוך, סוג enum, מחרוזת וכו'. להצהרת switch יש בלוק קוד אחד המתאים לכל ערך. כאשר תימצא ההתאמה, הבלוק המתאים יבוצע. משפט switch עובד כמו הצהרת if-else-if ladder.

יש לזכור את הנקודות הבאות בהצהרת switch:

  • יכול להיות מספר N של מקרים בתוך הצהרת switch.
  • ערכי המקרה חייבים להיות ייחודיים.
  • ערכי המקרה חייבים להיות קבועים.
  • לכל הצהרת מקרה יש הצהרת break בסוף הקוד. הצהרת הפסקה היא אופציונלית.
  • להצהרת switch יש בלוק ברירת מחדל שנכתב בסוף. הצהרת ברירת המחדל היא אופציונלית.

תחביר

 switch(expression){ case expression1: //code to be executed; break; //optional case expression2: //code to be executed; break; //optional ........ default: //when no case is matched, this block will be executed; break; //optional } 

הצהרת ה-switch מכילה את הדברים הבאים. יכולים להיות כל מספר של מקרים בתוך הצהרת switch.

מקרה: אחרי המקרה צריך להיות קבוע אחד בלבד ולאחר מכן נקודה-פסיק. הוא לא יכול לקבל משתנה או ביטוי אחר.

לשבור: יש לכתוב את הפסקה בסוף הבלוק כדי לצאת מהצהרת switch לאחר ביצוע בלוק מקרה. אם לא נכתוב break, הביצוע ממשיך עם הערך התואם לבלוק המקרים הבא.

בְּרִירַת מֶחדָל: יש לכתוב את בלוק ברירת המחדל בסוף הצהרת switch. זה מופעל כאשר אין מקרה יתאים.

הצהרת מתג TypeScript

דוגמא

 let a = 3; let b = 2; switch (a+b){ case 1: { console.log('a+b is 1.'); break; } case 2: { console.log('a+b is 5.'); break; } case 3: { console.log('a+b is 6.'); break; } default: { console.log('a+b is 5.'); break; } } 

תְפוּקָה:

הצהרת מתג TypeScript

החלף מארז עם String

 let grade: string = 'A'; switch (grade) { case'A+': console.log('Marks >= 90'+'
&apos;+&apos;Excellent&apos;); break; case&apos;A&apos;: console.log(&apos;Marks [ &gt;= 80 and = 70 and = 60 and <70 ]'+'
'+'average'); break; case'c': console.log('marks < 60'+'
'+'below average'); default: console.log('invalid grade.'); } pre> <p>In this example, we have a string variable grade. The switch statement evaluates grade variable value and match with case clauses and then execute its associated statements.</p> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/typescript-tutorial/79/typescript-switch-statement-3.webp" alt="TypeScript Switch Statement"> <hr> <h2>Switch Case with Enum</h2> <p>In TypeScript, we can use the switch case with Enum in the following ways.</p> <h3>Example</h3> <pre> enum Direction { East, West, North, South }; var dir: Direction = Direction.North; function getDirection() { switch (dir) { case Direction.North: console.log(&apos;You are in North Direction&apos;); break; case Direction.East: console.log(&apos;You are in East Direction&apos;); break; case Direction.South: console.log(&apos;You are in South Direction&apos;); break; case Direction.West: console.log(&apos;You are in West Direction&apos;); break; } } getDirection(); </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/typescript-tutorial/79/typescript-switch-statement-4.webp" alt="TypeScript Switch Statement"> <hr> <h2>TypeScript Switch Statement is fall-through.</h2> <p>The TypeScript switch statement is fall-through. It means if a break statement is not present, then it executes all statements after the first match case.</p> <h3>Example</h3> <pre> let number = 20; switch(number) { //switch cases without break statements case 10: console.log(&apos;10&apos;); case 20: console.log(&apos;20&apos;); case 30: console.log(&apos;30&apos;); default: console.log(&apos;Not in 10, 20 or 30&apos;); } </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/typescript-tutorial/79/typescript-switch-statement-5.webp" alt="TypeScript Switch Statement"></70>

תְפוּקָה:

הצהרת מתג TypeScript

הצהרת המתג של TypeScript היא נופלת.

הצהרת המתג של TypeScript היא נופלת. זה אומר שאם הצהרת break לא קיימת, אז היא מבצעת את כל ההצהרות לאחר מקרה ההתאמה הראשון.

דוגמא

 let number = 20; switch(number) { //switch cases without break statements case 10: console.log(&apos;10&apos;); case 20: console.log(&apos;20&apos;); case 30: console.log(&apos;30&apos;); default: console.log(&apos;Not in 10, 20 or 30&apos;); } 

תְפוּקָה:

הצהרת מתג TypeScript