Skip to content

Really quick lesson

Kam (カマリ) edited this page Jan 28, 2019 · 1 revision

Literal Questions

- char vs. character literal
- string vs. string literal
- char/character literal vs. string/string literal

Literals

The literals were trying to differentiate between assignment and variable.

Give an example on strings (v. char).

C++ has this thing called constants. Basically, they are variables in a program that cannot change. At all.

These constants are set using literals. It's called a literal because it's a literal representation of whatever that type is.

A good example is a string literal. We know that strings come like such "" right? So if we set a constant with a string value (ex: const char[3] MY_NAME = "KAM") instead of a normal variable (ex: char[3] MY_NAME = "KAM"), we have used a string literal ("KAM").

This works for all other data types, too. So a character literal would look exactly how you think it would. :)

As for "char/character literal vs. string/string literal"? If you understand the stuff above, you know that this comes down to char v. string. :)

So... how do these constants get set? Can I see a literal?

Yup. Surely. Best example I have is this.

Imagine that we're writing a program that uses the number pi (aka 3.14159...). We know that pi never changes. So we would define a constant using literal.

#include <iostream>
using namespace std;

#define PI 3.14159

int main() {
    cout << "The value of pi is:" << PI << endl;
    return 0;
}

another way we could do this is:

#include <iostream>
using namespace std;

int main() {
    const double PI = 3.14159;

    cout << "The value of pi is:" << PI << endl;
    return 0;
}

You can also set a constant using the const keyword. The difference is, you can use const inside of main(), where you have to use #define outside of it.

Still have some questions? More detail here.

Initialize Questions

- Why initialize variables if they are input values?

Good question. The quick answer is that C++ doesn't always set a value to 0. It sets it to some arbitrary value. Stack overflow answer here. This is a really good example.

Update example code.

Type Questions

- Type Conversions
I was playing around with adding and dividing int and double values, trying to determine when there's a mix of data types, which type gets superiority/changes the outcome.

There's no hard and fast rule for this (that I know of). It's one of those things you have to play around with an figure out (as I don't even remember it). So lets do it.

We know that:

  • integer divided by integer equals integer.
  • double divided by double equals double.

We need to figure out:

  • integer divided by double equals _____.
  • double divided by integer equals _____.

Syntax Questions

- i++/i--

I'm sure you're asking "What the fuck does this say/mean/do????". Understandable. Basically, this is "syntaxic shorthand". Example:

i++ is the same as i = i + 1.

But what the fuck does i = i + 1 do?!?

Yeah. Kind of confusing. Let me explain. First, when looking a line of code know this rule:

When there is an equal sign, the right side gets handled before the left.

Knowing that rule, imagine this situation. We have a program where i is equal to 2. So:

int main() {
    int i = 2;
    return 0;
}

We then do i = i + 1.

int main() {
    int i = 2;
    
    i = i + 1;
    
    return 0;
}

This means that the right side, i + 1, is equal to 2 + 1. Imagine the statement is now i = 2 + 1.

Meaning now, that i = 3.

That's exactly how that works.

Back to my point, though.

So, if i++ is the same as i = i + 1, that means, its going to increase i by 1. Yup. Now that you know how i++ works, can you guess how i-- works?

Lesson on if-else

If there's time, anything on today's lecture material (if-else statements)

An if statement has 2 parts:

  • Boolean expression
  • Logic

Since we know that booleans can only be true or false, boolean expressions can only evaluate to one of those two choices as well.

More info here.