Explanation of this C++ code I learned in college
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++
|
show 5 more comments
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++
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
|
show 5 more comments
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++
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++
c++
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
|
show 5 more comments
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
|
show 5 more comments
6 Answers
6
active
oldest
votes
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.
add a comment |
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.
add a comment |
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 ...
.
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
add a comment |
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
.
add a comment |
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.
add a comment |
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.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 14 '18 at 14:15
SunKnight0SunKnight0
2,468167
2,468167
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 14 '18 at 14:17
John PerryJohn Perry
1,401919
1,401919
add a comment |
add a comment |
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 ...
.
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
add a comment |
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 ...
.
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
add a comment |
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 ...
.
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 ...
.
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
add a comment |
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
add a comment |
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
.
add a comment |
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
.
add a comment |
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
.
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
.
answered Nov 14 '18 at 14:19
SwordfishSwordfish
1
1
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 14 '18 at 14:33
user463035818user463035818
17.9k42868
17.9k42868
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Jan 2 at 19:52
answered Nov 14 '18 at 17:23
static_caststatic_cast
1,07411421
1,07411421
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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