Swift: Else if

This entry is part 23 of 41 in the series Swift

The if, else if statements in Swift.

func numvalue(num: Int) ->String {
    
    if num > 0 {
        return "positive"
    } else if num == 0 {
        return "zero"
    } else {
        return "negative"
    }
}

print(numvalue(-7))

swift

C++: Combining while loop and if statement

This entry is part 13 of 61 in the series C++

The while loop will check for a condition and will continue to run the loop if the condition is not met.

For certain condition, you might want to perform a specific task.  You can use if statement to check for this condition.

Consider the following example.

#include <iostream>
using namespace std;

void main()
{
	int i = 0;

	cout << "Please enter a value between 0 and 10" << endl;

	cin >> i;

	while ((i != 1) && (i != 10))
	{	

		if (i > 10)
		{
			cout << "You have entered a right value that is larger than 10.";
		}

		cout << "Please enter a value between 0 and 10" << endl;
		cin >> i;

	}

	cout << "You have entered the right value";
}

If 1 or 10 is entered, it will exit the while loop.  If a value larger than 10 is entered, a message will be printed out and it will still continue to run the while loop.

In line 12, we have introduced 2 new operators.  The != which is not equal and the && which is the and operator.

You can refer to C++ operator in this wiki page.

whileif

C++: Flow control with if statement

This entry is part 10 of 61 in the series C++

Like most languages, C++ provides an if statement that supports conditional execution.

We can use an if to check for each condition.  You can actually stack up the many if statements to check for the different conditions.

The example below checks for 2 conditions.

#include <iostream>
#include <string>
using namespace std;

void main()
{
	string singer = "Taylor Swift";
	int count = 6;

	if (singer == "Taylor Swift")
	{
		cout << "I am a fan.";
	}

	if (count == 5)
	{
		cout << "This is her fifth album.";
	}

	cout << endl;

}

For an if statement, if an expression evaluates to true, it will proceed to execute other statements inside the curly braces.

The first expression singer==”Taylor Swift” will evaluate to true and so the line inside the curly braces is printed out.

For the second expression count==5, as we have set count to be 6 earlier, this expression will not evaluate to true.  So it will not execute the cout statement inside the loop.

ifstatement

 

Posted in

Java: If else statement

This entry is part 7 of 54 in the series Learn Java

The if else statement will check if a condition is true.

In the below example, we set a string a to be “Elton”.

Since (a == “Elton”) will evaluate to true, the first println statement will be executed.

package javaapplication6;

public class JavaApplication6 {

    public static void main(String[] args) {
       
        String a = "Elton";
        
        if (a == "Elton")
        {
            System.out.println("Crocodile Rock");
        } else
        {
            System.out.println("Uptown Girl");
        }
    }
}

if

Javascript: If Else

This entry is part 15 of 27 in the series Javascript Tutorial

The If…Else… statement in Javascript is similar to other programming languages such as C.

It will first check if a condition is true and will execute a block of code if the condition is met.

<!DOCTYPE html>
<html>
<head>
 <title>If Else</title>
 <script>
 function myFunction() {
   var passing_score = 60;
   var your_score = 70;
   if (your_score > passing_score) {
      document.getElementById("str1").innerHTML = "You have passed";
      } else {
      document.getElementById("str1").innerHTML = "You failed";
      }
    }
 </script>
</head>
 <body>
  <p id="str1">Passed or failed</p>
  <br>
  <button onclick="myFunction()">Click Me</button>
 </body>
</html>

button

As your_score, which is 70 is greater than passing_score which is 60, the first condition is output in the browser.

In the fiddle, you can try changing the value for passing_score or your_score to see what is output in the browser.