Why conditional statements
Let us hypothetically consider two statements, statement A and statement B. Observe the truth table for the statements:. He claimed that they are divisible by 9. Do you agree or disagree? Justify your answer. Conditional statement : If a number is a multiple of 3, then it is divisible by 9. Write the converse, inverse, and contrapositive statement for the following conditional statement. The inverse statement is, "If you do not study well then you will not pass the exam" if not p, then not q.
The contrapositive statement is, "If you did not pass the exam, then you did not study well" if not q, then not p. Here are a few activities for you to practice. The mini-lesson targeted the fascinating concept of the conditional statement. The math journey around conditional statements started with what a student already knew and went on to creatively crafting a fresh concept in the young minds.
Done in a way that not only it is relatable and easy to grasp, but also will stay with them forever. At Cuemath , our team of math experts is dedicated to making learning fun for our favorite readers, the students! Through an interactive and engaging learning-teaching-learning approach, the teachers explore all angles of a topic. Conditional statements are used to justify the given condition or two statements as true or false. If is used when a specified condition is true. If your condition is false, the code next to then will be ignored and the code next to if will run.
Think of it as saying " If this condition is true then do this, or else do that". Here is what the conditional looks like filled in. If this condition is true the string does not equal a blank string it means the user has entered text, so the app will give the user an answer. If the condition is false the string is equal to a blank string , then all the code next to then is ignored, and skips to else , which tells the user to "Ask me a question first!
There are a few different ways to write this conditional statement. All of them work, there is no correct answer. There are four more examples below. See if you can understand them. Condition: the length of the string in the textbox is not equal to zero.
Condition: the textbox is not empty. Condition: the length of the string in the textbox is equal to zero. Condition: the textbox is empty. See if you can remember how to create your conditional statement. This activity builds on the activity from Coding 5: Variables. Try to start the count over as soon as the user reaches Where do you think you should put your conditional statement?
Sometimes it tells the user that they won, and sometimes it just keeps counting past This form divides the conditional statement with an ellipsis following the comparison operator.
This means that both the initial value and comparison operator are provided on the first line, with each case providing a second value for comparison. A limited number of comparison operators can be used with this form. If expression comparisonOperator This example shows how a multi-case if statement can be used to execute actions based on information stored in a variable. In this form, the first line of the if statement only provides an initial value.
Subsequently, each case begins with an operator to finish the conditional, as well as any statements to be executed in that case. This makes it easy to conduct multiple different tests on a single value. If value This example shows how a multi-case if statement can be used with a variety of different operators in the same statement to conduct different tests on the same value. In form 2, cases can individually access properties to evaluate using the apostrophe-s 's and dot.
This example accesses the name and age properties of an individual stored in the person variable using the apostrophe-s 's and dot. For more information, see Keep Checking Cases. In this form, the conditional is divided immediately after the if , so that the entire conditional and code to be executed are provided by each case. This provides the greatest flexibility of the three forms, because each case is fully customizable. A single statement can be provided on the same line as the case, or a number of statements can be provided on subsequent lines as a statement list.
This example randomly generates values and then generates different output based on those values using the multi-case if statement form 3. You can copy and paste this example into a script and run it yourself. The typical execution flow of a multi-case if statement is that each case is checked in turn to see if they evaluate as true the conditions provided are met. After a case is encountered that is found to be true, the code for that case is executed and then control jumps to the next statement in the script following the end if at the end of the multi-case if statement.
When using a multi-case if statement, the flow of execution can be modified within cases, altering the outcome of the script significantly. Case statements complete the conditional of a multi-case if statement, and outline the actions to be taken when that case is evaluated to be true.
The above program checks if a number is less or greater than 10 and prints the result using nested if-else construct. NOTE: In nested if-else, we have to be careful with the indentation because multiple if-else constructs are involved in this process, so it becomes difficult to figure out individual constructs. Proper indentation makes it easy to read the program. This type of structure is known as the else-if ladder.
This chain generally looks like a ladder hence it is also called as an else-if ladder. The test-expressions are evaluated from top to bottom. Whenever a true test-expression if found, statement associated with it is executed. When all the n test-expressions becomes false, then the default else statement is executed.
The above program prints the grade as per the marks scored in a test. We have used the else-if ladder construct in the above program. Skip to content.