Learning C-Style C++


by Direct Approach Mentoring



if else

8. Control Structures


Sometimes you'll only want to perform an action in certain situations. For example, let's print
"That's my lucky number!"
only if

number

is

17

:

if

( number ==

17

)
{
  printf(
"That's my lucky number!"
);
}


if

is always followed by an expression in parentheses. An expression is any chunk of code that has a value.
Examples:

5

has the value 5. It is called a literal, because it is literally 5. It doesn't stand for something else.
number has the value that was last stored in it. It could be a lot of things, maybe

17

.

3

+

4

has the value

7

.
number +

10

has the value of whatever number is, plus

10

.

These four examples are are all expressions.


(More coming soon!)