CC123 (Midterms)

Cards (220)

  • Array
    A collection of the same type variable
  • String
    A sequence of Unicode characters or array of characters
  • Arrays of strings
    An array of arrays of characters
  • Use case for arrays of strings
    • Storing the names of students in a class
  • Arrays of strings
    • Can be one dimensional or multidimensional
  • Declaring a string array
    1. Declaration without size:
    2. String[] variable_name;
    3. string[] variable_name;
    4. Declaration with size:
    5. String[] variable_name = new String[provide_size_here];
    6. string[] variable_name = new string[provide_size_here];
  • String array declaration
    • string[] s1;
    • String[] s2 = new String[4];
  • Initializing arrays of strings
    1. Arrays can be initialized after declaration
    2. Must be initialized with the new keyword if initializing after declaration
    3. Can't be initialized by only assigning values
  • Initializing string arrays
    • string[] str1, str2;
    • str1 = new string[5]{ "Element 1", "Element 2", "Element 3", "Element 4", "Element 5" };
    • str2 = new string[]{ "Element 1", "Element 2", "Element 3", "Element 4", "Element 5" };
  • Initialization without giving size is not valid in C#
  • Incorrect string array initialization
    • String[] str = new String[];
    • string[] str1;
    • str1 = {"Element 1", "Element 2", "Element 3", "Element 4" };
  • Accessing string array elements
    1. Assign values to array elements using index
    2. Access array elements using index
  • Accessing string array elements
    • s1[0] = 10;
    • s1[1] = 30;
    • s1[2] = 20;
    • s1[0]; // returns Geeks
    • s1[2]; // returns Noida
  • Declaring and initializing string array in one line
    • String[] weekDays = new string[3] {"Sun", "Mon", "Tue", "Wed"};
  • C# string array is an array of objects
  • It doesn't matter whether you create a string array using 'string' keyword or 'String' class object, they are the same
  • Decision Making
    Similar to decision making in real life, a certain block of code needs to be executed when some condition is fulfilled
  • Control statements

    Used to control the flow of execution of program based on certain conditions
  • Conditional statements in C#
    • if
    • if-else
    • if-else-if
    • Nested if
    • Switch
    • Nested switch
  • IF Statement
    1. Checks the given condition
    2. If condition evaluates to true, the block of code/statements will execute
    3. If condition evaluates to false, the block of code/statements will not execute
  • If the curly brackets { } are not used with if statements then the statement just next to it is only considered associated with the if statement
  • IF Statement

    • if (condition) statement 1; statement 2;
  • IF-else Statement

    1. Evaluates the code if the condition is true
    2. Executes the else block if the condition is false
  • IF-else-if ladder Statement

    1. Executes one condition from multiple statements
    2. Starts from top and checks each if condition
    3. Executes the statement of if block that evaluates to be true
    4. If none of the if conditions evaluate to true, the last else block is executed
  • Nested IF Statement

    1. If statement inside an if statement
    2. If statement is the target of another if or else statement
    3. Used when more than one condition needs to be true and one of the condition is the sub-condition of parent condition
  • Switch Statement
    • Alternative to long if-else-if ladders
    • Expression is checked for different cases and the matching case is executed
    • break statement is used to move out of the switch
    • If break is not used, control flows to all cases below until break is found or switch ends
    • Default case (optional) is executed if none of the cases match
  • Nested Switch
    1. Switch case present inside another switch case
    2. Inner switch is present in one of the cases in parent switch
  • Nested Switch
    • switch (j) { case 5: Console.WriteLine(5); switch (j - 1) { case 4: Console.WriteLine(4); switch (j - 2) { case 3: Console.WriteLine(3); break; } break; } break; case 10: Console.WriteLine(10); break; case 15: Console.WriteLine(15); break; default: Console.WriteLine(100); break; }
  • Computers are a balanced mix of software and hardware
  • Hardware
    A piece of mechanical device and its functions are being controlled by a compatible software
  • Hardware understands instructions
    In the form of electronic charge, which is the counterpart of binary language in software programming
  • Binary language
    Has only two alphabets, 0 and 1
  • To instruct, the hardware codes must be written in binary format, which is simply a series of 1s and 0s
  • It would be a difficult and cumbersome task for computer programmers to write such codes, which is why we have compilers to write such codes
  • Language Processing System
    Any computer system is made of hardware and software. The hardware understands a language which humans cannot understand. So we write programs in high-level language, which is easier for us to understand and remember. These programs are then fed into a series of tools and OS components to get the desired code that can be used by the machine
  • How a program using C compiler is executed on a host machine

    1. User writes a program in C language (high-level language)
    2. The C compiler compiles the program and translates it to assembly program (low-level language)
    3. An assembler then translates the assembly program into machine code (object)
    4. A linker tool is used to link all the parts of the program together for execution (executable machine code)
    5. A loader loads all of them into memory and then the program is executed
  • Preprocessor
    A tool that produces input for compilers, dealing with macro-processing, augmentation, file inclusion, language extension, etc.
  • Interpreter
    Translates high-level language into low-level machine language, but reads the source code or input statement by statement, converts it to intermediate code, executes it, and then takes the next statement
  • Assembler
    Translates assembly language programs into machine code, the output of which is called an object file
  • Linker
    Links and merges various object files together to make an executable file