Explanation of this C++ code I learned in college










-1















I'm a newbie here and I just started college. We are learning C++ and I find it a little bit difficult, because of the way the teachers explain.
Yesterday we did a task that says to create a program, which finds greatest common divisor of 2 numbers. So, the teacher started writing the code, but the explanation wasn't enough for me and I really need some help right now.
(I putted comments on the things I don't understand.)
Here is the code:



#include <iostream>
#include <cmath>

using namespace std;

int main()

int a, b;
cout << "a = ";
cin >> a;
cout << "b = ";
cin >> b;
cout << "GCD (" << a << ", " << b << ") is ";

if (a != 0 && b != 0)
size_t min = abs(a) < abs(b) ? abs(a) : abs(b); //What's that after (?)?
size_t max = abs(a) > abs(b) ? abs(a) : abs(b);
size_t diff = max - min; //What is that variable used for?

while (diff > 0)

min = diff < min ? diff : min;
max = diff > min ? diff : min;
diff = max - min;

cout << min << endl;

else
system("pause");
return 0;



QUESTION: When should I put on if's, while's etc.?










share|improve this question

















  • 5





    "When should I put on if's, while's etc.?" - always.

    – Neil Butterworth
    Nov 14 '18 at 14:12











  • 1) What book are you using to learn C++ from, alongside the lectures? If you aren't using any, consider picking one from this list. 2) What, exactly, you find unclear about the explanations given to you? What were those explanations? If you don't give such information to us, we might just repeat what your lecturer already said, which would help no-one.

    – Algirdas Preidžius
    Nov 14 '18 at 14:13







  • 3





    tbh i think the best you can do is ask your prof or co-students. Working together with others and direct tutoring is extremely important and valuable and cannot be replaced by online Q&As

    – user463035818
    Nov 14 '18 at 14:20






  • 1





    The correct person to ask is your teacher. If they don't realise they are assuming students know certain language constructs, they won't adjust their approach to teaching the course.

    – paddy
    Nov 14 '18 at 14:20






  • 1





    Beware that this code shows a lot of bad habits. My impression is that your teacher may be bad at what they teach...

    – François Andrieux
    Nov 14 '18 at 14:23















-1















I'm a newbie here and I just started college. We are learning C++ and I find it a little bit difficult, because of the way the teachers explain.
Yesterday we did a task that says to create a program, which finds greatest common divisor of 2 numbers. So, the teacher started writing the code, but the explanation wasn't enough for me and I really need some help right now.
(I putted comments on the things I don't understand.)
Here is the code:



#include <iostream>
#include <cmath>

using namespace std;

int main()

int a, b;
cout << "a = ";
cin >> a;
cout << "b = ";
cin >> b;
cout << "GCD (" << a << ", " << b << ") is ";

if (a != 0 && b != 0)
size_t min = abs(a) < abs(b) ? abs(a) : abs(b); //What's that after (?)?
size_t max = abs(a) > abs(b) ? abs(a) : abs(b);
size_t diff = max - min; //What is that variable used for?

while (diff > 0)

min = diff < min ? diff : min;
max = diff > min ? diff : min;
diff = max - min;

cout << min << endl;

else
system("pause");
return 0;



QUESTION: When should I put on if's, while's etc.?










share|improve this question

















  • 5





    "When should I put on if's, while's etc.?" - always.

    – Neil Butterworth
    Nov 14 '18 at 14:12











  • 1) What book are you using to learn C++ from, alongside the lectures? If you aren't using any, consider picking one from this list. 2) What, exactly, you find unclear about the explanations given to you? What were those explanations? If you don't give such information to us, we might just repeat what your lecturer already said, which would help no-one.

    – Algirdas Preidžius
    Nov 14 '18 at 14:13







  • 3





    tbh i think the best you can do is ask your prof or co-students. Working together with others and direct tutoring is extremely important and valuable and cannot be replaced by online Q&As

    – user463035818
    Nov 14 '18 at 14:20






  • 1





    The correct person to ask is your teacher. If they don't realise they are assuming students know certain language constructs, they won't adjust their approach to teaching the course.

    – paddy
    Nov 14 '18 at 14:20






  • 1





    Beware that this code shows a lot of bad habits. My impression is that your teacher may be bad at what they teach...

    – François Andrieux
    Nov 14 '18 at 14:23













-1












-1








-1








