Blog

  • Iterate through a code block using for statement in C#

    Use the for iteration statement to loop a pre-set number of times and control the iteration process.

    Learning objectives

    After you complete this module, you’ll be able to:

    • Use the for statement to loop through a block of code
    • Modify how the .NET Runtime executes the looping logic, changing the value of the iterator, the condition and the pattern

    https://lernix.com.my/angular-training-courses-malaysia

  • Branch the flow of code using the switch-case construct in C#

    Learn how to add branching logic that matches one variable or expression against many possible values.

    Learning objectives

    After you complete this module, you’ll be able to:

    • Use the switch-case construct to match a variable or expression against several possible outcomes.
    • Convert code that uses an if-elseif-else construct into a switch-case construct.

    https://lernix.com.my/aws-training-courses-malaysia

  • Control variable scope and logic using code blocks in C#

    Use code blocks with more confidence, understanding how they impact the visibility and accessibility of both higher and lower-level constructs in your code.

    Learning objectives

    After you complete this module, you’ll be able to:

    • Understand the impact of declaring and initializing variables inside and outside of code blocks.
    • Remove code blocks in if statements to improve readability when there’s only one line of code in the body of the code block.
    • Describe the purpose and scoping hierarchy for namespaces, classes, and methods.

    https://lernix.com.my/azure-training-courses-malaysia

  • Guided project – Develop foreach and if-elseif-else structures to process array data in C#

    Gain experience developing a console app that implements arrays, foreach loops, and if statements to achieve app specifications.

    Learning objectives

    In this module, you’ll practice how to:

    • Use Visual Studio Code to develop a C# console application that uses foreach loops to access array data.
    • Use if statements to evaluate conditions and create code branches.

    https://lernix.com.my/big-data-hadoop-training-courses-malaysia

  • Install and configure Visual Studio Code

    Learn how to configure Visual Studio Code for C# and how to use this professional IDE to create and run console applications.

    Learning objectives

    In this module, you will:

    • Configure Visual Studio Code and your development environment for programming in C#.
    • Explore the Visual Studio Code user interface.
    • Create, edit, build, and run a console application using Visual Studio Code.

    https://lernix.com.my/blockchain-training-courses-malaysia

  • Prepare

    In this guided project, you’ll use Visual Studio Code to develop portions of a C# console application. You’ll begin by writing the code that performs various numeric calculations. All calculations must be completed within the existing iteration and selection structures. This Prepare unit provides you with the overall goals of the project and the requirements for your application. The Setup section describes how to set up your development environment, including a “Starter” code project.

     Important

    This module includes coding activities that require Visual Studio Code. You’ll need access to a development environment that has Visual Studio Code installed and configured for C# application development.

    Project specification

    The Starter code project for this module is a C# console application that implements the following code features:

    • Uses arrays to store student names and assignment scores.
    • Uses a foreach statement to iterate through the student names as an outer program loop.
    • Uses an if statement within the outer loop to identify the current student’s name and access that student’s assignment scores.
    • Uses a foreach statement within the outer loop to iterate through the assignment scores array and sum the values.
    • Uses an algorithm within the outer loop to calculate the average exam score for each student.
    • Use an if-elseif-else construct within the outer loop to evaluate the average exam score and assign a letter grade automatically.
    • Integrates extra credit scores when calculating the student’s final score and letter grade as follows:
      • Detects extra credit assignments based on the number of elements in the student’s scores array.
      • Applies a 10% weighting factor to extra credit assignments before adding extra credit scores to the sum of exam scores.

    Your goal in this challenge is to implement the coding updates required to produce the teacher’s requested score report.

    The current score report lists the student’s name followed by the calculated overall grade and letter grade. Here is the existing report format:

    OutputCopy

    Student         Grade   Letter Grade
    
    Sophia          95.6    A
    Andrew          91.6    A-
    Emma            89.2    B+
    Logan           93      A
    

    In addition to the student’s final numeric score and letter grade, the teacher wants the updated report to include the exam score and the impact that extra credit work has on the student’s final grade. The format of the updated score report should appear as follows:

    OutputCopy

    Student         Exam Score      Overall Grade   Extra Credit
    
    Sophia          92.2            95.88   A       92 (3.68 pts)
    Andrew          89.6            91.38   A-      89 (1.78 pts)
    Emma            85.6            90.94   A-      89 (5.34 pts)
    Logan           91.2            93.12   A       96 (1.92 pts)
    

    Setup

    Use the following steps to prepare for the Challenge project exercises:

    1. To download a zip file containing the Starter project code, select the following link: Lab Files.
    2. Unzip the download files.Unzip the files in your development environment. Consider using your PC as your development environment so that you have access to your code after completing this module. If you aren’t using your PC as your development environment, you can unzip the files in a sandbox or hosted environment.
      1. On your local machine, navigate to your downloads folder.
      2. Right-click Challenge-project-foreach-if-array-CSharp-main.zip, and then select Extract all.
      3. Select Show extracted files when complete, and then select Extract.
      4. Make note of the extracted folder location.
    3. Copy the extracted ChallengeProject folder to your Windows Desktop folder. NoteIf a folder named ChallengeProject already exists, you can select Replace the files in the destination to complete the copy operation.
    4. Open the new ChallengeProject folder in Visual Studio Code.
      1. Open Visual Studio Code in your development environment.
      2. In Visual Studio Code, on the File menu, select Open Folder.
      3. Navigate to the Windows Desktop folder and locate the “ChallengeProject” folder.
      4. Select ChallengeProject and then select Select Folder.The Visual Studio Code EXPLORER view should show the ChallengeProject folder and two subfolders named Final and Starter.

    https://lernix.com.my/ibm-aix-system-i-training-courses-malaysia

  • Review the solution to the improve code readability challenge activity

    The following code is one possible solution for the challenge from the previous unit.

    c#Copy

    /*
       This code reverses a message, counts the number of times 
       a particular character appears, then prints the results
       to the console window.
     */
    
    string originalMessage = "The quick brown fox jumps over the lazy dog.";
    
    char[] message = originalMessage.ToCharArray();
    Array.Reverse(message);
    
    int letterCount = 0;
    
    foreach (char letter in message)
    {
        if (letter == 'o')
        {
            letterCount++;
        }
    }
    
    string newMessage = new String(message);
    
    Console.WriteLine(newMessage);
    Console.WriteLine($"'o' appears {letterCount} times.");
    

    This code is merely “one possible solution“. You may have come up with some different variable names and different vertical spacing and tab indentation. Here’s a list of changes that were made.

    • The code includes a higher-level description of what the entire code listing is attempting to accomplish in a multi-line comment at the top. You could argue that this is a small improvement over the original code comments, however, given the challenge’s description of the code, there wasn’t much more context available.
    • The individual comments were removed because they weren’t providing any real insight into the code’s purpose or function.
    • Several blank lines were added to improve the phrasing of the code listing. Keep code lines together when they appear similar, or when they work with each other to accomplish a small task.
    • Line feeds and tabs were added to improve the appearance of the foreach statement and the if statement.
    • Local variable naming conventions were applied to better convey the purpose of each value.

    If you identified the same issues and addressed them in a similar way, congratulations! Continue on to the knowledge check in the next unit.

    https://lernix.com.my/ibm-cognos-bi-training-courses-malaysia

  • Choose variable names that follow rules and conventions

    A software developer once famously said, “The hardest part of software development is naming things.” Not only does the name of a variable have to follow certain syntax rules, it should also be used to make the code more human-readable and understandable. That’s a lot to ask of one line of code!

    Variable name rules

    There are some variable naming rules that are enforced by the C# compiler.

    • Variable names can contain alphanumeric characters and the underscore (_) character. Special characters like the pound #, the dash -, and the dollar sign $ are not allowed.
    • Variable names must begin with an alphabetical letter or an underscore, not a number. Using an underscore character to start a variable name is typically reserved for private instance fields. A link to further reading can be found in the module summary.
    • Variable names must NOT be a C# keyword. For example, these variable name declarations won’t be allowed: float float; or string string;.
    • Variable names are case-sensitive, meaning that string MyValue; and string myValue; are two different variables.

    Variable name conventions

    Conventions are suggestions that are agreed upon by the software development community. While you’re free to decide not to follow these conventions, they’re so popular that it might make it difficult for other developers to understand your code. You should practice adopting these conventions and make them part of your own coding habits.

    • Variable names should use camel case, which is a style of writing that uses a lower-case letter at the beginning of the first word and an upper-case letter at the beginning of each subsequent word. For example: string thisIsCamelCase;.
    • Variable names should be descriptive and meaningful in your application. You should choose a name for your variable that represents the kind of data it will hold (not the data type). For example: bool orderComplete;, NOT bool isComplete;.
    • Variable names should be one or more entire words appended together. Don’t use contractions because the name of the variable may be unclear to others who are reading your code. For example: decimal orderAmount;, NOT decimal odrAmt;.
    • Variable names shouldn’t include the data type of the variable. You might see some advice to use a style like string strMyValue;. It was a popular style years ago. However, most developers don’t follow this advice anymore and there are good reasons not to use it.

    The example string firstName; follows all of these rules and conventions, assuming you want to use this variable to store data that represents someone’s first name.

    Variable name examples

    Here’s a few examples of variable declarations (using common data types):

    c#Copy

    char userOption;
    
    int gameScore;
    
    float particlesPerMillion;
    
    bool processedCustomer;
    

    Other naming conventions

    The rules and conventions described above are for local variables. A local variable is a variable that is scoped within the body of a method, or a variable in a console application that uses top-level statements (like the code in this module).

    There are other types of constructs that you can use in your applications, and many have their own conventions. For example, classes are often used in C# programming, and have associated conventions. Although you won’t be creating classes in this module, it’s important for you to know that the naming conventions you just learned about fit into a larger naming framework.

    https://lernix.com.my/ibm-informix-training-courses-malaysia

  • Get started with .NET Libraries

    There’s more to building a C# application than stringing together lines of code. You’ll need the .NET Runtime, which hosts and manages your code as it executes on the end user’s computer. You’ll also rely on the .NET Class Library, a prewritten collection of coding resources that you can use in your applications. This unit explains what the .NET Class Library is and how it complements the C# programming language.

    What is the .NET Class Library?

    When you need to find a book, a public library is a good place to look. After all, libraries contain thousands and thousands of books, and they’re organized into sections that help you to find what you’re looking for. When you need to implement a programming task, the .NET Class Library is a good place to look, because it’s an organized collection of programming resources.

    The .NET Class Library is a collection of thousands of classes containing tens of thousands of methods. For example, the .NET Class Library includes the Console class for developers working on console applications. The Console class includes methods for input and output operations such as Write()WriteLine()Read()ReadLine(), and many others. For example, you may already be familiar with the following code:

    C#Copy

    Console.WriteLine("Hello, World!")
    

    You can think of a class as a container for methods, kind of like the sections of the public library. Developers typically keep related methods together in a single class. As you saw in the previous example, any methods that can send or receive information from a console window are collected into the System.Console class in the .NET Class Library.

    In many cases, these classes and methods enable you to build a specific type of application. For example, one of the larger subsets of classes and methods enable you to create dynamic web applications. There’s also several families of classes that enable you to build native desktop applications. Another subset of classes and methods enable you to access a database. There are lots of classes in the .NET Class Library that support specific types of applications.

    There are other classes with methods that provide support in a more general way. In other words, their utility spans a wide range of device platforms, application frameworks, and technology areas. For example, if you want to read or write file information, or perform trigonometry or calculus operations, there are general purpose classes that you can use in your code. It doesn’t matter whether you’re building applications for the web, desktop, mobile device, or the cloud, general purpose classes and methods are there to help.

    As you can imagine, having a massive library of functionality available to your applications is a huge time saver for you as a software developer. The classes and methods in the .NET Class Library are created by Microsoft and are available for use in your applications.

    Even data types are part of the .NET Class Library

    C# data types (such as string and int) are actually made available through classes in the .NET Class Library. The C# language masks the connection between the data types and the .NET classes in order to simplify your work. However, behind the scenes, the data types are implemented just like every other class in the .NET Class Library. This connection provides your everyday variables with built-in methods that can be very helpful. The string class has lots of these helpful methods. For example, the string class has methods for converting text to uppercase and lowercase (ToUpper and ToLower).

    How to find what you need in the .NET Class Library

    With so many classes and methods, how can you find what you need for your application?

    First of all, remember that finding every class and method in the .NET Class Library is like finding every book in a large public library. You don’t need every book in the library, and you won’t be using every class and method in the .NET Class Library. Depending on the types of projects that you work on, you’ll become more familiar with some parts of the .NET Class Library and less familiar with others. Again, it’s like spending time in a section of the public library, over time you become familiar with what’s available. No one knows all of the .NET Class Library, not even people that work at Microsoft.

    Second, necessity will drive you to what you need. Most people go to the library when they need to find a book, not to see how many different books they can find. You don’t need to research classes and methods without a reason. When you have trouble figuring out a programming task, you can use your favorite search engine to find blog posts, articles, or forums where other developers have worked through similar issues. Third-party sources can give you clues about which .NET classes and methods you might want to use, and you may even find sample code that you can try.

    Third, Microsoft provides an online language reference and programming guide for C# that you can search through. You’ll likely spend time reading Microsoft’s documentation when you need to understand exactly what methods do, how they work, and their limitations. This documentation will become your source of truth for the .NET Class Library. Microsoft’s documentation team works closely with the .NET Class Library’s software developers to ensure its accuracy.

    Finally, as you begin to experiment with small code projects you’ll deepen your understanding of how the classes and methods work.

    All software developers follow a similar process when stepping into unfamiliar territory. The process of discovery is enjoyable, albeit challenging.

    Recap

    • The .NET Class Library supplies you with a wealth of functionality that you can use by merely referencing the classes and methods that you need.
    • Even your data types are part of the .NET Class Library. C# merely provides an alias for those data types.

    https://lernix.com.my/ibm-informix-training-courses-malaysia

  • Prepare for guided project

    You’ll be using the .NET Editor as your code development environment. You’ll be writing code that uses string and numeric variables, performs calculations, then formats and displays the results to a console.

    Project overview

    You’re developing a Student GPA Calculator that will help calculate students’ overall Grade Point Average. The parameters for your application are:

    • You’re given the student’s name and class information.
    • Each class has a name, the student’s grade, and the number of credit hours for that class.
    • Your application needs to perform basic math operations to calculate the GPA for the given student.
    • Your application needs to output/display the student’s name, class information, and GPA.

    To calculate the GPA:

    • Multiply the grade value for a course by the number of credit hours for that course.
    • Do this for each course, then add these results together.
    • Divide the resulting sum by the total number of credit hours.

    You’re provided with the following sample of a student’s course information and GPA:

    OutputCopy

    Student: Sophia Johnson
    
    Course          Grade   Credit Hours	
    English 101         4       3
    Algebra 101         3       3
    Biology 101         3       4
    Computer Science I  3       4
    Psychology 101      4       3
    
    Final GPA:          3.35
    

    Setup

    Use the following steps to prepare for the Guided project exercises:

    1. Open the .NET Editor coding environment.
    2. Copy and paste the following code into the .NET Editor. These values represent the student’s name and course details.C#Copystring studentName = "Sophia Johnson"; string course1Name = "English 101"; string course2Name = "Algebra 101"; string course3Name = "Biology 101"; string course4Name = "Computer Science I"; string course5Name = "Psychology 101"; int course1Credit = 3; int course2Credit = 3; int course3Credit = 4; int course4Credit = 4; int course5Credit = 3;

    Now you’re ready to begin the Guided project exercises. Good luck!

    https://lernix.com.my/ibm-infosphere-datastage-training-courses-malaysia