Scilab/C2/Conditional-Branching/English
From Script | Spoken-Tutorial
Revision as of 10:00, 13 June 2014 by PoojaMoolya (Talk | contribs)
Title of script: Conditional Branching
Author: Anuradha Amrutkar
Keywords: if-then-else, select-case
|
|
---|---|
Slide | Welcome to the spoken tutorial on Conditional Branching in Scilab. |
Slide | To practice this tutorial open the scilab console window on your computer |
Slide | We will discuss two types of Conditional constructs in Scilab that is the "if-then-else" construct and the "select-case conditional" construct. |
Slide | The if statement allows us to execute a group of statements if a given condition is satisfied. |
Demonstration | Let me give you an example:
n = 42, if (n == 42) then disp("The number is forty two") end of if construct. |
Narration | Here '=' is the assignment operator, which assigns 42 to the variable n, and '==' is the equality operator, which checks for the equality between the right hand and the left hand side operands. |
Narration | In this case n and 42 and gives the result in Boolean. |
Narration | Here the comma after the first line is optional, Also the then keyword is optional. |
Narration | It can be replaced by a comma or a carriage return. |
Narration | The end keyword ends the "if" construct. |
Narration | On executing the script, we see the output as follows. |
Narration | So far we have seen how to execute a set of statements if a condition is true. |
Narration | Now we will see how to execute another set of statements if that condition is false or we may wish to check if some other condition is satisfied. |
Demonstration | We can do this by using 'else' or 'elseif' keyword respectively. Here is how we do it (the action is shown) |
Demonstration | In this example, 54 is assigned to a variable n and checked for both true condition using 'if' and false condition using 'else' as described: |
Demonstration | I will cut this paste in the scilab console hit enter
n = 54, if (n == 42) then disp("The number is forty two") elseif (n == 54) then, disp("The number is fifty four") else disp("The number is neither forty two nor fifty four") end |
Demonstration | You see the output (output is displayed). |
Demonstration | If you notice, the examples shown above are on multiple lines. |
Narration | They can also be written in a single line with proper semicolons and commas. |
Demonstration | I will cut this and paste in the scilab to execute hit enter
x = 3; y = 5; z = 4; if x>5 then disp(x), elseif x>6 then disp(y), else disp(z), end |
Narration | The select statement allows to combine several branches in a clear and simple way. |
Narration | Depending on the value of a variable, it allows to perform the statement corresponding to the case keyword. |
Narration | There can be as many branches as required. |
Narration | Let us try with an example. |
Demonstration | We will assign 100 to a variable 'n' and check the cases 42, 54 and a default case represented by else cut paste hit enter |
Demonstration | n = 100,
select n case 42 disp("The number is forty two") case 54 disp("The number is fifty four") else disp("The number is neither forty two nor fifty four") end |
Demonstration | Here we see the output . (Shows the output) |
Slide | This brings us to the end of this spoken tutorial on Conditional Branching using Scilab. |
Slide | In this tutorial we have learnt the if - elseif - else statement and the select statement. |
Slide | There are many other functions in Scilab which will be covered in other spoken tutorials. |
Slide | Keep watching the Scilab links. |
Slide | Spoken Tutorials are part of the Talk to a Teacher project, supported by the National Mission on Education through ICT. |
Slide | More information on the same is available at http://spoken-tutorial.org/NMEICT-Intro. |