I'm a newbie here and I just started college. We are learning C++ and I find it a little bit difficult, because of the way the teachers explain.
Yesterday we did a task that says to create a program, which finds greatest common divisor of 2 numbers. So, the teacher started writing the code, but the explanation wasn't enough for me and I really need some help right now.
(I putted comments on the things I don't understand.)
Here is the code:



#include <iostream>
#include <cmath>

using namespace std;

int main()

int a, b;
cout << "a = ";
cin >> a;
cout << "b = ";
cin >> b;
cout << "GCD (" << a << ", " << b << ") is ";

if (a != 0 && b != 0)
size_t min = abs(a) < abs(b) ? abs(a) : abs(b); //What's that after (?)?
size_t max = abs(a) > abs(b) ? abs(a) : abs(b);
size_t diff = max - min; //What is that variable used for?

while (diff > 0)

min = diff < min ? diff : min;
max = diff > min ? diff : min;
diff = max - min;

cout << min << endl;

else
system("pause");
return 0;



QUESTION: When should I put on if's, while's etc.?










share|improve this question














I'm a newbie here and I just started college. We are learning C++ and I find it a little bit difficult, because of the way the teachers explain.
Yesterday we did a task that says to create a program, which finds greatest common divisor of 2 numbers. So, the teacher started writing the code, but the explanation wasn't enough for me and I really need some help right now.
(I putted comments on the things I don't understand.)
Here is the code:



#include <iostream>
#include <cmath>

using namespace std;

int main()

int a, b;
cout << "a = ";
cin >> a;
cout << "b = ";
cin >> b;
cout << "GCD (" << a << ", " << b << ") is ";

if (a != 0 && b != 0)
size_t min = abs(a) < abs(b) ? abs(a) : abs(b); //What's that after (?)?
size_t max = abs(a) > abs(b) ? abs(a) : abs(b);
size_t diff = max - min; //What is that variable used for?

while (diff > 0)

min = diff < min ? diff : min;
max = diff > min ? diff : min;
diff = max - min;

cout << min << endl;

else
system("pause");
return 0;



QUESTION: When should I put on if's, while's etc.?







c++






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 14:08









Boyan PetrovBoyan Petrov

19




19







  • 5





    "When should I put on if's, while's etc.?" - always.

    – Neil Butterworth
    Nov 14 '18 at 14:12











  • 1) What book are you using to learn C++ from, alongside the lectures? If you aren't using any, consider picking one from this list. 2) What, exactly, you find unclear about the explanations given to you? What were those explanations? If you don't give such information to us, we might just repeat what your lecturer already said, which would help no-one.

    – Algirdas Preidžius
    Nov 14 '18 at 14:13







  • 3





    tbh i think the best you can do is ask your prof or co-students. Working together with others and direct tutoring is extremely important and valuable and cannot be replaced by online Q&As

    – user463035818
    Nov 14 '18 at 14:20






  • 1





    The correct person to ask is your teacher. If they don't realise they are assuming students know certain language constructs, they won't adjust their approach to teaching the course.

    – paddy
    Nov 14 '18 at 14:20






  • 1





    Beware that this code shows a lot of bad habits. My impression is that your teacher may be bad at what they teach...

    – François Andrieux
    Nov 14 '18 at 14:23












  • 5





    "When should I put on if's, while's etc.?" - always.

    – Neil Butterworth
    Nov 14 '18 at 14:12











  • 1) What book are you using to learn C++ from, alongside the lectures? If you aren't using any, consider picking one from this list. 2) What, exactly, you find unclear about the explanations given to you? What were those explanations? If you don't give such information to us, we might just repeat what your lecturer already said, which would help no-one.

    – Algirdas Preidžius
    Nov 14 '18 at 14:13







  • 3





    tbh i think the best you can do is ask your prof or co-students. Working together with others and direct tutoring is extremely important and valuable and cannot be replaced by online Q&As

    – user463035818
    Nov 14 '18 at 14:20






  • 1





    The correct person to ask is your teacher. If they don't realise they are assuming students know certain language constructs, they won't adjust their approach to teaching the course.

    – paddy
    Nov 14 '18 at 14:20






  • 1





    Beware that this code shows a lot of bad habits. My impression is that your teacher may be bad at what they teach...

    – François Andrieux
    Nov 14 '18 at 14:23







5




5





"When should I put on if's, while's etc.?" - always.

– Neil Butterworth
Nov 14 '18 at 14:12





"When should I put on if's, while's etc.?" - always.

– Neil Butterworth
Nov 14 '18 at 14:12













1) What book are you using to learn C++ from, alongside the lectures? If you aren't using any, consider picking one from this list. 2) What, exactly, you find unclear about the explanations given to you? What were those explanations? If you don't give such information to us, we might just repeat what your lecturer already said, which would help no-one.

