CS UNIT 11

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

Explain the difference between a constant and a variable in programming, and provide an example of when you might use each.

A constant is a named value that cannot change during the execution of a program, such as pi. A variable is a named value that can change during execution, such as a user's score in a game.

Describe the purpose of declaring variables before using them in a program. What advantages does this practice offer?

Declaring variables before use helps ensure that the program allocates the correct memory and data type for the variable. It improves code readability and helps prevent errors caused by using undeclared or mistyped variables.

Explain the concept of an identifier list in programming and why it's considered good practice to create one.

An identifier list is a comprehensive record of all variables and constants used in a program. Creating one ensures that all identifiers are declared and assigned values before manipulation, improving code organization and reducing errors.

How does pseudocode help in the initial stages of programming? Give an example of how pseudocode can be used to describe a simple task.

<p>Pseudocode allows programmers to outline the logic of a program without being concerned about the syntax of a specific language. For example, pseudocode for adding two numbers might look like this: <code>INPUT num1, num2</code>, <code>SUM = num1 + num2</code>, <code>OUTPUT SUM</code>.</p>
Signup and view all the answers

Describe the different input methods shown, with the programming languages that use the specific method.

<p>Python combines the prompt with the input statement and the type of the input, VB uses a separate prompt and input and the input specifies the type, and Java uses a separate prompt and input. The input specifies the type and declares the variable of the same type.</p>
Signup and view all the answers

Explain the purpose of using a WHILE loop to validate user input, and provide a pseudocode example of how it could be implemented.

<p>A <code>WHILE</code> loop ensures that the user provides valid input by repeatedly prompting for input until the specified condition is met. Example: <code>WHILE input &lt; 0 OR input &gt; 10 DO</code>, <code>OUTPUT &quot;Invalid input, enter again:&quot;</code>, <code>INPUT input</code>, <code>ENDWHILE</code>.</p>
Signup and view all the answers

Describe the primary difference between a CASE statement and a nested IF statement in programming. When is it more appropriate to use a CASE statement?

<p>A nested <code>IF</code> statement handles a series of binary choices, while a <code>CASE</code> statement handles multiple distinct choices based on the value of a single variable. A <code>CASE</code> statement is more appropriate when dealing with several mutually exclusive conditions, as it enhances code readability and maintainability.</p>
Signup and view all the answers

Explain the use of the OTHERWISE or ELSE or DEFAULT condition in a CASE statement, and provide a scenario where it would be particularly useful.

<p>The <code>OTHERWISE</code>/<code>ELSE</code>/<code>DEFAULT</code> condition in a <code>CASE</code> statement acts as a catch-all, executing when none of the specified cases match the input value. It is useful for handling unexpected or invalid input, like displaying an error message when a user enters an unrecognized menu option.</p>
Signup and view all the answers

Explain the difference between pre-condition and post-condition loops. What programming languages use them?

<p>A pre-condition loop checks the condition before executing the loop body and VB and Java use post-condition loops, while only Python uses pre-condition loops</p>
Signup and view all the answers

Explain the difference between a count-controlled loop, a post-condition loop and a pre-condition loop. Give an example of situations for each type.

<p>A count-controlled loop repeats a fixed number of times (e.g., printing numbers 1 to 10). A post-condition loop executes at least once and continues until a condition is met (e.g., validating user input). A pre-condition loop checks a condition before each iteration and continues as long as the condition is true (e.g., processing data while there are records to read).</p>
Signup and view all the answers

Explain why a REPEAT...UNTIL loop might be preferred over a WHILE...DO loop for input validation. What key characteristic makes it suitable for this purpose?

<p>A <code>REPEAT...UNTIL</code> loop is preferred for input validation because it always executes the loop body at least once, ensuring that the user is prompted for input before the validation check. This avoids the need for a separate initial input statement before the loop.</p>
Signup and view all the answers

Explain the purpose of loops in programming and describe a scenario where using a loop can significantly simplify a programming task.

<p>Loops allow a block of code to be executed repeatedly. A loop simplifies printing each element of an array, avoiding the need to write separate print statements for each element.</p>
Signup and view all the answers

Define what is meant by structured programming and explain the advantages of using this approach.

<p>Structured programming involves organizing code into logical, independent modules (procedures and functions) to improve readability, maintainability, and reusability. It reduces complexity and makes it easier to debug and modify the code.</p>
Signup and view all the answers

Explain the role of procedures in structured programming. How do procedures contribute to code organization and reusability?

<p>Procedures (or subroutines) group a set of statements into a reusable block of code. Procedures enhance code organization by breaking down complex tasks into smaller, manageable units. Reusability is improved as procedures can be called multiple times from different parts of the program.</p>
Signup and view all the answers

Explain why using procedures is advantageous when the same set of statements needs to be used throughout the program.

<p>Using procedures avoids code duplication by encapsulating a set of statements into a reusable block. This reduces the size of the program, improves readability, and simplifies maintenance, as changes only need to be made in one place.</p>
Signup and view all the answers

