coding#6

Nested Loops and Programming Concepts

Introduction

  • Lecturers: Ms Marinda Taljaard, Ms Isabelle Taljaard, Mr Ighsaan Salie

  • Understanding programming involves key steps to be followed systematically.

Programming Thought Process

  • Information Gathering:

    • What information is at hand?

    • Variables (categories: types, constants, initialized, declared, user input)

    • What results are needed?

    • Required outputs and their display formats.

    • How to process the data from input to output?

    • Planning the solution involves applying formulas, logical expressions, loops, and their order.

    • How to implement the code?

    • Writing the solution involves adhering to syntax rules.

    • Is the code functioning correctly?

    • Conduct testing with various input data.

Practical 4 Overview

  • Discussion Points:

    • A breakdown of tasks over the following slides.

    • Various scenarios employed in practical sessions; individual solutions may differ yet still be correct.

Task 2 - Switch Statement Requirements
  • Requires a switch statement with:

    • Case Labels for each valid option.

    • Default Case Label for handling invalid options.

Task 1 - Output Message Format
  • Importance of adhering to specific formats for output messages.

Practical 4 Instructions and Looping Structures

Looping Constructs
  • Repeating Instructions:

    • Instructions can be repeated via loops.

  • Types of Loop Structures:

    • For Statement

    • While Statement

    • Do Statement: also known as do…while statement.

    • Nested Loops: an advanced loop where one loop runs inside another.

  • Considerations When Looping:

    • Selection of the appropriate looping structure.

    • Statements to be executed inside the loop versus those outside it (before/after).

    • Be cautious to prevent infinite loops.

Nested Loops

  • Characteristics of Nested Loops:

    • Can have multiple combinations (e.g., while-in-while, for-in-for, etc.).

    • Each loop is defined as:

    • Outer Loop: the primary loop that controls the set of iterations.

    • Inner Loop: contained within the outer loop, performing tasks for each iteration of the outer loop.

  • Considerations for Nested Loops:

    • Determining play of control by outer and inner loops.

    • Deciding which operations are part of the outer loop versus the inner loop.

Example Implementation
Creating a Table with Nested Loops
  • Goal: Create a table with 3 rows and 5 columns using nested loops.

    • Control Mechanics:

    • Outer loop: manages rows (one iteration per row).

    • Inner loop: controls column display for each row.

    • Example Output Structure:

    • Rows and columns constructed one-by-one with content displayed consolidated side-by-side per row twelve till last column.

Example Explanation
  • After completion of the inner loop:

    • The command WriteLine ends the current row after displaying its contents, triggering a new line for the next row.

  • The process continues incrementally through the rows until complete.

Homework Assignments

Homework Problem 1
  • Task: Use nested loops to display the following pattern:


    • 1 12 123 1234 12345

  • **Queries to Consider: **

    • Number of rows?

    • Number of columns?

    • Method for generating rows with increasing column counts?

Homework Problem 2
  • Task Overview:

    • Develop a program for 5 students to:

    • Read 3 valid grades (0 to 100) per student with accurate prompts.

    • Calculate and display the highest mark for each student and overall highest mark in the class.

  • Planning Steps:

    • Use an outer loop for the student count.

    • Implement an inner loop to capture three marks for each student.

    • Prompting user for valid entries; re-prompt if invalid marks are entered.

  • Considerations:

    • Determine where to compute per-student highest marks and overall highest class mark.

    • Properly initializing necessary variables and ensuring efficient data handling within loops.

Module To-Do List

  • Upcoming Tasks:

    • Review video materials available on the module site for Week 06, focusing on nested loops.

    • Complete Homework Problem 1 and 2 prior to the ensuing lecture.

    • Understand the outlined process for the impending semester test (a detailed breakdown is provided earlier).

    • Slot booking for the semester test on 20 March (open from 11 March to 18 March at 14:00).

    • Provide module feedback through the Google form shared in the Week 06 section.

    • Address any questions regarding the session topics.

Conclusion

  • Final Remarks:

  • A reminder to consolidate learning and apply concepts before scheduling the next meeting with lecturers.

  • Encouragement to engage actively in homework and upcoming assessments, utilizing the resources provided.