Week 3 Flashcards
(59 cards)
Describe a conditional statement with a “plan B”.
if (true_or_false_condition)
perform_if_condition_true;
else
perform_if_condition_false;
Code a conditional statement to the following scenario: If the weather is good, go to the park. If the weather is bad, stay at home.
if (weather_is_good) GoToPark();
else
StayAtHome();
For an if-else statement if you want to give more than one instruction how would you code that?
- group instructions in a block with {}
if (condition) {instruction1();
instruction2();}
else {instruction1();
instruction2();}
Condition statements can be …, which means more than one condition can be included in a statement. The else statement will refer to the … if statement. … improves readability and emphasizes the nesting of inner conditional statements.
nested, closest former, indentation
Code the following:
If the weather is fine, we’ll go for a walk. If we find a nice restaurant, we’ll have lunch there. Otherwise, we’ll eat a sandwich. If the weather is poor, we’ll go to the theater. If there are no tickets, we’ll go shopping in the nearest mall.
if (Weather_good) if (Nice_restaurant_found) Have_lunch(); else Eat_sandwhich(); else if(Tickets_available) Go_to_cinema(); else Go_shopping();
What is a cascade?
Nested conditional statements that have several alternatives if a condition is not met.
if (condition) instruction(); else if (nested condition 1) instruction(); else if (nested condition 2) instruction(); else instruction();
Most computers store ints using … bits (… bytes).
32, 4
What issues arise when using ints? (3)
- we might not need such a large value using 32 bits for simple calculations such as e.g. counting sheep
- we might need even larger values where 32 bits are not enough
- if we don’t need negative numbers why waste half the bits on this
Can we change the range of ints the computer actually uses?
Yes, you can change the memory requirements allocated to storing ints. You can also specify if you only want to use positive numbers.
What does “long” do?
- declares we want to use a greater range than 32 bits for ints
What does “short” do?
- declares we want to use a smaller range than 32 bits for ints
What does “unsigned” do?
- specifies that we only want to use non-negative ints
How do you use unsigned, long and short?
short int Counter
unsigned int Counter
long long int Counter
- int can be omitted as the use of the other 3 already implies we are dealing with int
short Counter
unsigned Counter
long long Counter
- modifiers can also be mixed together
unsigned long long Counter
Which restrictions exist regarding the use of the modifiers short, long and unsigned?
- only unsigned can be used in conjunction with char
- short and long can’t be used simultaneously
- short and unsigned can’t be used with float, long can
What is long float a synonym for?
double
What is the difference between long float and double?
- long float stores 32 bits (8 digits), double 64 (15-17 digits)
- double is more accurate
What is numerical anomaly?
- float and double have finite storage capacity, only a number of digits can be displayed
- if you add a very small number to a very large number, the small one will disappear
What is conversion?
~ changing the type of the data (perhaps combined with a change of its value, which may be caused by a loss of accuracy)
What 2 types of conversions does the C language know?
implicit and explicit conversions
What is an implicit conversion?
~ work according to language rules and are not specified in the code in any visible way; their operation is silent and automatic
What is an explicit conversion?
~ are carried out at the developer’s request; the developer should insert them explicitly inside the code indicating which value should be converted and into which resulting type
Implicit conversion happens according to which rules?
Rules apply in order of following strict rules until all the data in the expression has the same type:
1) data of type char or short int will be converted to type int (integer promotion)
2) if there is any value of type float, the other data will be converted to float
3) if there’s any value of type double, the other data will be converted to double
4) if there’s any value of type long int, the other data will be converted to long int
5) last conversion as requested by context
How do you introduce explicit conversions?
- with the typecast operator
(type) value
How would you convert the following? (implicit conversion)
int Int
char Char
short Short
float Float
Int = Short + Char + Float
1) promotions first:
Int = (int)Short + (int)Char + Float
2) Int = (float)((int)Short + (int)Char) + Float)
3) There is no double.
4) Due to the context of the assignment to Int, all need to be converted to int.
Int = (int)((float)((int)Short + (int)Char) + Float));