5. The IF Statement Flashcards

1
Q

control structures: a computer can proceed

A
  • in a sequence
  • selectively (branch) - making a choice
  • repetitively (iteratively) - looping
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

If-Else statement

A

allows you to do diff things depending on a condition

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

condition

A

an expression whose value is true or false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

symbols for conditions

A
>
<
== 
!=
>=
<= 
!> 
etc
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

note on ==

A

cannot use = for conditions, as = is used for assigning

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

combining else and if

A

if ..
else if…
else ..

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

logical (boolean) expressions)

A

data type bool has logical values true and false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

bool, true, false

A

reserved words

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

identifier true

A

has the value 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

identifier false

A

has the value 0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

regional operators

A

allow comparisons
req 2 operands
return 1 if expresion true, 0 otherwise

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

comparing diff data types

A

unpredictable results produced

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

regional operators

A
==
!=
<
<=
>
>=
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

logical (boolean) operators

A

enable you to combine logical expressions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

logical operators list

A

! (not)
&& (and)
|| (or)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

putting !

A

putting ! in front of a logical expression reverses its value

17
Q

eg of using logical operators

A

(x > 0 && x <= 50)

cannot write this:
0 < x <= 50)

18
Q

precedence of operators

A

evaluated left to right

parentheses can override precedence

19
Q

short-circuit evaluation

A

evaluation of logical expression stops when value of expression known

  • if first part of && false, whole lot is false
  • if first part of || true, whole lot is true
20
Q

{ } and if statements

A

only use { } if you need to group together multiple statements within the if statement. Don’t use if there is only 1 statement in a branch

21
Q

IF syntax

A
  • condition must be in ()
  • no ; after the condition
  • can leave out ELSE if not needed
22
Q

the keyword CONST

A

useful for values that are used multiple times in the program

23
Q

using CONST

A
instead of if (temp > 37) ...
use if (temp > NORMAL_TEMP), with NORMAL_TEMP being assigned to be 37 at the start

as it is more readable, otherwise significance of 37 not clear. Also makes it easier to change later on if you wanted to, for example, change it to 38

24
Q

Notes on constants

A

common practice to name constants with all capitals

eg. PI, NORMAL_TEMP, TAXRATE, PRSI

25
Q

Using const example

A

const double NORMAL_TEMP = 37;