Swift: Optional variable

This entry is part 35 of 41 in the series Swift

For Swift, when you declare a variable, you need to provide an initial value.  If the value is unknown, you can declare it as an optional.

var name:String?

//name = "abcde"

if name != nil {
    print("Your name: \(name!)")
    
} else {
    print("There is no value in the Name")
    
}

swift

Swift: Variables

This entry is part 1 of 41 in the series Swift

Apple introduced a new and aptly named Editor called Playground and this editor allows us to look at the output almost immediately on the right-hand side of the editor.

We will use the Playground for this series of tutorial.  This is a quick run through of Swift code and syntax.

Defining variables in Swift:

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

import UIKit

var str = "Hello, playground"
print(str)

var int = 10
print(int)

var dec = 10.511
print(dec)

swift1

Java: integer variables

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

In a programming language, the usage of a variable is very similar to variable in Mathematics.  We can assign variable with a particular value.

The example below will make use of 3 variables to to perform a simple mathematical function.

package javaapplication4;

public class JavaApplication4 {

    public static void main(String[] args) {
        
        int a = 10;
        int b = 15;
        int total;
        total = a + b;
        System.out.println(total);
    }
}

In this program, we first create integers a and b.  We then sum the variables and assign the sum to the variable total.

integer