Fundamentals of programming Flashcards
What is the && operator?
Called Logical AND operator. If both the operands are non zero then condition becomes true.
e.g. (A && B) is false.
What is the || operator?
Called Logical OR Operator. If any of the two operands is non zero then condition becomes true.
e.g. (A || B) is true.
What is the ! operator?
Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.
!(A && B) is true.
What is the difference between the For, While and Do While loop? When should you use each?
While Loop:
- Used when you know you need to loop
- But don’t know how many times
- Remember checks condition first
- So may never run if condition not met at the start
- Need additional variables to control or count
For Loop:
- Use when you know how many times to run
- Or you can work out how many times
- Good for traversing a table or structure
- The variable declared can be used in the code
Do While
- Used when you don’t know how many times
- But you do know it is at least once
- Need additional variables to control or count
How do you change between Upper and Lower case?
string word = “crystal”;
Console.WriteLine(word.ToUpper());
Console.WriteLine(word.ToLower());
output=
CRYSTAL
crystal
How do you create a substring from a string?
string word = “crystal”;
Console.WriteLine(word.Substring(3, 3));
Console.WriteLine(word.Substring(1, 4));
//first number is index, second is length
output=
STA
RYST
How do you find the index of something within a string? How do you check if a string is contained within another?
string word = “crystal”;
Console.WriteLine(word.IndexOf(“sta”));
Console.WriteLine(word.Contains(“cry”));
output=
3
TRUE
How do you insert one string into another?
word.Insert(index, “”)
What are the Math operations?
Math.Abs - returns the absolute value
Math.Truncate - cut off any fractional part e.g. 14.56 -> 14
Math.Round - round to nearest whole number
Math.Floor - round down
Math.Pow - raise a number to the power of
Math.Sqrt - returns the squre root
What is a function?
A subroutine that returns a value
How does passing parameters into a function work?
- Subroutines can accept variables
1. Data types must be specified within brackets, 2. The same order of specifying variables must be used when calling the function
What does reference (ref) do for a value in a subroutine?
Any changes made to the value within the subroutine are retained, without this, they are not.
What is one benefit of using reference?
It removes the need for global variables
What is an exception?
An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program’s instructions
How does try, catch work?
- If an exception occurs, instead of erroring, the code at risk of causing an error can be wrapped in a ‘try{}’, if it does cause an exception the ‘catch{}’ block will be run.
- If no exception occurs, the catch block is ignored