Conditional Constructs if if-else if-elseif-else
Conditional Constructs • Different situations  Different solutions • Complex problem  Multiple possible solutions
Problem with problems • Finding the best fit solution for each problem • Check different permutations and combinations
IF • If a particular condition is true -> perform a specific action • Eg: – if (fruit == "apple") fruit_color = "Red";
If-else • If a particular condition is true -> perform a specific action otherwise perform next action • Eg: – if(fruit == "apple") fruit_color = "Red"; else fruit_color = "Orange";
If-elseif-else • If a particular condition is true -> perform a specific action • Otherwise check another condition, if true  perform that specific action • If none of above conditions is true  perform a specific action • Eg: if(fruit=="red_apple") fruit_color = "red"; elseif (fruit=="green_apple") fruit_color="green"; else fruit_color="orange";
Using Operators • Check a specific condition • Analyse the problem • Find the best fit solution by checking multiple conditions simulaneously • Form relationship between different conditions to be analysed • Do arithmetic calculations
Operators X = 10 Operand Operator Operand
ASSIGNMENT OPERATOR =
Assignment Operator • = • Assign a value to a variable/constant/array • Value on the RHS is assigned to the storage on LHS • Eg: apples_bunch = 12; PI = 3.14; fruit = "apple";
RELATIONAL OPERATOR >,<,>=,<=,==,!=
Relational Operator • Form relationship between two conditions • Compare the values of parameters
Relational • To make a comparison while checking a condition, use relational operators. Operator Functionality Let’s consider x=5; y=10; > More Than x>y; False < Less Than x<y; True >= More Than or Equal To x>=y; False <= Less Than or Equal To x<=y; True == Equal to x==y; False != Not Equal to x!=y; True
IF • If a particular condition is true -> perform a specific action • Eg: if (fruit == 'apple') fruit_color = 'Red';
LOGICAL OPERATOR &&, ||, !
Logical Operator • Form relationship between multiple conditions • Also known as combinational operator – Combines different conditions together to find solution – AND- && – OR- || – NOT- !
Logical • To combine two or more situations of conditional checking use, logical operators. Operator Functionality Condition Example && Both the sides of operator should result true x<y && y!=x True x<y && y==x False || Either side of operator should result true x<y || y!=x True x<y || y!=x True ! Inverse the result x!<y False
If-else • Checking multiple conditions and parameter values. • Eg: if(fruit == "apple" && fruit_color == "red") fruit = "Red Apple"; else fruit = 'Orange';
If-elseif-else • Checking multiple conditions • Eg: if(fruit=="apple" && fruit_color == "red") fruit = "Red Apple"; elseif (fruit=="apple" && fruit_color == "green") fruit = "Green Apple"; else fruit = "Orange";
ARITHMETIC OPERATOR +,-,*,/,%
Arithmetic • To compute arithmetic value we use Arithmetic Operators. Operator Functionality Condition Example + Addition x+y 15 - Subtraction x-y -5 * Multiplication x*y 50 / Division x/y 0 % Remainder x%y 5
Arithmetic Function • Eg: if(fruit=="apple" && fruit_color == "red") red_apple = red_apple+1; elseif (fruit=="apple" && fruit_color == "green") green_apple = green_apple+1; else orange = orange+1;
UNARY OPERATOR +,-,++,--
Unary Operator • Works with a single operand • There is no RHS (Right hand side) to the condition • Performing certain operations with a shorthand
Unary Operators • Used with single operand Operator Functionality Condition Example + Positive Value X= +15 - Negative Value X= -5 ++ Increment X++ or ++X -- Decrement X-- or --X ! NOT !x
Conditional Constructs and Operators used in Javascript https://www.codewizacademy.com https://www.facebook.com/codewizacademy/ https://www.instagram.com/codewizacademy/

Conditional Constructs in Javascript

  • 1.
  • 2.
    Conditional Constructs • Differentsituations  Different solutions • Complex problem  Multiple possible solutions
  • 3.
    Problem with problems •Finding the best fit solution for each problem • Check different permutations and combinations
  • 4.
    IF • If aparticular condition is true -> perform a specific action • Eg: – if (fruit == "apple") fruit_color = "Red";
  • 5.
    If-else • If aparticular condition is true -> perform a specific action otherwise perform next action • Eg: – if(fruit == "apple") fruit_color = "Red"; else fruit_color = "Orange";
  • 6.
    If-elseif-else • If aparticular condition is true -> perform a specific action • Otherwise check another condition, if true  perform that specific action • If none of above conditions is true  perform a specific action • Eg: if(fruit=="red_apple") fruit_color = "red"; elseif (fruit=="green_apple") fruit_color="green"; else fruit_color="orange";
  • 7.
    Using Operators • Checka specific condition • Analyse the problem • Find the best fit solution by checking multiple conditions simulaneously • Form relationship between different conditions to be analysed • Do arithmetic calculations
  • 8.
  • 9.
  • 10.
    Assignment Operator • = •Assign a value to a variable/constant/array • Value on the RHS is assigned to the storage on LHS • Eg: apples_bunch = 12; PI = 3.14; fruit = "apple";
  • 11.
  • 12.
    Relational Operator • Formrelationship between two conditions • Compare the values of parameters
  • 13.
    Relational • To makea comparison while checking a condition, use relational operators. Operator Functionality Let’s consider x=5; y=10; > More Than x>y; False < Less Than x<y; True >= More Than or Equal To x>=y; False <= Less Than or Equal To x<=y; True == Equal to x==y; False != Not Equal to x!=y; True
  • 14.
    IF • If aparticular condition is true -> perform a specific action • Eg: if (fruit == 'apple') fruit_color = 'Red';
  • 15.
  • 16.
    Logical Operator • Formrelationship between multiple conditions • Also known as combinational operator – Combines different conditions together to find solution – AND- && – OR- || – NOT- !
  • 17.
    Logical • To combinetwo or more situations of conditional checking use, logical operators. Operator Functionality Condition Example && Both the sides of operator should result true x<y && y!=x True x<y && y==x False || Either side of operator should result true x<y || y!=x True x<y || y!=x True ! Inverse the result x!<y False
  • 18.
    If-else • Checking multipleconditions and parameter values. • Eg: if(fruit == "apple" && fruit_color == "red") fruit = "Red Apple"; else fruit = 'Orange';
  • 19.
    If-elseif-else • Checking multipleconditions • Eg: if(fruit=="apple" && fruit_color == "red") fruit = "Red Apple"; elseif (fruit=="apple" && fruit_color == "green") fruit = "Green Apple"; else fruit = "Orange";
  • 20.
  • 21.
    Arithmetic • To computearithmetic value we use Arithmetic Operators. Operator Functionality Condition Example + Addition x+y 15 - Subtraction x-y -5 * Multiplication x*y 50 / Division x/y 0 % Remainder x%y 5
  • 22.
    Arithmetic Function • Eg: if(fruit=="apple"&& fruit_color == "red") red_apple = red_apple+1; elseif (fruit=="apple" && fruit_color == "green") green_apple = green_apple+1; else orange = orange+1;
  • 23.
  • 24.
    Unary Operator • Workswith a single operand • There is no RHS (Right hand side) to the condition • Performing certain operations with a shorthand
  • 25.
    Unary Operators • Usedwith single operand Operator Functionality Condition Example + Positive Value X= +15 - Negative Value X= -5 ++ Increment X++ or ++X -- Decrement X-- or --X ! NOT !x
  • 26.
    Conditional Constructs and Operators usedin Javascript https://www.codewizacademy.com https://www.facebook.com/codewizacademy/ https://www.instagram.com/codewizacademy/