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: [cpp] int test () {printf ("test \ n"); return 1 ;}int main () {int I = 0; if (I & test () printf ("ok1"); else printf ("ok2");} running result: [cpp] view plaincopytest ok2 test II. js source code: [javascript] function test () {console. log ("test"); return 1 ;}var I = 0; if (I & test () console. log ("ok1"); else console. log ("ok2"); running result: [javascript] ok2 in principle, although I = 0 for the if condition, regardless of the result returned by the test function, it is 0 (false) after the operation, and the result should be "ok2", and the two languages operate completely on this mechanism. Obviously, c ++ has done a lot of useless work. Next let's take a look at the or operation. Test 3. c ++ source code: [cpp] int test () {printf ("test \ n"); return 0 ;}int main () {int I = 1; if (I | test () printf ("ok1"); else printf ("ok2");} running result: [cpp] ok1 test 4. js source code: [javascript] function test () {console. log ("test"); return 0;} var I = 1; if (I | test () console. log ("ok1"); else console. log ("ok2"); running result: [javascript] ok1 has the same or operation mechanism as c ++ and js. When the first condition is true, the result of the test function is no longer judged. I am confused, so I will test c # again. Test v. c # source code: [csharp] 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_C Lick (object sender, EventArgs e) {con = ""; bool I = false; if (I & test () con ++ = "ok1 "; else con + = "ok2"; MessageBox. show (con) ;}} running result: 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 in this way: [javascript] if (A & B) {} else {} When A is not successful, B is naturally not judged, so the running efficiency is not affected, however, if c ++ is used for writing, both conditions will be judged, and the efficiency will be affected. Therefore, you have to write: [cpp] if (A) {if (B) {} else {} Here is a summary of the confusions encountered during development. As for the deep-seated problems of the two languages, I cannot clearly explain them.

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.