top of page

If / Else

If

The if statement is a codeblock which will run if the criteria is met. if statements use the following syntax:
if (criteria){ execute; }

Example:

Output:

ifelse2.png
ifelse1.png

If the input meets the criteria of being greater than 10 a message is prompted. If the criteria is not met the if statement is overlooked and the program continues to end.

This can be accomplished using a one-liner statement if the space constraint is necessary, however keep in mind not to overdo it and keep the code relatively readable.

Example:

Output:

ifelse4.png
ifelse3.png

Else

To use if statements in conjunction we will first define an if statement then create else if statements for each scenario we would like to account for.

Example:

Output:

ifelse6.png
ifelse5.png

We create three scenarios with if and else if statements. This gives us the value of input i relative to the number 10.

Note that if an if statement is found to be true this does not mean that the program will stop there and skip to the end, instead it will continue to check every else if statement.

Example:

Output:

ifelse8.png
ifelse7.png

Although i is greater than 10 the program continues to check the next statement which also proves valid, so both outputs are presented.

At the end of all of our else if statements we can create a blanket else statement without any if. This will execute when none of the previous if/else if statements are found to be valid.

Example:

Output:

ifelse10.png
ifelse9.png

If the condition of input i being greater than 10 is not met, the else statement will be executed and present or message.

  • Twitter
  • LinkedIn
  • discord-logo--v2
  • kisspng-github-pages-logo-repository-fork-github-logo-1-magentys-5b69de71b51265

Dragon Eye Intelligence LLC

bottom of page