In the context of procedures, what is a parameter Explain the purpose of using parameters in procedures.

<p>A parameter is a variable that is a passed to a procedure so that it can use or modify that value. Parameters enable procedures to operate on different data inputs, making them more versatile and reusable.</p>
Signup and view all the answers

Describe what is meant by passing a parameter by value versus passing a parameter by reference. What are the implications of each method?

<p>Passing by value creates a copy of the variable, so modifications within the procedure do not affect the original variable. Passing by reference provides the procedure with direct access to the original variable, allowing changes within the procedure to alter the original variable.</p>
Signup and view all the answers

What are the key components of a procedure or function header? What information does the header convey?

<p>A procedure or function header typically includes the name of the procedure/function, the parameters it accepts (along with their datatypes), and (for a function) the data type of the return value. The header provides essential information about how to call and use the procedure/function correctly.</p>
Signup and view all the answers

What is the key difference between a procedure and a function in programming? How does this difference affect how they are used?

<p>A procedure performs a task and does not return a value, while a function performs a task and always returns a value. Functions are used as part of an expression, while procedures are called as standalone statements.</p>
Signup and view all the answers

Explain what is meant by a library routine in programming, and provide two examples of common library routines.

<p>A library routine is a pre-written, tested, and reusable function or procedure that is part of a programming language's standard library. Examples include input/output routines (e.g., <code>print</code> or <code>input</code>) and mathematical functions (e.g., <code>sqrt</code> or <code>sin</code>).</p>
Signup and view all the answers

What does the term header mean in the context of defining procedures and functions? What pieces of information are stored in headers?

<p>In the context of defining procedures and functions, the term header is the first line. This header contains it's name, what parameter may be being requested and the type of return value for the function.</p>
Signup and view all the answers

Explain what is meant by argument.

<p>An argument is the value passed to a procedure or function.</p>
Signup and view all the answers

What is the purpose of a RETURN statement in a function definition, and when might a function have more than one RETURN statement?

<p>A <code>RETURN</code> statement specifies the value that the function will return to the caller. A function may have multiple <code>RETURN</code> statements when different conditions lead to different results, allowing the function to exit at various points based on the input or calculations.</p>
Signup and view all the answers

What's the difference in Python with passing parameters?

<p>In Python, all data is passed by value.</p>
Signup and view all the answers

In the context of functions and procedures, what does the term ByRef mean?

<p><code>ByRef</code> means &quot;by reference.&quot; When a parameter is passed by reference, the called routine receives a direct pointer to the data, and any changes made to that data will persist after the routine's execution.</p>
Signup and view all the answers

Give three different variable name examples, with the following rules. One must be camel case, one must be snake case, and one must be pascal case.

<p>camel case: myVariableName, snake case: my_variable_name, pascal case: MyVariableName.</p>
Signup and view all the answers

In the pseudocode FUNCTION substring (myString: STRING, start: INTEGER, length: INTEGER) RETURNS STRING, what purpose do STRING and INTEGER serve?

<p><code>STRING</code> and <code>INTEGER</code> define the data types of the parameters the <code>substring</code> function accepts. <code>STRING</code> indicates that <code>myString</code> should be a string of text and <code>INTEGER</code> indicates that both <code>start</code> and <code>length</code> should be whole numbers.</p>
Signup and view all the answers

Explain what is the name of a procedure called, and what should be used instead?

<p>Procedures can also be called void functions in Python and Subroutines in VB, as well haveing unique names depending on the language.</p>
Signup and view all the answers

What are Library routines fully tested for?

<p>Library routines are fully tested and ready for use.</p>
Signup and view all the answers

Why is the keyword RETURN used in functions?

<p>The keyword RETURN is used as one of the statements in a function to specify the value to be returned</p>
Signup and view all the answers

Rewrite the pseudocode to ensure a function can only return a positive integer:

<p>REAL RETURNS REAL. This is not possible, as it can be any number. The INTEGER will ensure that the number returned is an integer and not a decimal.</p>
Signup and view all the answers

In terms of coding algorithms, sometimes tasks can be very similar, explain how we still get the correct answer, and what is done to the algorithm?

<p>Instead of repeating these statements every time they are required, many programming languages make use of subroutines or named procedures.</p>
Signup and view all the answers

For variables, what is important to do before any data manipulation?

<p>It is good practice to assign a value to any variables that are used, so that the programmer is certain of the starting value.</p>
Signup and view all the answers

What loop is best to use when the loop counter does not have to be managed by the programmer?

<p>Where the number of repetitions is known, a FOR-Next loop is the best choice, as the loop counter does not have to be managed by the programmer.</p>
Signup and view all the answers

Explain what is means by library routines?

<p>Many programming language development systems include library routines that are ready to incorporate into a program. These routines are fully tested and ready for use.</p>
Signup and view all the answers

Explain how the interface between a procedure and a program must relate?

<p>The interface between a procedure and a program must match the procedure definition. When a procedure is defined with parameters, the parameters in the procedure call must match those in the procedure definition.</p>
Signup and view all the answers

