Programming Fundamentals: Selection Statements, Input/Output, Arithmetic Operations, and Loops Quiz

LuxuryRomanticism avatar
LuxuryRomanticism
·
·
Download

Start Quiz

Study Flashcards

12 Questions

What is the purpose of selection statements in programming?

To determine how a program executes based on conditions

In an if statement, what happens if the condition is false?

The program continues without executing the if block

What type of statement compares the value of an expression to multiple cases?

switch statement

What happens if none of the cases match in a switch statement?

The default case is executed, if present

Which type of statement checks if a condition is met and branches the program accordingly?

if statement

What is the primary purpose of a loop in programming?

To repeat a block of code multiple times

What function in programming languages is commonly used to read user input?

input()

In arithmetic operations, what does division do?

Divides values

What is the purpose of a for loop in programming?

Iterates through a range or list of items executing code block once for each item

Which arithmetic operation involves multiplying two numbers?

Multiplication

If you want to display information to the user in programming, which function would you typically use?

print()

What happens in a switch statement if none of the cases match the input value?

Execution falls to the default case

Study Notes

Programming Fundamentals: Selection Statements, Input, Output, Arithmetic Operations, and Loops

Selection Statements

Selection statements are fundamental elements of programming that determine how a program executes based on verifiable conditions. They serve as decision points where a program can branch off in different directions depending on the value of a condition.

Two types of selection statements are commonly used: if and switch.

If Statement

An if statement checks if a condition is met. If the condition is true, the code within the curly braces {} after the keyword if is executed. Otherwise, if the condition is false, the program continues execution without executing anything inside the if statement.

Example:

if x > 0:
    print("Positive number")
else:
    print("Non-positive number")

Switch Statement

A switch statement compares the value of an expression to multiple cases. If there is a match, the corresponding code is executed. If none of the cases match, the code under the default case is executed, if present.

Example:

def main():
    dayOfWeek = input("Enter a weekday (Monday, Tuesday, Wednesday, Thursday, Friday): ")
    
    switch dayOfWeek:
        case "Monday": print("Today is Monday.") break
        case "Tuesday": print("Today is Tuesday.") break
        case "Wednesday": print("Today is Wednesday.") break
        case "Thursday": print("Today is Thursday.") break
        case "Friday": print("Today is Friday.") break
        default: print("Invalid weekday.") break

Input & Output

Input and output operations allow programs to receive data from external sources and display results. They enable interaction with users and the environment.

Input

To obtain input from the user or another source, programming languages typically provide functions like input(). This function reads a line from standard input (like the keyboard) and converts it into a string.

Example:

userInput = input("Enter your name: ")
print("Hello,", userInput, "!")

Output

To display information to the user, the print() function is commonly used. It outputs the specified message followed by a newline character.

Example:

print("Hello, World!")

Arithmetic Operations

Arithmetic operations involve performing basic mathematical functions such as addition, subtraction, multiplication, and division. Programming languages follow the standard mathematical rules for these operations.

Addition

Addition combines the values of two numbers to obtain a new value.

Example:

a = 10
b = 5
sum = a + b
print(sum)

Subtraction

Subtraction calculates the difference between two numbers.

Example:

a = 10
b = 5
difference = a - b
print(difference)

Multiplication

Multiplication multiplies the values of two numbers to obtain a new value.

Example:

a = 10
b = 5
product = a * b
print(product)

Division

Division divides the value of the first number by the second number.

Example:

a = 10
b = 5
quotient = a / b
print(quotient)

Loops

Loops enable repetition of code blocks based on a specified condition. Two types of loops are commonly used: for loop and while loop.

For Loop

The for loop iterates through a range or list of items, executing the code block once for each item.

Example:

for i in range(10):
    print("Hello")

While Loop

The while loop continues executing the code block while a specified condition remains true. Once the condition becomes false, the loop terminates.

Example:

x = 0
while x < 10:
    print("Hello")
    x += 1

These programming concepts form the foundation of most programming languages. Understanding them allows developers to build powerful applications and solve complex problems through structured, conditional, and iterative operations.

Test your knowledge on essential programming concepts including selection statements (if and switch), input/output operations, arithmetic operations (addition, subtraction, multiplication, division), and loops (for and while). Improve your understanding of decision-making, data manipulation, and iterative processes in programming.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free

More Quizzes Like This

Structured Programming Fundamentals Quiz
10 questions
Programming Fundamentals Reviewer Quiz
5 questions
Introduction to Programming Constructs
6 questions

Introduction to Programming Constructs

IlluminatingNovaculite3231 avatar
IlluminatingNovaculite3231
Use Quizgecko on...
Browser
Browser