Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Questions and answers begin here Logo Questions and answers begin here Logo
Sign InSign Up

Questions and answers begin here

Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Blog
  • Contact Us

What does null statement mean? (solved)

Home/ Questions/Q 347
Next
In Process
What does null statement mean? (solved)
crpinion
crpinion Teacher

I’m having a bit of trouble understanding how to use a null statement in my programming code. I’ve been working on building a website where the user inputs some data and then the website performs some calculations and spits out an answer. I’m using JavaScript as the language for this particular part of the site, and I’ve seen some code samples online that use the null statement.
Here’s an example of what I’m trying to do:

function calculateAnswer() {
var input1 = parseInt(document.getElementById("input1").value);
var input2 = parseInt(document.getElementById("input2").value);
var answer;

if (input1 < input2) { answer = input2 - input1; } else if (input1 > input2) {
answer = input1 - input2;
}
// what goes here?
document.getElementById("result").innerHTML = answer;
}

In the code above, the if and else if statements work perfectly fine. The problem arises when both input1 and input2 are equal – I want the “answer” variable to be set to null in that case. But I’m not sure how to do that. I’ve seen some people use a single semicolon as the null statement, but I don’t understand why that works. Could someone explain how to use a null statement in JavaScript code, and perhaps give an example of when it might be necessary? Thank you in advance!

coding-conceptscoding-problemsnull-statementprogramming-basicssyntax
  • 529
  • 0 Followers
  • 1
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    msboricua88 Teacher
    2020-11-30T12:42:44+00:00Added an answer about 2 years ago

    Hey there,
    From what I understand, you are curious about what a null statement is and how it can be used in coding. In simple terms, a null statement is a statement that performs no operation, but acts as a placeholder within a program. For example, adding a semicolon to the end of an if statement with no code block following it would produce a null statement.
    Null statements can be used in a variety of situations in programming. One common use is in an empty loop, which might be used to hold a program’s place while waiting for a specific event to occur. Another example is in a switch statement, where a null statement triggers no action to be taken.
    It’s important to note that while null statements might not do anything significant in and of themselves, they can still have an impact on the larger program. For example, a null statement could potentially create an infinite loop if placed within the wrong part of a loop.
    In terms of syntax, a null statement is represented simply by a semicolon, with no other code or statements on that line. While they may seem insignificant, null statements can be a valuable tool for programmers to use in order to achieve the desired functionality in their code.
    I hope this helps to answer your question. If you have any further inquiries or if there is anything more specific I can help with, please let me know!
    Best,
    [Your Name]

    • 120
    • Reply
    • Share
      Share
      • Share onFacebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. ericdavidm Teacher
    2020-11-30T19:40:11+00:00Added an answer about 2 years ago

    A null statement is a statement containing nothing or a statement that does nothing. It is often used as a placeholder, so that an algorithm or program idea can be written without knowing the exact code needed to execute. A null statement can be used in many programming languages, such as C++, Java, and Python.
    For example, imagine you have a for loop that needs to loop through an array, but you want to skip over every other element in the array. You could use a null statement like this:
    “`
    for (int i = 0; i < array.length; i++) { if (i % 2 == 0) { // do something } else { ; // null statement } } ``` In this case, the null statement is used to represent a "do nothing" alternative to the if statement. By including the null statement in the else block, you are telling the programming language to do nothing if the if condition is not met. Using null statements can help make your code more readable and easier to understand, especially when dealing with complex algorithms or processes. It is important to use caution when using null statements, however, as too many can make your code harder to debug and maintain.

    • 41
    • Reply
    • Share
      Share
      • Share onFacebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  3. quirkygameplay Begginer
    2020-11-30T18:21:42+00:00Added an answer about 2 years ago

    A null statement is simply an empty statement that is used when a statement is required syntactically in a program, but no action is necessary. It does not perform any operation and just proceeds to the next statement. In C language, a semicolon (;) is used to represent a null statement.
    For example, consider the following loop that prints the integers from 1 to 5:
    “`
    for(int i=1;i<=5;i++){ printf("%dn",i); } ``` If we want to keep the loop running but we don't want it to do anything at some point, we can use a null statement: ``` for(int i=1;i<=5;i++){ if(i==3){ ; // Null statement } else{ printf("%dn",i); } } ``` In this example, the null statement is used when i is equal to 3, so the program does nothing and moves on to the next iteration of the loop. Null statements can also be used inside if statements, functions, or anywhere a statement is syntactically required. While null statements may seem unnecessary and even confusing, they can be helpful in making code easier to read and understand.

    • 28
    • Reply
    • Share
      Share
      • Share onFacebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  4. elibatista.lima Teacher
    2020-11-30T19:28:51+00:00Added an answer about 2 years ago

    The null statement is a useful component in programming languages that performs no action. It is also known as an empty statement. This statement is mostly used when a program requires no action. For instance, if a user types a semicolon (;) in an empty line when coding, they may have created a null statement. This is because a semicolon is used to indicate the end of an expression in the code. The null statement is not to be confused with a comment that also does nothing, but is used to occupy a line of code.

    Null statements can impact the performance of any given program positively or negatively depending on how they are used. In a situation where loops are needed, null statements can be used to produce an empty version of the loop, which is useful in some situations. Any code that performs no action will execute much faster when compared to styles that require a few more actions.

    In conclusion, null statements can be something that might be overlooked or not thought of as necessary to some people. Still, if it is used efficiently, it can be an excellent program booster.

    • 17
    • Reply
    • Share
      Share
      • Share onFacebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  5. norolahian Begginer
    2020-11-30T22:18:01+00:00Added an answer about 2 years ago

    In programming, a null statement is a statement that has no effect on the program execution. This statement is used to keep track of the program flow and to add a placeholder for future instructions. An example of a null statement is the empty statement, which is written as a semicolon (;). This statement can be used when a loop or an if statement does not need to execute any code after the condition is tested.

    Imagine you are creating an if statement that checks if a user entered a valid input. If the input is invalid, you want to display an error message and prompt the user again until the input is valid. In this case, you can use a null statement to indicate that there is no code to execute when the input is valid. This will keep the program flow intact while waiting for valid input. Without the null statement, the program would terminate when the input is valid, which is not what you want.

    Using null statements is a common practice in programming and helps you to write efficient and maintainable code. By using null statements, you can write code that is self-documenting and easy to understand for future developers who may need to work on your code. So, don’t be afraid to use null statements in your code where appropriate, as it can help to make your code more efficient and robust.

    • 2
    • Reply
    • Share
      Share
      • Share onFacebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.