Questions about if statements (C ++ vs JavaScript)

Source: Internet
Author: User

I recently switched to C ++ and developed some C ++ projects. I am familiar with the JS language features and have encountered great difficulties in programming, the most confusing issue is the issue of conditional judgment. Here we will compare it with C ++ and Js. First, let's take a look at several tests.

Test 1. c ++ source code:

int test(){printf("test\n");return 1;}int main(){int i=0;    if(i&test())    printf("ok1");    else    printf("ok2");}

Running result:

testok2

Test 2. js source code:

function test(){  console.log("test");  return 1;}var i=0;if(i&&test())  console.log("ok1"); else console.log("ok2");

Running result:

ok2

In principle, although I = 0 is 0 (false) for the if condition, regardless of the result returned by the test function, the result should be "ok2 ", the operating mechanisms of these two languages are completely different. Obviously, C ++ has done a lot of useless work. Next let's take a look at the or operation.

Test 3. c ++ source code:

int test(){printf("test\n");return 0;}int main(){int i=1;    if(i||test())    printf("ok1");    else    printf("ok2");}

Running result:

ok1

Test 4. js source code:

function test(){  console.log("test");  return 0;}var i=1;if(i||test())  console.log("ok1"); else console.log("ok2");

Running result:

ok1

In terms of or operation, the judgment mechanism of C ++ and JS languages is the same. When the first condition is true, the result of the test function of the parallel condition is no longer judged.

I am confused, so I will test C # again.

Test v. C # source code:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace WindowsFormsApplication1{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        string con = "";        bool test()        {            con += "test";            return true;        }        private void button1_Click(object sender, EventArgs e)        {            con = "";            bool i = false;            if (i&&test())                con += "ok1";            else                con += "ok2";            MessageBox.Show(con);        }    }}

Running result:

Note that C # has the same mechanism as Js.

The reason for this comparison is that if two conditions A, B, A, and B are both set up to do one thing, and A is not set up to B, then Javascript is used, the statement can be written as follows:

if(A&&B){}else{}

When a fails, it is natural that B will not be judged again, so the running efficiency will not be affected. However, if C ++ is used for writing, both conditions will be judged, and the efficiency will be affected, therefore, write as follows:

if(A){    if(B){       }}else{}

Write down the confusions encountered during development here. As for the deep-seated problems of these two languages, I cannot make it clear. Make a summary first.

Author:Kunoy Source:Http://blog.csdn.net/kunoy Statement:The authors write blogs to sum up experience and exchange learning.
If you need to reprint the statement, please keep it as much as possible and provide the original article connection clearly on the article page. Thank you!

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.