# Difference between revisions of "KonsolScript:Tutorials:Conditional"

(→Closing Thoughts: lw) |
(s/code/syntaxhighlight/ for all code fragments) |
||

Line 8: | Line 8: | ||

With these conditional operators, we can form a question in KonsolScript that means is ''1 equal to 2''? | With these conditional operators, we can form a question in KonsolScript that means is ''1 equal to 2''? | ||

− | < | + | <syntaxhighlight type="cpp"> |

1 EQ 2 | 1 EQ 2 | ||

− | </ | + | </syntaxhighlight> |

=== If statement === | === If statement === | ||

The '''if''' statement is the "''then do this''" of conditionals. | The '''if''' statement is the "''then do this''" of conditionals. | ||

− | < | + | <syntaxhighlight type="cpp"> |

//demonstrating if statement | //demonstrating if statement | ||

function main() { | function main() { | ||

Line 22: | Line 22: | ||

} | } | ||

//Outputs (nothing)... | //Outputs (nothing)... | ||

− | </ | + | </syntaxhighlight> |

The sample above won't output anything since '''1''' is ''not'' equal to '''2'''. | The sample above won't output anything since '''1''' is ''not'' equal to '''2'''. | ||

=== Else statement === | === Else statement === | ||

The '''else''' statement is the "''then do that''" of conditionals. | The '''else''' statement is the "''then do that''" of conditionals. | ||

− | < | + | <syntaxhighlight type="cpp"> |

//demonstrating else statement | //demonstrating else statement | ||

function main() { | function main() { | ||

Line 38: | Line 38: | ||

//Outputs... | //Outputs... | ||

//1 is NOT equal to 2 | //1 is NOT equal to 2 | ||

− | </ | + | </syntaxhighlight> |

The sample above would output "''1 is NOT equal to 2''" since '''1''', ''surely'', is not equal to '''2'''. | The sample above would output "''1 is NOT equal to 2''" since '''1''', ''surely'', is not equal to '''2'''. | ||

=== Else-If statement === | === Else-If statement === | ||

The '''else if''' statement is where we try to make another questioning. | The '''else if''' statement is where we try to make another questioning. | ||

− | < | + | <syntaxhighlight type="cpp"> |

//demonstrating else-if statement | //demonstrating else-if statement | ||

function main() { | function main() { | ||

Line 56: | Line 56: | ||

//Outputs... | //Outputs... | ||

//1 is less than 2 | //1 is less than 2 | ||

− | </ | + | </syntaxhighlight> |

The above sample will print "''1 is less than 2''" and not "''1 is NOT equal to 2''" because the second question was answered '''yes''' so the ''no'' part will not be executed. | The above sample will print "''1 is less than 2''" and not "''1 is NOT equal to 2''" because the second question was answered '''yes''' so the ''no'' part will not be executed. | ||

== Last Words == | == Last Words == | ||

The '''else''' statement can never be written before '''if''' statement. The samples below will give you error. | The '''else''' statement can never be written before '''if''' statement. The samples below will give you error. | ||

− | < | + | <syntaxhighlight type="cpp"> |

//demonstrating wrong usage of if-else statement | //demonstrating wrong usage of if-else statement | ||

else { | else { | ||

Line 68: | Line 68: | ||

Konsol:Log("1 is equal to 2") | Konsol:Log("1 is equal to 2") | ||

} | } | ||

− | </ | + | </syntaxhighlight> |

− | < | + | <syntaxhighlight type="cpp"> |

//demonstrating wrong usage of if-else statement | //demonstrating wrong usage of if-else statement | ||

else if (1 LT 2) { | else if (1 LT 2) { | ||

Line 77: | Line 77: | ||

Konsol:Log("1 is equal to 2") | Konsol:Log("1 is equal to 2") | ||

} | } | ||

− | </ | + | </syntaxhighlight> |

Also, if you're more used to using the conditional operators of C/C++, you can use the '''==''' (''same with'' '''EQ'''), '''!=''' (''same with'' '''NE'''), '''>''' (''same with'' '''GT'''), '''>=''' (''same with'' '''GE'''), '''<''' (''same with'' '''LT'''), and '''<=''' (''same with'' '''LE'''). See the sample below. | Also, if you're more used to using the conditional operators of C/C++, you can use the '''==''' (''same with'' '''EQ'''), '''!=''' (''same with'' '''NE'''), '''>''' (''same with'' '''GT'''), '''>=''' (''same with'' '''GE'''), '''<''' (''same with'' '''LT'''), and '''<=''' (''same with'' '''LE'''). See the sample below. | ||

− | < | + | <syntaxhighlight type="cpp"> |

//demonstrating else-if statement | //demonstrating else-if statement | ||

function main() { | function main() { | ||

Line 93: | Line 93: | ||

//Outputs... | //Outputs... | ||

//1 is less than 2 | //1 is less than 2 | ||

− | </ | + | </syntaxhighlight> |

~creek23~ | ~creek23~ | ||

[[Category:KonsolScript]] [[Category:Tutorial]] | [[Category:KonsolScript]] [[Category:Tutorial]] |

## Latest revision as of 18:22, 3 July 2014

## Conditional

In programming, you can achieve artificial intelligence with the help of Conditionals. We provide a question answerable by *yes* or *no* which will be acted-upon by *then do this* or *then do that*.

### Conditional Operators

In KonsolScript, we have **EQ** (*EQual to*), **NE** (*Not Equal to*), **GT** (*Greater Than*), **GE** (*Greater than or Equal to*), **LT** (*Less Than*), and **LE** (*Less than or Equal to*).

With these conditional operators, we can form a question in KonsolScript that means is *1 equal to 2*?

1 EQ 2

### If statement

The **if** statement is the "*then do this*" of conditionals.

//demonstrating if statement function main() { if (1 EQ 2) { Konsol:Log("1 is equal to 2") } } //Outputs (nothing)...

The sample above won't output anything since **1** is *not* equal to **2**.

### Else statement

The **else** statement is the "*then do that*" of conditionals.

//demonstrating else statement function main() { if (1 EQ 2) { Konsol:Log("1 is equal to 2") } else { Konsol:Log("1 is NOT equal to 2") } } //Outputs... //1 is NOT equal to 2

The sample above would output "*1 is NOT equal to 2*" since **1**, *surely*, is not equal to **2**.

### Else-If statement

The **else if** statement is where we try to make another questioning.

//demonstrating else-if statement function main() { if (1 EQ 2) { Konsol:Log("1 is equal to 2") } else if (1 LT 2) { Konsol:Log("1 is less than 2") } else { Konsol:Log("1 is NOT equal to 2") } } //Outputs... //1 is less than 2

The above sample will print "*1 is less than 2*" and not "*1 is NOT equal to 2*" because the second question was answered **yes** so the *no* part will not be executed.

## Last Words

The **else** statement can never be written before **if** statement. The samples below will give you error.

//demonstrating wrong usage of if-else statement else { Konsol:Log("1 is NOT equal to 2") } if (1 EQ 2) { Konsol:Log("1 is equal to 2") }

//demonstrating wrong usage of if-else statement else if (1 LT 2) { Konsol:Log("1 is less than 2") } if (1 EQ 2) { Konsol:Log("1 is equal to 2") }

Also, if you're more used to using the conditional operators of C/C++, you can use the **==** (*same with* **EQ**), **!=** (*same with* **NE**), **>** (*same with* **GT**), **>=** (*same with* **GE**), **<** (*same with* **LT**), and **<=** (*same with* **LE**). See the sample below.

//demonstrating else-if statement function main() { if (1 == 2) { Konsol:Log("1 is equal to 2") } else if (1 < 2) { Konsol:Log("1 is less than 2") } else { Konsol:Log("1 is NOT equal to 2") } } //Outputs... //1 is less than 2

~creek23~