– Algirdas Preidžius
Nov 14 '18 at 14:13






1) What book are you using to learn C++ from, alongside the lectures? If you aren't using any, consider picking one from this list. 2) What, exactly, you find unclear about the explanations given to you? What were those explanations? If you don't give such information to us, we might just repeat what your lecturer already said, which would help no-one.

– Algirdas Preidžius
Nov 14 '18 at 14:13





3




3





tbh i think the best you can do is ask your prof or co-students. Working together with others and direct tutoring is extremely important and valuable and cannot be replaced by online Q&As

– user463035818
Nov 14 '18 at 14:20





tbh i think the best you can do is ask your prof or co-students. Working together with others and direct tutoring is extremely important and valuable and cannot be replaced by online Q&As

– user463035818
Nov 14 '18 at 14:20




1




1





The correct person to ask is your teacher. If they don't realise they are assuming students know certain language constructs, they won't adjust their approach to teaching the course.

– paddy
Nov 14 '18 at 14:20





The correct person to ask is your teacher. If they don't realise they are assuming students know certain language constructs, they won't adjust their approach to teaching the course.

– paddy
Nov 14 '18 at 14:20




1




1





Beware that this code shows a lot of bad habits. My impression is that your teacher may be bad at what they teach...

– François Andrieux
Nov 14 '18 at 14:23





Beware that this code shows a lot of bad habits. My impression is that your teacher may be bad at what they teach...

– François Andrieux
Nov 14 '18 at 14:23












6 Answers
6






active

oldest

votes


















1














It is necessary when you need more that one line/statement to be executed by the if/else/while. Valid examples:



if (a != 0 || b != 0)
cout << (a>b ? a : b) << endl;

if (a != 0 || b != 0) cout << (a>b ? a : b) << endl;

if (a != 0 || b != 0)
cout << (a>b ? a : b) << endl;
a++;


If you did:



if (a != 0 || b != 0) 
cout << (a>b ? a : b) << endl;
a++;


The the a++; would be executed regardless of the if condition.



Some programmers like to use even for single statements because they believe it leads to more usable and maintainable code. I do not belong to that group but I can see the arguments on either side.






