I.T. Discussion Community!
-Collapse +Expand
C#
Search C# Group:

Advanced
-Collapse +Expand C# To/From
To/FromCODEGuides
-Collapse +Expand C# Study Test
PRESTWOODCERTIFIED
-Collapse +Expand C# Store
PRESTWOODSTORE
   ► KBTo/From GuidesReferenceLanguage Basics  Print This    All Groups  

Literals (Cross Ref > Language Basics)

Literals

General Info: Programming Literals

A value directly written into the source code of a computer program (as opposed to an identifier like a variable or constant). Literals cannot be changed. Common types of literals include string literals, floating point literals, integer literals, and hexidemal literals. Literal strings are usually either quoted (") or use an apostrophe (') which is often referred to as a single quote. Sometimes quotes are inaccurately referred to as double quotes.

Languages Focus

In addition to understanding whether to use a quote or apostrophe for string literals, you also want to know how to specify and work with other types of literals including floating point literals. Some compilers allow leading and trailing decimals (.1 + .1), while some require a leading or trailing 0 as in (0.1 + 0.1). Also, because floating point literals are difficult for compilers to represent accurately, you need to understand how the compiler handles them and how to use rounding and trimming commands correctly for the nature of the project your are coding.

Access VBA:   quote

String literals are quoted as in "Prestwood". If you need to embed a quote use two quotes in a row.

To specify a floating point literal between 1 and -1, you can preceed the decimal with a 0 or not (both work). In other words, preceding and following decimals are allowed (both .1 and 0.1). Trailing decimals are optimized out and replaced with # if only integer values are used.

Syntax Example:
MsgBox ("Hello")
MsgBox ("Hello ""Mike"".")
 
'Does Access VBA evaluate this simple
'floating point math correctly? No! 
If (.1 + .1 + .1) = 0.3 Then
MsgBox "Correct"
Else
MsgBox "Not correct"
End If

More Info

Definition:  Programming Literals

ASP Classic:   quote

String literals are quoted as in "Prestwood". If you need to embed a quote use two quotes in a row.

To specify a floating point literal between 1 and -1, you can preceed the decimal with a 0 or not (both work). In other words, preceding and following decimals are allowed (both .1 and 0.1). Trailing decimals are also allowed (1, 1., and 1.0 are all equivalent and allowed).

Syntax Example:
Response.Write "Hello"
Response.Write "Hello ""Mike""."
  
'Does ASP evaluate this simple
'floating point math correctly? No! 
If (.1 + .1 + .1) = .3 Then
 Response.Write "Correct"
Else
 Response.Write "Not correct"
End If

More Info

Definition:  Programming Literals

C#:   quote

String literals are quoted as in "Prestwood". If you need to embed a quote use a slash in front of the quote as in \".

To specify a floating point literal between 1 and -1, you can preceed the decimal with a 0 or not (both work). In other words, preceding decimals are allowed (both .1 and 0.1). Trailing decimals are not allowed.

Syntax Example:
Console.WriteLine("Hello");
Console.WriteLine("Hello \"Mike\".");
 
//Does C# evaluate this simple
//floating point math correctly? No! 
if ((0.1 + 0.1 + 0.1) == 0.3)
{
MessageBox.Show("Correct");
}
else
{
MessageBox.Show("Not correct");
}

More Info

Definition:  Programming Literals

C++:   quote

String literals are quoted as in "Prestwood". If you need to embed a quote use a slash in front of the quote as in \".

Syntax Example:
printf("Hello\n");
printf("Hello \"Mike\".\n");
 
cout << "Hello" << endl;
cout << "Hello \"Mike\".\n";

More Info

Definition:  Programming Literals

C++/CLI:   qoute

Same as standard C++. String literals are quoted as in "Prestwood". If you need to embed a quote use a slash in front of the quote as in \".

To specify a floating point literal between 1 and -1, you can preceed the decimal with a 0 or not (both work). In other words, preceding and following decimals are allowed (both .1 and 0.1). Trailing decimals are also allowed (1, 1., and 1.0 are all equivalent and allowed).

Syntax Example:
MessageBox::Show("Hello");
MessageBox::Show("Hello \"Mike\".");
  
//Does ASP evaluate this simple
//floating point math correctly? No! 
if ((.1 + .1 + .1) == 0.3) {
MessageBox::Show("Correct");
} else {
MessageBox::Show("Not correct");
}

More Info

Definition:  Programming Literals

Corel Paradox:   quote

String literals are quoted as in "Prestwood". If you need to embed a quote use a slash in front of the quote as in \".

In ObjectPAL, string literals are limited to 255 characters but there's nothing preventing you from using multiple string literals together as in:

msgInfo("", "You can " + " add literals together in ObjectPAL") 

To specify a floating point literal between 1 and -1, you can preceed the decimal with a 0 or not (both work). In other words, preceding and following decimals are allowed (both .1 and 0.1). Trailing decimals are also allowed (1, 1., and 1.0 are all equivalent and allowed).

Syntax Example:
msgInfo("", "Hello")
msgInfo("", "Hello \"Mike\".")
  
;Does ObjectPAL evaluate this simple
;floating point math correctly? No!
If (.1 + .1 + .1) = .3 Then
 msgInfo("", "Correct")
Else
 msgInfo("", "Not correct")
EndIf


Linked Certification Question(s)

The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.

Beginner

1 Beginner Level Question

Question #1: Multiple Choice

Which code statement correctly embeds a quote within a literal string?

Answer:
1. 
msgInfo("", "Hello ""Mike"".")
2. 
msgInfo("", "Hello \qMike\q.")
3. 
msgInfo("", 'Hello "Mike".')
4. 
msgInfo("", "Hello \"Mike\".")

More Info

Definition:  Programming Literals
 

Delphi:   apostrophe

String literals are single quoted (the apostrophe) as in 'Prestwood'. If you need to embed an apostrophe use two apostrophes in a row.

Floating point literals must start with an integer. For example, to specify a fractional floating point literal between 1 and -1, you preceed the decimal with a 0; otherwise, you will get a compiler error.

//x := .1 + .1;   //Does not work.
x := 0.1 + 0.1;   //Does work.
Syntax Example:
ShowMessage('Hello');
ShowMessage('Hello Mike''s website.');


Linked Certification Question(s)

The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.

Beginner

2 Beginner Level Questions

Question #1: Multiple Choice

Which code line correctly embeds an apostrophe?

Answer:
1. 
ShowMessage('Hello Mike\'s website.');
2. 
ShowMessage('Hello Mike''s website.');
3. 
ShowMessage("Hello Mike's website.");
4. 
ShowMessage('Hello Mike#'s website.');
5. 
ShowMessage('Hello Mike'''s website.');
Question #2: Yes or No?

If x is defined as a variable of type double, will the following compile?

x := .1 + .1;
Answer:
  • Yes
  • No
  • More Info

    Definition:  Programming Literals
     

    Delphi Prism:   quote or apostrophe

    In Prism, you use either quotes or apostrophes for string literals.

    Different than Delphi, you can start floating point literals with a decimal or an integer. For example, to specify a fractional floating point literal between 1 and -1, you can preceed the decimal with a 0 or not.

    x := .1 + .1;     //Does work.
    x := 0.1 + 0.1;   //Does work.
    Syntax Example:
    MessageBox.Show('Hello');
    MessageBox.Show("Hello");

    //Example of embedding quotes and apostropes:
    MessageBox.Show('He said, "Who''s computer?"');
    MessageBox.Show("She said, ""Mike's computer"".");




    Linked Certification Question(s)

    The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.

    Beginner

    1 Beginner Level Question

    Question #1: True or False?

    In Prism, you can use either apostrphes or quotes for string literals and you can embed an apostrophe and quote in either as follows:

    MessageBox.Show('He said, "Who''s computer?"');
    MessageBox.Show("She said, ""Mike's"".");
    Answer:
  • True
  • False
  • More Info

    Definition:  Programming Literals
     

    Java:   quote

    String literals are quoted as in "Prestwood". If you need to embed a quote use a slash in front of the quote as in \".

    To specify a floating point literal between 1 and -1, you can preceed the decimal with a 0 or not (both work). In other words, preceding and following decimals are allowed (both 1. and 1.0 work as well as .1 and 0.1). In general, Java follows the IEEE 754 Binary Floating-Point Arithmetic standard.

    Syntax Example:

    System.out.println("Hello");
    System.out.println("Hello \"Mike\".");
      
    //Does Java evaluate this simple
    //floating point math correctly? No!
    if ((.1 + .1 + .1) == 0.3) {
    System.out.println("Correct");
    } else {
    System.out.println("Not correct");
    }

    More Info

    Definition:  Programming Literals

    JavaScript:   quote or apostrophe

    String literals use either an apostrophe (also known as a single quote) as in 'Prestwood' or quoted as in "Prestwood". If you need to embed an apostrophe in an apostrophe-literal or a quote in a quoted-literal, precede it with a slash as in \' and \".

    Syntax Example:
    Alert("Hello");
    Alert("Hello \"Mike\".")
      
    Alert('Hello');
    Alert('Hello Mike\'s website.')
     
    //Does JavaScript evaluate this simple
    //floating point math correctly? No! 
    if ((.1 + .1 + .1) == .3) {
     document.write("Correct")
    } else {
     document.write("Not correct")
    }

    Working Example

    <html>
    <body>
     
    <h1>JavaScript Literals</h1>
    <script language=JavaScript>
    <!--
    //Preceding decimals.

    if ((.1 + .1 + .1) == .3) {
     document.write("Correct")
    } else {
     document.write("Not correct")
    }
     
    //Trailing decimals.
    if ((1.0 + 1. + 1) == 3.0) {
     document.write("Correct")
    } else {
     document.write("Not correct")
    }
    //-->
    </script>
     
    </body>
    </html>

    More Info

    Code:  JavaScript Literals (quote or apostrophe)
    Definition:  Programming Literals

    Perl:   quote

    String literals are quoted as in "Prestwood". If you need to embed a quote use a slash in front of the quote as in \".

    To specify a floating point literal between 1 and -1, you can preceed the decimal with a 0 or not (both work). In other words, preceding and following decimals are allowed (both .1 and 0.1). Trailing decimals are also allowed (1, 1., and 1.0 are all equivalent and allowed).

    Syntax Example:
    print "Hello";
    print "Hello \"Mike\".";
      
    #Does Perl evaluate this simple
    #floating point math correctly? No! 
    if ((.1 + .1 + .1) == .3) {
      print("Correct");
    } else {
      print("Not correct");
    }

    Working Example

    #!/usr/local/bin/perl -w
    print("Content-type: text/html\n\n");
    #Start of HTML page.
    print("<html>");
    print("<head><title>Perl Literals</title></head>");
    print("<body>");

    if ((.1 + .1 + .1) == .3) {
     print("Correct");
    } else {
      print("Not correct");
    }

    #Trailing decimals.
    if ((1.0 + 1. + 1) == 3) {
     print("Correct");
    } else {
      print("Not correct");
    }
    print("</body></html>");

    More Info

    Code:  Perl Literals (quote)
    Definition:  Programming Literals

    PHP:   quote or apostrophe

    In PHP you can use quotes, or apostrophes as in "Prestwood", and 'Prestwood' for string literals. Use a slash in front of a quote or apostrophe to embed same type as in \' and \".

    To specify a floating point literal between 1 and -1, you can preceed the decimal with a 0 or not (both work). In other words, preceding and following decimals are allowed (both .1 and 0.1). Trailing decimals are also allowed (1, 1., and 1.0 are all equivalent and allowed).

    Syntax Example:
    echo "Mike's drums are over there.<br>";
    echo 'Mike said, "hi!"<br>';
      
    //Does PHP evaluate this simple
    //floating point math correctly? No! 
    If ((.1 + .1 + .1) == .3) {
     Echo "Correct";
    } Else {
     Echo "Not correct";
    }

    Working Example

    <html>
    <head><title>PHP Literals</title></head>
    <body>
    <?PHP
    //
    //Quotes
    //
    echo "Mike's drums are over there.<br>";
    echo 'Mike said, "hi!"<br>';
      
    //Variable within a literal.
    echo "Your name is $fullname.<br>";
    echo "You are $Age and weigh $Weight.<br>";
      
    //$ within a literal ok too.
    echo "That will be $1.52.<br>";
    If ((.1 + .1 + .1) == .3) {
     Echo "Correct";
    } Else {
     Echo "Not correct";
    }
    ?>
    </body>
    </html>

    More Info

    Code:  PHP Literals (quote or apostrophe)
    Definition:  Programming Literals

    VB Classic:   quote

    String literals are quoted as in "Prestwood". If you need to embed a quote use two quotes in a row.

    To specify a floating point literal between 1 and -1, you can preceed the decimal with a 0 or not (both work). In other words, preceding and following decimals are allowed (both .1 and 0.1). Trailing decimals are optimized out and replaced with # if only integer values are used.

    Syntax Example:
    MsgBox ("Hello")
    MsgBox ("Hello ""Mike"".")
      
    'Does VB evaluate this simple
    'floating point math correctly? No! 
    If (.1 + .1 + .1) = 0.3 Then
    MsgBox "Correct"
    Else
    MsgBox "Not correct"
    End If

    More Info

    Definition:  Programming Literals

    VB.Net:   quote

    String literals are quoted as in "Prestwood". If you need to embed a quote use two quotes in a row.

    To specify a floating point literal between 1 and -1, preceed the decimal with a 0. If you don't, the compiler will auto-correct your code and place a leading 0. It will change .1 to 0.1 automatically. Trailing decimals are not allowed.

    Syntax Example:
    Console.WriteLine("Hello")
    Console.WriteLine("Hello ""Mike"".")
      
    'Does VB.Net evaluate this simple
    'floating point math correctly? No! 
    If (.1 + .1 + .1) = 0.3 Then
    MsgBox "Correct"
    Else
    MsgBox "Not correct"
    End If




    Linked Certification Question(s)

    The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.

    Beginner

    1 Beginner Level Question

    Question #1: Multiple Choice

    Which of the following code statements correctly embeds a quoted string within a quoted string?

    Answer:
    1. 
    MessageBox.Show("Hi \"Brian\".")
    2. 
    MessageBox.Show("Hi ""Brian"".")
    3. 
    MessageBox.Show('Hi "Brian".')
    4. 
    MessageBox.Show("Hi \qBrian\q.")
    5. 

    All of the above.

    More Info

    Definition:  Programming Literals
     





    Follow PrestwoodBoards on: 


    ©1995-2020 PrestwoodBoards  [Security & Privacy]
    Professional IT Services: Coding | Websites | Computer Tech