Created: 2023-01-22 15:50
Status: #concept
Subject: Programming Logic
Tags: C JavaScript Python Boolean Operator

Conditional Statements

Description

Conditional Statements control Program Flow with the use of Boolean Expressions.

  1. if...else
if (condition) {
	/* code to run if condition is true */
} else if (another_condition) {
	/* code to run if another_condition is true */
} else {
	/* run this code if everything above is false */
}
  1. switch (expression) { case value: }
switch (expression) {
  case choice1:
    run this code
    break;

  case choice2:
    run this code instead
    break;

  // include as many cases as you like

  default:
    actually, just run this code
}
  1. Ternary Operator condition ? exprIfTrue : exprIfFalse
const greeting = isBirthday
  ? 'Happy birthday Mrs. Smith — we hope you have a great day!'
  : 'Good morning Mrs. Smith.';

References