share|improve this answer






























    1














    size_t min = abs(a) < abs(b) ? abs(a) : abs(b); //What's that after (?)?


    C and C++ have a construct that is similar to an if-else statement. This line basically says that if abs(a) is smaller than abs(b), then min should take the value of abs(a); otherwise, it should take the value of abs(b).



    size_t diff = max - min; //What is that variable used for?


    It's not clear what you mean here. If you mean diff, the code essentially uses it in the subsequent while loop to perform division by repeated subtraction. This is a very strange thing to do, especially because it is so inefficient, and division would have been more compact and efficient in the loop. It's even stranger given that earlier the author uses ?: (which you asked about); that construction is used mainly because it's more compact and efficient than an if-else statement, but this is rather strange code, anyway.




    When should I put on if's, while's etc.?




    You should do it by default. You don't have to do it if only one statement is to be performed when the condition is true (resp. false) but people usually do as a matter of good style and to assist readability. For instance, this code



    if (a != 0 || b != 0)
    cout << (a>b ? a : b) << endl;
    else
    cout << "not possible!!!n";


    could just as easily be



    if (a != 0 || b != 0) 
    cout << (a>b ? a : b) << endl;
    else
    cout << "not possible!!!n";



    ...and a lot of instructors would actually require the latter from learners.






    share|improve this answer






























      1














      This is the syntax for an if-statement



      if ( condition ) statement-true else statement-false


      statement-true is either one statement or a block of statements in ...



      So you can use if without ... if there is only one line. But it is better to always use ....






      share|improve this answer




















      • 2





        "is either one statement or a block of statements in ..." Technically speaking, it is always a single statement. It's just ... is a compound statement.

        – Algirdas Preidžius
        Nov 14 '18 at 14:17


















      0














      In addition to the other answers:




      //What's that after (?)?



      foo ? bar : qux;


      Is the use of the ternary operator.



      If foo is true the expression evaluates to bar else it evaluates to qux.






      share|improve this answer






























        0














        Just adding my two cents...



        This



        if (a != 0 || b != 0)
        cout << (a>b ? a : b) << endl;


        is equivalent to



        if (a != 0 || b != 0) 
        cout << (a>b ? a : b) << endl;



        The big difference comes when you realize that code is not something static that you write once and then never change again. Lets change the example a little bit (intentially weird intendation)



        if ( x ) // (I)
        y = a;
        z = b;


        is not the same as



        if ( x ) // (II)
        y = a;
        z = b;



        Using brackets allows you to focus on only the part you care about. Consider that you later decide to swap the two lines, then



        if ( x ) 
        z = b;
        y = a;



        is still ok (its the same as (II), apart from swapping the two instructions), while



        if ( x )
        z = b;
        y = a;


        is doing something completely different as the version above (I). If you use the brackets you dont need to care whether those two lines are inside a if block. To decide if you can swap them you need to look at nothing more than those two lines. This is not the case if you do not use the brackets. This may seem like a minor thing, though I have seen countless bugs caused by not putting brackets where there could be some.






        share|improve this answer






























          0














          For 1 line of code following if, else, else if, while, etc



          if (<some condition>)
          //1 line of code`


          and



          if (<some condition>)

          //1 line of code



          ...are equivalent and it is up to you (personal style,developer choice,readability etc.) to make that decision.



          For > 1 line of code following if, else, else if, while, etc
          is required if you want code completely scoped to the condition statement. It is up to you the developer to make sure to scope these lines of code (i.e. the compiler will not warn you about this.. it will not know if the intent was 1 line or multiple lines).



          So an example



          if(<some condition>)

          //line of code 1
          //line of code 2



          the compiler will let you do this...



          if(<some condition>)
          // line of code 1
          // line of code 2


          but //line of code 2 has no relation to the if condition since it was not scoped with and will be executed regardless of the if statement condition.






          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302129%2fexplanation-of-this-c-code-i-learned-in-college%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            6 Answers
            6






            active

            oldest

            votes








            6 Answers
            6






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            It is necessary when you need more that one line/statement to be executed by the if/else/while. Valid examples:



            if (a != 0 || b != 0)
            cout << (a>b ? a : b) << endl;

            if (a != 0 || b != 0) cout << (a>b ? a : b) << endl;

            if (a != 0 || b != 0)
            cout << (a>b ? a : b) << endl;
            a++;


            If you did:



            if (a != 0 || b != 0) 
            cout << (a>b ? a : b) << endl;
            a++;


            The the a++; would be executed regardless of the if condition.



            Some programmers like to use even for single statements because they believe it leads to more usable and maintainable code. I do not belong to that group but I can see the arguments on either side.






            share|improve this answer



























              1














              It is necessary when you need more that one line/statement to be executed by the if/else/while. Valid examples:



              if (a != 0 || b != 0)
              cout << (a>b ? a : b) << endl;

              if (a != 0 || b != 0) cout << (a>b ? a : b) << endl;

              if (a != 0 || b != 0)
              cout << (a>b ? a : b) << endl;
              a++;


              If you did:



              if (a != 0 || b != 0) 
              cout << (a>b ? a : b) << endl;
              a++;


              The the a++; would be executed regardless of the if condition.



              Some programmers like to use even for single statements because they believe it leads to more usable and maintainable code. I do not belong to that group but I can see the arguments on either side.






              share|improve this answer

























                1












                1








                1







                It is necessary when you need more that one line/statement to be executed by the if/else/while. Valid examples:



                if (a != 0 || b != 0)
                cout << (a>b ? a : b) << endl;

                if (a != 0 || b != 0) cout << (a>b ? a : b) << endl;

                if (a != 0 || b != 0)
                cout << (a>b ? a : b) << endl;
                a++;


                If you did:



                if (a != 0 || b != 0) 
                cout << (a>b ? a : b) << endl;
                a++;


                The the a++; would be executed regardless of the if condition.



                Some programmers like to use even for single statements because they believe it leads to more usable and maintainable code. I do not belong to that group but I can see the arguments on either side.






                share|improve this answer













                It is necessary when you need more that one line/statement to be executed by the if/else/while. Valid examples:



                if (a != 0 || b != 0)
                cout << (a>b ? a : b) << endl;

                if (a != 0 || b != 0) cout << (a>b ? a : b) << endl;

                if (a != 0 || b != 0)
                cout << (a>b ? a : b) << endl;
                a++;


                If you did:



                if (a != 0 || b != 0) 
                cout << (a>b ? a : b) << endl;
                a++;


                The the a++; would be executed regardless of the if condition.



                Some programmers like to use even for single statements because they believe it leads to more usable and maintainable code. I do not belong to that group but I can see the arguments on either side.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 14 '18 at 14:15









                SunKnight0SunKnight0

                2,468167




                2,468167























                    1














                    size_t min = abs(a) < abs(b) ? abs(a) : abs(b); //What's that after (?)?


                    C and C++ have a construct that is similar to an if-else statement. This line basically says that if abs(a) is smaller than abs(b), then min should take the value of abs(a); otherwise, it should take the value of abs(b).



                    size_t diff = max - min; //What is that variable used for?


                    It's not clear what you mean here. If you mean diff, the code essentially uses it in the subsequent while loop to perform division by repeated subtraction. This is a very strange thing to do, especially because it is so inefficient, and division would have been more compact and efficient in the loop. It's even stranger given that earlier the author uses ?: (which you asked about); that construction is used mainly because it's more compact and efficient than an if-else statement, but this is rather strange code, anyway.




                    When should I put on if's, while's etc.?




                    You should do it by default. You don't have to do it if only one statement is to be performed when the condition is true (resp. false) but people usually do as a matter of good style and to assist readability. For instance, this code



                    if (a != 0 || b != 0)
                    cout << (a>b ? a : b) << endl;
                    else
                    cout << "not possible!!!n";


                    could just as easily be



                    if (a != 0 || b != 0) 
                    cout << (a>b ? a : b) << endl;
                    else
                    cout << "not possible!!!n";



                    ...and a lot of instructors would actually require the latter from learners.






                    share|improve this answer



























                      1














                      size_t min = abs(a) < abs(b) ? abs(a) : abs(b); //What's that after (?)?


                      C and C++ have a construct that is similar to an if-else statement. This line basically says that if abs(a) is smaller than abs(b), then min should take the value of abs(a); otherwise, it should take the value of abs(b).



                      size_t diff = max - min; //What is that variable used for?


                      It's not clear what you mean here. If you mean diff, the code essentially uses it in the subsequent while loop to perform division by repeated subtraction. This is a very strange thing to do, especially because it is so inefficient, and division would have been more compact and efficient in the loop. It's even stranger given that earlier the author uses ?: (which you asked about); that construction is used mainly because it's more compact and efficient than an if-else statement, but this is rather strange code, anyway.




                      When should I put on if's, while's etc.?




                      You should do it by default. You don't have to do it if only one statement is to be performed when the condition is true (resp. false) but people usually do as a matter of good style and to assist readability. For instance, this code



                      if (a != 0 || b != 0)
                      cout << (a>b ? a : b) << endl;
                      else
                      cout << "not possible!!!n";


                      could just as easily be



                      if (a != 0 || b != 0) 
                      cout << (a>b ? a : b) << endl;
                      else
                      cout << "not possible!!!n";



                      ...and a lot of instructors would actually require the latter from learners.






                      share|improve this answer

























                        1












                        1








                        1







                        size_t min = abs(a) < abs(b) ? abs(a) : abs(b); //What's that after (?)?


                        C and C++ have a construct that is similar to an if-else statement. This line basically says that if abs(a) is smaller than abs(b), then min should take the value of abs(a); otherwise, it should take the value of abs(b).



                        size_t diff = max - min; //What is that variable used for?


                        It's not clear what you mean here. If you mean diff, the code essentially uses it in the subsequent while loop to perform division by repeated subtraction. This is a very strange thing to do, especially because it is so inefficient, and division would have been more compact and efficient in the loop. It's even stranger given that earlier the author uses ?: (which you asked about); that construction is used mainly because it's more compact and efficient than an if-else statement, but this is rather strange code, anyway.




                        When should I put on if's, while's etc.?




                        You should do it by default. You don't have to do it if only one statement is to be performed when the condition is true (resp. false) but people usually do as a matter of good style and to assist readability. For instance, this code



                        if (a != 0 || b != 0)
                        cout << (a>b ? a : b) << endl;
                        else
                        cout << "not possible!!!n";


                        could just as easily be



                        if (a != 0 || b != 0) 
                        cout << (a>b ? a : b) << endl;
                        else
                        cout << "not possible!!!n";



                        ...and a lot of instructors would actually require the latter from learners.






                        share|improve this answer













                        size_t min = abs(a) < abs(b) ? abs(a) : abs(b); //What's that after (?)?


                        C and C++ have a construct that is similar to an if-else statement. This line basically says that if abs(a) is smaller than abs(b), then min should take the value of abs(a); otherwise, it should take the value of abs(b).



                        size_t diff = max - min; //What is that variable used for?


                        It's not clear what you mean here. If you mean diff, the code essentially uses it in the subsequent while loop to perform division by repeated subtraction. This is a very strange thing to do, especially because it is so inefficient, and division would have been more compact and efficient in the loop. It's even stranger given that earlier the author uses ?: (which you asked about); that construction is used mainly because it's more compact and efficient than an if-else statement, but this is rather strange code, anyway.




                        When should I put on if's, while's etc.?




                        You should do it by default. You don't have to do it if only one statement is to be performed when the condition is true (resp. false) but people usually do as a matter of good style and to assist readability. For instance, this code



                        if (a != 0 || b != 0)
                        cout << (a>b ? a : b) << endl;
                        else
                        cout << "not possible!!!n";


                        could just as easily be



                        if (a != 0 || b != 0) 
                        cout << (a>b ? a : b) << endl;
                        else
                        cout << "not possible!!!n";



                        ...and a lot of instructors would actually require the latter from learners.







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Nov 14 '18 at 14:17









                        John PerryJohn Perry

                        1,401919




                        1,401919





















                            1














                            This is the syntax for an if-statement



                            if ( condition ) statement-true else statement-false


                            statement-true is either one statement or a block of statements in ...



                            So you can use if without ... if there is only one line. But it is better to always use ....






                            share|improve this answer




















                            • 2





                              "is either one statement or a block of statements in ..." Technically speaking, it is always a single statement. It's just ... is a compound statement.

                              – Algirdas Preidžius
                              Nov 14 '18 at 14:17















                            1














                            This is the syntax for an if-statement



                            if ( condition ) statement-true else statement-false


                            statement-true is either one statement or a block of statements in ...



                            So you can use if without ... if there is only one line. But it is better to always use ....






                            share|improve this answer




















                            • 2





                              "is either one statement or a block of statements in ..." Technically speaking, it is always a single statement. It's just ... is a compound statement.

                              – Algirdas Preidžius
                              Nov 14 '18 at 14:17













                            1












                            1








                            1







                            This is the syntax for an if-statement



                            if ( condition ) statement-true else statement-false


                            statement-true is either one statement or a block of statements in ...



                            So you can use if without ... if there is only one line. But it is better to always use ....






                            share|improve this answer















                            This is the syntax for an if-statement



                            if ( condition ) statement-true else statement-false


                            statement-true is either one statement or a block of statements in ...



                            So you can use if without ... if there is only one line. But it is better to always use ....







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 14 '18 at 16:02

























                            answered Nov 14 '18 at 14:15









                            Thomas SablikThomas Sablik

                            2,89911430




                            2,89911430







                            • 2





                              "is either one statement or a block of statements in ..." Technically speaking, it is always a single statement. It's just ... is a compound statement.

                              – Algirdas Preidžius
                              Nov 14 '18 at 14:17












                            • 2





                              "is either one statement or a block of statements in ..." Technically speaking, it is always a single statement. It's just ... is a compound statement.

                              – Algirdas Preidžius
                              Nov 14 '18 at 14:17







                            2




                            2





                            "is either one statement or a block of statements in ..." Technically speaking, it is always a single statement. It's just ... is a compound statement.

                            – Algirdas Preidžius
                            Nov 14 '18 at 14:17





                            "is either one statement or a block of statements in ..." Technically speaking, it is always a single statement. It's just ... is a compound statement.

                            – Algirdas Preidžius
                            Nov 14 '18 at 14:17











                            0














                            In addition to the other answers:




                            //What's that after (?)?



                            foo ? bar : qux;


                            Is the use of the ternary operator.



                            If foo is true the expression evaluates to bar else it evaluates to qux.






                            share|improve this answer



























                              0














                              In addition to the other answers:




                              //What's that after (?)?



                              foo ? bar : qux;


                              Is the use of the ternary operator.



                              If foo is true the expression evaluates to bar else it evaluates to qux.






                              share|improve this answer

























                                0












                                0








                                0







                                In addition to the other answers:




                                //What's that after (?)?



                                foo ? bar : qux;


                                Is the use of the ternary operator.



                                If foo is true the expression evaluates to bar else it evaluates to qux.






                                share|improve this answer













                                In addition to the other answers:




                                //What's that after (?)?



                                foo ? bar : qux;


                                Is the use of the ternary operator.



                                If foo is true the expression evaluates to bar else it evaluates to qux.







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Nov 14 '18 at 14:19









                                SwordfishSwordfish

                                1




                                1





















                                    0














                                    Just adding my two cents...



                                    This



                                    if (a != 0 || b != 0)
                                    cout << (a>b ? a : b) << endl;


                                    is equivalent to



                                    if (a != 0 || b != 0) 
                                    cout << (a>b ? a : b) << endl;



                                    The big difference comes when you realize that code is not something static that you write once and then never change again. Lets change the example a little bit (intentially weird intendation)



                                    if ( x ) // (I)
                                    y = a;
                                    z = b;


                                    is not the same as



                                    if ( x ) // (II)
                                    y = a;
                                    z = b;



                                    Using brackets allows you to focus on only the part you care about. Consider that you later decide to swap the two lines, then



                                    if ( x ) 
                                    z = b;
                                    y = a;



                                    is still ok (its the same as (II), apart from swapping the two instructions), while



                                    if ( x )
                                    z = b;
                                    y = a;


                                    is doing something completely different as the version above (I). If you use the brackets you dont need to care whether those two lines are inside a if block. To decide if you can swap them you need to look at nothing more than those two lines. This is not the case if you do not use the brackets. This may seem like a minor thing, though I have seen countless bugs caused by not putting brackets where there could be some.






                                    share|improve this answer



























                                      0














                                      Just adding my two cents...



                                      This



                                      if (a != 0 || b != 0)
                                      cout << (a>b ? a : b) << endl;


                                      is equivalent to



                                      if (a != 0 || b != 0) 
                                      cout << (a>b ? a : b) << endl;



                                      The big difference comes when you realize that code is not something static that you write once and then never change again. Lets change the example a little bit (intentially weird intendation)



                                      if ( x ) // (I)
                                      y = a;
                                      z = b;


                                      is not the same as



                                      if ( x ) // (II)
                                      y = a;
                                      z = b;



                                      Using brackets allows you to focus on only the part you care about. Consider that you later decide to swap the two lines, then



                                      if ( x ) 
                                      z = b;
                                      y = a;



                                      is still ok (its the same as (II), apart from swapping the two instructions), while



                                      if ( x )
                                      z = b;
                                      y = a;


                                      is doing something completely different as the version above (I). If you use the brackets you dont need to care whether those two lines are inside a if block. To decide if you can swap them you need to look at nothing more than those two lines. This is not the case if you do not use the brackets. This may seem like a minor thing, though I have seen countless bugs caused by not putting brackets where there could be some.






                                      share|improve this answer

























                                        0












                                        0








                                        0







                                        Just adding my two cents...



                                        This



                                        if (a != 0 || b != 0)
                                        cout << (a>b ? a : b) << endl;


                                        is equivalent to



                                        if (a != 0 || b != 0) 
                                        cout << (a>b ? a : b) << endl;



                                        The big difference comes when you realize that code is not something static that you write once and then never change again. Lets change the example a little bit (intentially weird intendation)



                                        if ( x ) // (I)
                                        y = a;
                                        z = b;


                                        is not the same as



                                        if ( x ) // (II)
                                        y = a;
                                        z = b;



                                        Using brackets allows you to focus on only the part you care about. Consider that you later decide to swap the two lines, then



                                        if ( x ) 
                                        z = b;
                                        y = a;



                                        is still ok (its the same as (II), apart from swapping the two instructions), while



                                        if ( x )
                                        z = b;
                                        y = a;


                                        is doing something completely different as the version above (I). If you use the brackets you dont need to care whether those two lines are inside a if block. To decide if you can swap them you need to look at nothing more than those two lines. This is not the case if you do not use the brackets. This may seem like a minor thing, though I have seen countless bugs caused by not putting brackets where there could be some.






                                        share|improve this answer













                                        Just adding my two cents...



                                        This



                                        if (a != 0 || b != 0)
                                        cout << (a>b ? a : b) << endl;


                                        is equivalent to



                                        if (a != 0 || b != 0) 
                                        cout << (a>b ? a : b) << endl;



                                        The big difference comes when you realize that code is not something static that you write once and then never change again. Lets change the example a little bit (intentially weird intendation)



                                        if ( x ) // (I)
                                        y = a;
                                        z = b;


                                        is not the same as



                                        if ( x ) // (II)
                                        y = a;
                                        z = b;



                                        Using brackets allows you to focus on only the part you care about. Consider that you later decide to swap the two lines, then



                                        if ( x ) 
                                        z = b;
                                        y = a;



                                        is still ok (its the same as (II), apart from swapping the two instructions), while



                                        if ( x )
                                        z = b;
                                        y = a;


                                        is doing something completely different as the version above (I). If you use the brackets you dont need to care whether those two lines are inside a if block. To decide if you can swap them you need to look at nothing more than those two lines. This is not the case if you do not use the brackets. This may seem like a minor thing, though I have seen countless bugs caused by not putting brackets where there could be some.







                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Nov 14 '18 at 14:33









                                        user463035818user463035818

                                        17.9k42868




                                        17.9k42868





















                                            0














                                            For 1 line of code following if, else, else if, while, etc



                                            if (<some condition>)
                                            //1 line of code`


                                            and



                                            if (<some condition>)

                                            //1 line of code



                                            ...are equivalent and it is up to you (personal style,developer choice,readability etc.) to make that decision.



                                            For > 1 line of code following if, else, else if, while, etc
                                            is required if you want code completely scoped to the condition statement. It is up to you the developer to make sure to scope these lines of code (i.e. the compiler will not warn you about this.. it will not know if the intent was 1 line or multiple lines).



                                            So an example



                                            if(<some condition>)

                                            //line of code 1
                                            //line of code 2



                                            the compiler will let you do this...



                                            if(<some condition>)
                                            // line of code 1
                                            // line of code 2


                                            but //line of code 2 has no relation to the if condition since it was not scoped with and will be executed regardless of the if statement condition.






                                            share|improve this answer





























                                              0














                                              For 1 line of code following if, else, else if, while, etc



                                              if (<some condition>)
                                              //1 line of code`


                                              and



                                              if (<some condition>)

                                              //1 line of code



                                              ...are equivalent and it is up to you (personal style,developer choice,readability etc.) to make that decision.



                                              For > 1 line of code following if, else, else if, while, etc
                                              is required if you want code completely scoped to the condition statement. It is up to you the developer to make sure to scope these lines of code (i.e. the compiler will not warn you about this.. it will not know if the intent was 1 line or multiple lines).



                                              So an example



                                              if(<some condition>)

                                              //line of code 1
                                              //line of code 2



                                              the compiler will let you do this...



                                              if(<some condition>)
                                              // line of code 1
                                              // line of code 2


                                              but //line of code 2 has no relation to the if condition since it was not scoped with and will be executed regardless of the if statement condition.






                                              share|improve this answer



























                                                0












                                                0








                                                0







                                                For 1 line of code following if, else, else if, while, etc



                                                if (<some condition>)
                                                //1 line of code`


                                                and



                                                if (<some condition>)

                                                //1 line of code



                                                ...are equivalent and it is up to you (personal style,developer choice,readability etc.) to make that decision.



                                                For > 1 line of code following if, else, else if, while, etc
                                                is required if you want code completely scoped to the condition statement. It is up to you the developer to make sure to scope these lines of code (i.e. the compiler will not warn you about this.. it will not know if the intent was 1 line or multiple lines).



                                                So an example



                                                if(<some condition>)

                                                //line of code 1
                                                //line of code 2



                                                the compiler will let you do this...



                                                if(<some condition>)
                                                // line of code 1
                                                // line of code 2


                                                but //line of code 2 has no relation to the if condition since it was not scoped with and will be executed regardless of the if statement condition.






                                                share|improve this answer















                                                For 1 line of code following if, else, else if, while, etc



                                                if (<some condition>)
                                                //1 line of code`


                                                and



                                                if (<some condition>)

                                                //1 line of code



                                                ...are equivalent and it is up to you (personal style,developer choice,readability etc.) to make that decision.



                                                For > 1 line of code following if, else, else if, while, etc
                                                is required if you want code completely scoped to the condition statement. It is up to you the developer to make sure to scope these lines of code (i.e. the compiler will not warn you about this.. it will not know if the intent was 1 line or multiple lines).



                                                So an example



                                                if(<some condition>)

                                                //line of code 1
                                                //line of code 2



                                                the compiler will let you do this...



                                                if(<some condition>)
                                                // line of code 1
                                                // line of code 2


                                                but //line of code 2 has no relation to the if condition since it was not scoped with and will be executed regardless of the if statement condition.







                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited Jan 2 at 19:52

























                                                answered Nov 14 '18 at 17:23









                                                static_caststatic_cast

                                                1,07411421




                                                1,07411421



























                                                    draft saved

                                                    draft discarded
















































                                                    Thanks for contributing an answer to Stack Overflow!


                                                    • Please be sure to answer the question. Provide details and share your research!

                                                    But avoid


                                                    • Asking for help, clarification, or responding to other answers.

                                                    • Making statements based on opinion; back them up with references or personal experience.

                                                    To learn more, see our tips on writing great answers.




                                                    draft saved


                                                    draft discarded














                                                    StackExchange.ready(
                                                    function ()
                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302129%2fexplanation-of-this-c-code-i-learned-in-college%23new-answer', 'question_page');

                                                    );

                                                    Post as a guest















                                                    Required, but never shown





















































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown

































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown







                                                    Popular posts from this blog

                                                    Use pre created SQLite database for Android project in kotlin

                                                    Darth Vader #20

                                                    Ondo