Tag Archives: variable

Converting variable types in C#

This tutorial explains how to convert variable types in the C# language (eg. convert from string to integer or float, or convert from integer or float to string). Here is a summary of the methods used:

  • float.Parse() – converts to float type
  • int.Parse() – converts to int type
  • .ToString() – converts to string type

Watch the video below and then scroll down to see the sample code.

Example of converting from string to float:

float myFloatVariable = float.Parse(myStringVariable);

Example of converting from string to int:

int myIntVariable = int.Parse(myStringVariable);

Example of converting from int or float to string:

string myStringVariable = myFloatVariable.ToString();

Here is the full sample code from the tutorial video:

using System;

namespace MyCSharpProject
{
  class MainClass
  {
    public static void Main(string[] args)
    {
      Console.WriteLine("Enter your name: ");
      string username = Console.ReadLine(); // store user input as string
      Console.WriteLine("Hello there, " + username);

      Console.WriteLine("Enter two numbers");
      float num1 = float.Parse(Console.ReadLine()); // convert user input from string to float
      float num2 = float.Parse(Console.ReadLine()); // convert user input from string to float
      Console.WriteLine("The result is: " + (num1 + num2)); // display string of text and float result 

      int num3 = 5;
      int num4 = 10;
      Console.WriteLine(num3.ToString() + num4.ToString()); // convert int to string
    }
  }
}


Next tutorial: Making comparisons in C#

Variables and strings in C#

This tutorial explains how to create variables and use strings in the C# programming language. Variables are used to store data that can be used throughout a program.

Variables can store data of different types. One data type is the string. Strings can contain characters such as letters, numbers or symbols. Strings can be a single character, a bunch of mixed characters, words, or sentences.

Other data types include integers (whole numbers eg. 4), floats, (numbers with decimal place eg. 3.56), and Booleans (true or false values). There are other data types that exist but these are the main ones we will start with. In this tutorial we will focus on strings and how they can be used in variables and statements.

When you create a variable you must declare its data type (eg. string), give it a name (eg. age, score, name), and then you can assign it a value. For example,  look at the following statement:

string firstName = "Joe";

The statement above would create a new variable called firstName of the string data type containing the value Joe. Strings are always contained inside single or double quotes.

Watch the video below and then scroll down to see the sample code.

Sample C# code

The code below contains comments to explain each statement.

using System;

namespace MyCSharpApp
{
  class MainClass
  {
    public static void Main (string[] args)
    {
      Console.WriteLine ("What is your name?"); // this displays a message in console
      string message = "Hello there, "; // creates a string variable that contains a message
      string name = Console.ReadLine (); // creates a string that stores the user's name from input
      Console.WriteLine (message + name); // writes the message and name to the console
    }
  }
}

Next tutorial: Integers and floats in C#

Variable scope in PHP (local and global variables)

This tutorial explains variable scope in PHP. The scope of a variable refers to where a variable can be used in a program. A variable’s scope can either be global or local

Global variables are variables that have been declared outside of a function and can be accessed anywhere in the program including inside functions. Local variables are made inside a function and can only be access inside that function they were created in. In other words, the local variable from one function cannot be used in another function or outside the function in the main part of the program. However, local variables can be made global if the programmer needs to use the variable outside the function it was created in.

Watch the video below and then scroll down to see the sample code.

Sample PHP code:

<?php
  $globalVariable = 10;

  function scopeCheck(){
    global $globalVariable;
    // $globalVariable can be accessed by this function because it is now global
    $globalVariable = 20;
    echo $globalVariable;

    $localVariable;
    // $localVariable can only be accessed inside this function because it is local
    // Writing global $localVariable; will make the variable global.
    $localVariable = 50;
  }

  scopeCheck();
  echo $globalVariable; // this will work because variable is global
  echo $localVariable; // this will not work because variable is local
?>

Variable scope

Now that you are using functions in JavaScript, it is really important to understand variable scope. JavaScript has two variable scopes: global and local.

  • A global variable is a variable which is declared outside a function and its value can be accessed and modified throughout your program
  • A local variable is a variable which is declared inside a function definition. It is created and then destroyed every time the function runs, and cannot be accessed or modified from outside the function.

Make sure that you watch the video on variable scope here to see how it works. Sample code is available for you to try out below.

<!DOCTYPE html>
<html lang="en"
<head>
 <meta charset="utf-8"/>
 <title>JavaScript - Variable scope</title>
 <script type="text/javascript">
 
 // A local variable is defined inside a function and can only be used inside a function it's declared in
 // A global variable can be used anywhere in the program
 
 var name = "Smith"; //global variable
 
 function printName(){
 document.write(name + " inside function<br/>");
 // the name variable is being used inside the function
 }
 
 printName(); // this will run the printName function
 
 document.write(name + " outside of function<br/>"); // this uses the name variable outside of the function
 
 function printName2(){
 var name2="Mary"; // name2 is a local variable created inside this function
 document.write(name2 + " inside function<br/>");
 }
 
 printName2(); // this will run the printName2 function
 
 // what will happen when the line of code below runs?
 document.write(name2 + " outside of function<br/>");
 // the statement on the line above will not work because the variable name2 cannot be accessed outside of the function
 // If you go to the JavaScript Console in your browser, you will see an error has been reported when running the code
 // The error will look like: Uncaught ReferenceError: name2 is not defined
 // This error occurs because name2 can only be used by the printName2 function, and not outside the function.
 
 </script>
 
