i'm back for the reflection of lab5 :)
so, the topic for lab5 in week 6 is switch statement, logical operators and conditional operator. In this lab, i should be able to use switch statement to specify alternative paths of program's execution. Then, i should be able to use logical operators to combine Boolean expressions. Lastly, i should able to use the conditional operators for concise and elegant code for decision making.
To complete this lab exercise, i have to know what is switch, logical operators and conditional operators. The question asked me to rewrite the if-else statement using switch statment. As i know, by implementing if-else statement, it tells the computer if the condition is true, then execute the a set of instructions or else execute another set of instructions. for the switch statement, it is made up of several or many set instructions which is only one will be selected by the user and executed by the computer.
This is the question where i have to rewrite the if-else statement using switch statement. As we can see it is more simple. In switch statement , i need to include switch expression where "a" is my switch expression in this question. For the case statement, it represents if-else statement. The statement are executed when the value in the case statement matches the value of the switch expression. Every case statement should have break or continue at the end in order to terminate the remainder of the switch statement. If there is no break, the next case statement will be executed. Switch statement is useful when we have constant variable and only selected case statement be executed. After knowing this switch statement, i able to convert back the statement into if-else statement. I have to know and understand the basic algorithm of switch statement.
Then, the other questions asked me to rewrite the if statements using the conditional operator.
This is one of the example of converting if statements using conditional operator. From 4 lines of statements become one line statement. For conditional operator the statement is " (booleanExpression) ? expression1 : expression2 ". So in the question ages is the booelan expression. We check the boolean expression either the ages is a true condition or false. The expression1 is for true statement while expression2 is for false statement. 3 operands in one statement.
From this basic understanding i can include this logic structure in problem solving which i have to know when to put identify if it suitable or not. So i have to understand clearly.