Swift: If else statement

This entry is part 3 of 41 in the series Swift

The simple if else  statement in Swift.

//: Playground - noun: a place where people can play

import UIKit


var number = 8

if number > 10
{
    print("More than 10")
    
} else {
    print("Less than 10")
    
}

swift1

C++: The if-else statement

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

The if…else statement allows you to specify an action to perform when the condition is true and a different action to perform when the condition is false.

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

void main()
{
	int grade = 70;

	if (grade >= 60)
		cout << "Passed";
	else
		cout << "Failed";

	cout << endl;
}

In the above code line 9 will evaluate to true and so the string Passed will be printed out.

ifelse

 

 

 

C#: Multiple if else in OOP style

If there are multiple selections, you can also use if with a series of else statements.

If Else
using System;

namespace Conditional
{
    class Program
    {
        private int number;

        private Program(int number1)
        {
            number = number1;
        }

        private void checkNumber()
        {
            if (number == 5)
            {
                Console.WriteLine("The number is 5");
            }
            else if (number == 10)
            {
                Console.WriteLine("The number is 10");
            }
            else
            {
                Console.WriteLine("The number is not found");
            }
        }

        static void Main()
        {
            int x = 10;

            Program check = new Program(x);
            check.checkNumber();
            Console.ReadLine();
        }
    }
}

In this case, the else statements are immediately followed by an if without using braces.

C#: If Else

This entry is part 40 of 71 in the series C#

The if else statement is a conditional branch statement.

If you are new to programming, this will be one of the first things that you should learn.

We can illustrate it by using the below statements:

if (expression)
  {
    statement 1;
  }
  else
  {
    statement 2;
  }

The expression is actually a Boolean expression.  It should resolve to either true or false.

If the expression resolves to true, statement 1 will be run.  If the expression resolves to false, statement 2 will be run.

Since the expression is a Boolean expression, let’s use the example below where it will check if a Boolean variable is set to true or false.

using System;

namespace Conditional
{
    class Program
    {
        static void Main()
        {
            bool test = false;

            if (test)
            {
                Console.WriteLine("The test boolean variable is set to true.");
            }
            else
            {
                Console.WriteLine("The test boolean variable is set to false.");
            }
            Console.ReadLine();
        }
    }
}

In line 11, we have if (test).

This is actually similar to if (test == true) as shown in the example below, with the minor modification.

If Else
using System;

namespace Conditional
{
    class Program
    {
        static void Main()
        {
            bool test = false;

            if (test == true)
            {
                Console.WriteLine("The test boolean variable is set to true.");
            }
            else
            {
                Console.WriteLine("The test boolean variable is set to false.");
            }
            Console.ReadLine();
        }
    }
}

 

 

PHP: If else

This entry is part 25 of 54 in the series PHP Tutorial

Often you’d want to execute a statement if a certain condition is met, and a different statement if the condition is not met.

This is what else is for. else extends an if statement to execute a statement in case the expression in the if statement evaluates to FALSE.

For example, the following code would display ‘a is greater than b’ if $a is greater than $b, and ‘a is NOT greater than b’ otherwise:

<?php

$a = 10;
$b = 20;

if ($a > $b) {
  echo "a is greater than b";
} else {
  echo "a is NOT greater than b";
}

?>