</head>
<body>
 
</body>
</html>

Variable scope in JavaScript

Variable scope refers to the context that a variable is defined in. JavaScript has two variable scopes: global and local.

  • A global variable is a variable which is declared outside a function and its value can be accessed and modified throughout your program
  • A local variable is a variable which is declared inside a function definition. It is created and then destroyed every time the function runs, and cannot be accessed or modified from outside the function.

This video explains variable scope when using functions and how to setup and use local and global variables in your JavaScript programs. You can watch the video below or click here to view it on YouTube. You can also find more information about variable scope in JavaScript here.

The sample code is below shows how to use local and global variables in JavaScript.

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="utf-8"/>
 <title>JavaScript - Variable scope</title>
 <script type="text/javascript">
 
 // A local variable is defined inside a function and can only be used inside a function it's declared in
 // A global variable can be used anywhere in the program
 
 var name = "Smith"; //global variable
 
 function printName(){
 document.write(name + " inside function<br/>");
 // the name variable is being used inside the function
 }
 
 printName(); // this will run the printName function
 
 document.write(name + " outside of function<br/>"); // this uses the name variable outside of the function
 
 function printName2(){
 var name2="Mary"; // name2 is a local variable created inside this function
 document.write(name2 + " inside function<br/>");
 }
 
 printName2(); // this will run the printName2 function
 
 // what will happen when the line of code below runs?
 document.write(name2 + " outside of function<br/>");
 // the statement on the line above will not work because the variable name2 cannot be accessed outside of the function
 // If you go to the JavaScript Console in your browser, you will see an error has been reported when running the code
 // The error will look like: Uncaught ReferenceError: name2 is not defined
 // This error occurs because name2 can only be used by the printName2 function, and not outside the function.
 
 </script>
 
</head>
<body>
 
</body>
</html>

Next tutorial: Arrays

Variables and Data Types in JavaScript

In this second video from the beginners JavaScript tutorial series, you will learn how to create variables and work with strings, integers, and float data types in JavaScript.

Variables are used to store information in program such as text, numbers or true/false values. Variables are given a name and are assigned a value. To create a variable in JavaScript we use the var keyword, then specify the variable name, use the = sign to assign a value, and then specify the value to store (you can also create a variable without immediately assigning it a value). For example: var myNumber = 50;

Variable names must be unique and cannot contain spaces. They don’t begin with a number but can contain numbers. They generally begin with a lowercase letter and if the variable name contains multiple words, then they can be indicated by using camelcase (starting each new word with an uppercase letter) eg. myVariable.

Variables can store data of different types. The main types are:

  • string – text including letters, numbers and other special characters eg. “Hello world”
  • integer – whole number values eg. 5
  • float – numbers with a decimal point eg. 5.3
  • boolean – a true or false value

Watch the video below or click here to view it on YouTube.

The code snippet below shows how to declare variables in JavaScript and how to assign values to a variable. Pay attention to the //comments which explain the different ways of setting up variables.

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="utf-8"/>
 <title>JavaScript - Variables</title>
 <script type="text/javascript">
 
 var myVariable = 50;
 
 // Variable names (identifiers) have to begin with lower or uppercase letters or an underscore
 // Variable names cannot begin with a number, however they can contain a number
 // To declare a variable, use the keyword var
 // You can assign a value to a variable when you declare it, in one line (as above)
 // ...or you can declare a variable and then assign a value later, as below...
 
 var myVariable2;
 myVariable2 = "Hello there";
 
 document.write(myVariable);
 // This displays the value of myVariable in the browser window
 
 </script>
 
</head>
<body>
 
 
</body>
</html>

This source code shows how create and use variables of different data types. Strings are text (letters, numbers and characters), integers are regular whole numbers (treated as numbers), and floats are real numbers (with decimal places eg. 4.5). Boolean is another data type which is used for true/false values. You’ll also see how to use the HTML br tag to add a new line between text that is displayed in the browser.

<!DOCTYPE html>
<html lang="en"
<head>
 <meta charset="utf-8"/>
 <title>JavaScript - Data Types</title>
 <script type="text/javascript">
 //integer
 var age = 20;
 document.write(age);
 
 //float
 var price = 12.95;
 document.write(price);
 
 //string
 var name = "Smith";
 var sentence = "He said \"hello\".";
 
 /*
 Just like in Python, a backslash can be used as an escape character so that
 quotation marks can be used inside a string without any errors
 You can also use single quotes to enclose a string if you want to use double quotes inside a sting
 eg. var sentence = 'He said "hello".';
 */
 
 document.write("</br>", sentence);
 //You can concatenate (join) strings using a comma and you can also use HTML tags inside quotation marks eg. </br>
 //The </br> tag is used to add a new line when the string is displayed in the browser.
 
 </script>
 
</head>
<body>
 
 
</body>
</html>

Hint: Have a look at the toFixed() method if you wish to display floats with a specified number of decimal places.

Next tutorial: Operators