How can multiple RETURN statements be used in the same function?

<p>There can be more than one <code>RETURN</code> used in a function if there are different paths through its statements. This technique needs to be used with great care.</p>
Signup and view all the answers

Explain three benefits of using structured programing?

<p>Structured programming helps improve readability, maintainability, and reusability.</p>
Signup and view all the answers

Flashcards

Constant

A named value that cannot change during the execution of a program.

Variable

A named location in memory that stores a value that can be changed during program execution.

Procedure

A set of statements grouped together to perform a specific task, but does not return a value.

Function

A set of statements grouped together that performs a specific task and always returns a value.

Signup and view all the flashcards

Library Routine

A pre-built and tested routine available within a programming language's development system.

Signup and view all the flashcards

Looping

The process of repeating a block of code.

Signup and view all the flashcards

Header

The first statement in a procedure or function definition, declares name, parameters, and return type.

Signup and view all the flashcards

Parameter

A changeable variable applied to a procedure or function to pass values in.

Signup and view all the flashcards

Argument

The actual value passed to a procedure or function when it is called.

Signup and view all the flashcards

Pass by Value

Passing a parameter so its value cannot be changed by the procedure.

Signup and view all the flashcards

Pass by Reference

A method of passing parameters to a procedure in which the variable can be changed.

Signup and view all the flashcards

Study Notes

  • This chapter discusses programming concepts, including variables, constants, programming structures, and subroutines

What you should already know

  • Write an algorithm using pseudocode to sort a list of 10 numbers
  • The numbers should be input with prompts, placed in an array, sorted, and then searched for the number 27
  • Output the sorted list and a message stating whether 27 was found

Programming Basics

  • A constant is a named value that cannot change during program execution
  • A variable is a named value that can change during program execution
  • A function is a set of statements grouped together, called when required, and always returns a value
  • Library routines are tested, ready-to-use routines available in the development system
  • A procedure is a set of statements grouped together, called when required

Constants and Variables

  • All variables and constants should be declared before use
  • Constants are assigned a value when declared
  • It is good practice to assign a value to any variables that are used
  • Create an identifier list to ensure every variable is declared and assigned before data manipulation
  • Some programming languages do not support the declaration of variables
  • In languages like Python, assignment of a value ensures the correct data type is used

Programming Constructs

CASE and IF

  • Nested IF statements are used when there are two different choices
  • CASE statements are used for clarity when there are several different choices
  • Choices can be: value of an identifier, a range of values, or exact value
  • All conditions should conclude with an OTHERWISE case if no conditions are met

Loops

  • Loops enable sections of code to be repeated as required
  • Three types of loops include:
    • Count-controlled loop (FOR ... NEXT)
    • Post-condition loop (REPEAT ... UNTIL)
    • Pre-condition loop (WHILE ... DO ... ENDWHILE)
  • Select the appropriate loop structure to efficiently solve the problem
  • Use a REPEAT UNTIL loop rather than a WHILE DO ENDWHILE loop for a validation check

Structured Programming

  • Use procedures and functions
  • Procedures are subroutines or named procedures
  • Procedures are defined once and can be called multiple times within a program

Procedures

  • Procedures are called "void functions" in Python, "subroutines" in VB, and "methods" in Java
  • A procedure can be defined in pseudocode as:
    • PROCEDURE
    • ENDPROCEDURE
  • The procedure is called using: CALL <identifier>
  • It is often useful to pass a parameter to a procedure to modify the actions
  • This is done by passing a parameter when the procedure is called in the procedure definition
  • A procedure with parameters can be defined in pseudocode, as follows:
    • PROCEDURE (:, :...)
    • ENDPROCEDURE
  • The procedure can then be called many times: CALL <identifier> (Valuel, Value2...)
  • There are two methods of passing a parameter to a procedure:
    • By value: the value of the variable cannot be changed within the procedure
    • By reference: the value of the variable passed as the parameter can be changed by the procedure
  • A procedure with parameters passed by reference can be defined in pseudocode as follows:
    • PROCEDURE (BYREF :, :...)
    • ENDPROCEDURE

Functions

  • Used for calculations or tasks that produce an answer
  • A function always returns a value
  • Defined once and called many times within a program
  • Functions are "fruitful functions" in Python that return values, "functions" in Visual Basic, and "methods with returns" in Java
  • Functions can be used on the right-hand side of expressions
  • A function without parameters is defined in pseudocode as follows:
    • FUNCTION RETURNS
    • ENDFUNCTION
  • A function with parameters is defined in pseudocode as follows:
    • FUNCTION (:, :...) RETURNS
    • ENDFUNCTION
  • The keyword RETURN specifies the value returned and is usually the last statement in the function
  • Functions are used as part of an expression
  • The returned value is the value the function outputs
  • The interface between a function and a program must match the function definition
  • When a function is defined with parameters, the parameters in the function call must match those in the function definition
  • When procedures or functions are defined, the first statement in the definition is a header

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

Use Quizgecko on...
Browser
Browser