G.E.S J.H.S Programming

Sequence Statements

Sequence Statements are statements that are executed in a sequential manner, one after another, in the order they appear in the code.

In programming, sequence statements are the most basic type of statements and are used to perform a series of operations or tasks. Each statement is executed one at a time, and the flow of execution moves from the first statement to the next until all statements have been executed.

Sequence statements are the simplest type of control structure in programming where each line of code is executed one after the other.

Think of it like a recipe: you can't eat the cake before you bake it, and you can't bake it before you mix the ingredients.

For example, the steps to calculate the area of a rectangle are:

  1. Get the length of the rectangle.
  2. Get the width of the rectangle.
  3. Calculate the area by multiplying the length and width.

When building such an interactive program to calculate the area, you must follow the following steps:

  1. Create a space on the memory to store the length.
  2. Create a space on the memory to store the width.
  3. Display information to the user to enter the length
  4. Get the length from the user and store it in the memory space created for length.
  5. Display information to the user to enter the width
  6. Get the width from the user and store it in the memory space created for width.
  7. Calculate the area by multiplying the length and width, and display the result to the user.

As a programmer, you must know the syntax to use to:

  1. Creates space in memory to store variables (Variable Declaration)
  2. Display information to the user (Output)
  3. Get input from the user (Input)
  4. Perform calculations (Processing)

The syntax to accomplish these varies from one programming language to another.

We will consider the syntax in C and Python programming languages.

We've already learned about variable declaration and initialization in C and python from our previous lessons.

Output syntax in C

The syntax for displaying output in C is the printf function.

The text to display is specified as a string and written between a double quote (") or single quote (')

For example, to display the message "Hello, World!" in C, you can use the following code:

printf("Hello, World!");

To display "WELCOME TO C PROGRAMMING" in C, you can use the following code:

printf("WELCOME TO C PROGRAMMING");

In the above program to calculate the area of a rectangle, to display "Enter length:", you can use the following code:

printf("Enter length:");

To display "Enter width:", you can use the following code:

printf("Enter width:");

Display memory content in C Programming

Placeholders are used in the printf function to display the values stored in variables.

Data Types and Placeholders Used in C

  1. %d - for integers
  2. %f - for floating-point numbers
  3. %c - for characters
  4. %s - for strings

After the format string, you specify the variables whose values you want to display in the order the placeholders appear.

For example, if we've named the memory to store the length as length, we can display its value using the following code:

printf("Length: %d", length);

If we've named the memory to store the width as width, we can display its value using the following code:

printf("Width: %d", width);

To display the area of the rectangle, we can use the following code:

printf("Area: %d", length * width);

In the above code, the placeholder is replaced by the result (length x width) to display the area

Using multiple placeholders

You can use multiple placeholders in a single printf statement to display multiple variables or values. You must specify the variables in the same order as the placeholders appear.

For example if the value of length in the memory is 5 and the width is 3 and we wish to display, "The area of a rectangle of length 5 cm and width 3 cm is 15 cm square", assuming the variables are named length and width, we could write the code below:

printf("The area of a rectangle of length %d cm and width %d cm is %d cm square", length, width, length * width);

In the above code, the placeholders are replaced by the values of the variables in the same order as they appear in the format string.

Note: %d is a placeholder for an integer value.

If our data types for length and width are floats, we would use %f instead of %d.

The code will then be:

printf("The area of a rectangle of length %f cm and width %f cm is %f cm square", length, width, length * width);

Output syntax in Python

In Python, you can use the print() function to display output.

For example, to display the message "Hello, World!" in Python, you can use the following code:

print("Hello, World!")

To display "WELCOME TO PYTHON PROGRAMMING" in Python, you can use the following code:

print("WELCOME TO PYTHON PROGRAMMING")

In the above program to calculate the area of a rectangle, to display "Enter length:", you can use the following code:

print("Enter length:")

To display "Enter width:", you can use the following code:

print("Enter width:")

For example, to display the length, width, and area of the rectangle in a single print statement, you can use the following code:

print(f"The area of a rectangle of length {length} cm and width {width} cm is {length * width} cm square")

The f-string syntax allows you to embed expressions directly inside string literals using curly braces {}.

In python, you can also use the format() method to display output.

For example, to display the length, width, and area of the rectangle in a single print statement using the format() method, you can use the following code:

print("The area of a rectangle of length {} cm and width {} cm is {} cm square".format(length, width, length * width))

Input syntax in C

In C, you can use the scanf() function to read input from the user.

The general syntax for scanf is:

scanf("placeholder", &variable);

In the above code, the placeholder specifies the type of data to read, and the ampersand (&) is used to pass the address of the variable where the input value will be stored.

The placeholders for scanf are:

  1. %d - for integers
  2. %f - for float values
  3. %lf - for double values
  4. %c - for characters

For example, to read an integer value from the user and store it in a variable named length, you can use the following code:

scanf("%d", &length);

Similarly, to read a float value from the user and store it in a variable named width, you can use the following code:

scanf("%f", &width);

We now have the necessary syntax knowledge to write our program to calculate the area of a rectangle in C.

Since the length and width could be decimal values, it is appropriate to store them as floats and use the %f placeholder for reading these values.

C Program To Calculate the Area of a Rectangle

#include <stdio.h>
int main() {
    float length, width; /* Declare variables to store length and width */
    printf("Enter length: "); /* Prompt the user to enter the length */
    scanf("%f", &length); /* Read the length from the user and store it in the variable 'length' */
    printf("Enter width: "); /* Prompt the user to enter the width */
    scanf("%f", &width); /* Read the width from the user and store it in the variable 'width' */
    printf("The area of a rectangle of length %.2f cm and width %.2f cm is %.2f cm square", length, width, length * width);
    return 0;
}

Note: The code to be executed should be placed between the opening and closing braces of the main function before the return statement.

If we use the double data type instead of float, we would use the %lf placeholder for reading these values.

The code will then be:

#include <stdio.h>
int main() {
    double length, width; /* Declare variables to store length and width */
    printf("Enter length: "); /* Prompt the user to enter the length */
    scanf("%lf", &length); /* Read the length from the user and store it in the variable 'length' */
    printf("Enter width: "); /* Prompt the user to enter the width */
    scanf("%lf", &width); /* Read the width from the user and store it in the variable 'width' */
    printf("The area of a rectangle of length %.2lf cm and width %.2lf cm is %.2lf cm square", length, width, length * width);
    return 0;
}

The explanatory statements are called comments which are ignored by the compiler.

You can use the c online compiler below to test the program:

https://www.programiz.com/c-programming/online-compiler/

Input syntax in Python

In Python, you can use the input() function to read input from the user.

The general syntax for input is:

variable = input("prompt")

In the above code, the prompt is displayed to the user, and the value entered by the user is stored in the variable.

For example, to read a value from the user and store it in a variable named length, you can use the following code:

length = float(input("Enter length: "))

Similarly, to read a float value from the user and store it in a variable named width, you can use the following code:

width = float(input("Enter width: "))

Note: in python input read from the user is string, so you need to convert it to the appropriate data type using functions like int() or float().

We now have the necessary syntax knowledge to write our program to calculate the area of a rectangle in Python.

length = float(input("Enter length: "))
width = float(input("Enter width: "))
print("The area of a rectangle of length", length, "cm and width", width, "cm is", length * width, "cm square")

Similarly, you can use the f-string syntax for more readable string formatting:

print(f"The area of a rectangle of length {length} cm and width {width} cm is {length * width} cm square")

The code with therefore be:

length = float(input("Enter length: "))
width = float(input("Enter width: "))
print(f"The area of a rectangle of length {length} cm and width {width} cm is {length * width} cm square")

You can likewise use the format() method for string formatting:

print("The area of a rectangle of length {} cm and width {} cm is {} cm square".format(length, width, length * width))

The code will then be:

length = float(input("Enter length: "))
width = float(input("Enter width: "))
print("The area of a rectangle of length {} cm and width {} cm is {} cm square".format(length, width, length * width))

In the above code, the placeholders in the format string are replaced by the values of the variables in the same order as they appear in the format string.

You can use the online python compiler below to test the program:

https://www.programiz.com/python-programming/online-compiler/

Programming Tasks

Write a program in C and Python to calculate the circumference and area of a circle.

Notes:

  1. The circumference of a circle is given by the formula: C = 2 * π * r
  2. The area of a circle is given by the formula: A = π * r2
  3. r2 = r * r (A square is a number multiplied by itself) in C
  4. r2 = r ** 2 in Python
  5. π = 3.142
  6. r is the radius of the circle, which can be obtained from the user.
  7. A constant is defined in C as #define CONSTANT_NAME VALUE_OF_CONSTANT
  8. Constant for PI (π) in C will be: #define PI 3.142
  9. A constant is defined in Python as CONSTANT_NAME = VALUE_OF_CONSTANT
  10. In python, variables in uppercase are typically used to represent constants.
  11. Constant for PI (π) in Python will be: PI = 3.142

The sequence of steps to solve this problem is as follows:

  1. Define a constant for PI (π).
  2. Get the radius of the circle from the user.
  3. Calculate the circumference using the formula C = 2 * π * r.
  4. Calculate the area using the formula A = π * r2.
  5. Display the calculated circumference and area to the user.

Note: for C, the get input is in three steps: declaration(s), instructing the user (printf), and getting the value (scanf).

For Python, the get input is in one step using the input() function and the type casting if not string.

C Program To Calculate Circumference and Area of a Circle

#include <stdio.h>
#define PI 3.142
int main() {
    float radius, circumference, area; /* Declare variables to store radius, circumference, and area */
    printf("Enter radius: "); /* Prompt the user to enter the radius */
    scanf("%f", &radius); /* Read the radius from the user and store it in the variable 'radius' */
    circumference = 2 * PI * radius; /* Calculate the circumference using the formula C = 2 * π * r */
    area = PI * radius * radius; /* Calculate the area using the formula A = π * r^2 */
    printf("The circumference of a circle with radius %.2f cm is %.2f cm\n", radius, circumference);
    printf("The area of a circle with radius %.2f cm is %.2f cm square", radius, area);
    return 0;
}

In the above C program, the circumference and area are stored before being displayed to the user.

In case you decide not to store but calculate directly, the code will be:

#include <stdio.h>
#define PI 3.142
int main() {
    float radius; /* Declare variable to store radius */
    printf("Enter radius: "); /* Prompt the user to enter the radius */
    scanf("%f", &radius); /* Read the radius from the user and store it in the variable 'radius' */
    printf("The circumference of a circle with radius %.2f cm is %.2f cm\n", radius, 2 * PI * radius);
    printf("The area of a circle with radius %.2f cm is %.2f cm square", radius, PI * radius * radius);
    return 0;
}

Notes:

  1. \n is used for a new line.
  2. The .2f format specifier is used to display the floating-point numbers with two decimal places.

Python Program To Calculate Circumference and Area of a Circle

PI = 3.142
radius = float(input("Enter radius: "))
circumference = 2 * PI * radius
area = PI * radius ** 2
print(f"The circumference of a circle with radius {radius} cm is {circumference} cm")
print(f"The area of a circle with radius {radius} cm is {area} cm square")

Using the format instead of the f-string, the code would be:

PI = 3.142
radius = float(input("Enter radius: "))
circumference = 2 * PI * radius
area = PI * radius ** 2
print("The circumference of a circle with radius {} cm is {} cm".format(radius, circumference))
print("The area of a circle with radius {} cm is {} cm square".format(radius, area))

In the above Python program, the circumference and area are stored before being displayed to the user.

In case you decide not to store but calculate directly, the code will be:

PI = 3.142
radius = float(input("Enter radius: "))
print(f"The circumference of a circle with radius {radius} cm is {2 * PI * radius} cm")
print(f"The area of a circle with radius {radius} cm is {PI * radius ** 2} cm square")

In the above code, the circumference and area are calculated directly within the print statements without being stored in separate variables.

Using the format method instead of the f-string, the code would be:

PI = 3.142
radius = float(input("Enter radius: "))
print("The circumference of a circle with radius {} cm is {} cm".format(radius, 2 * PI * radius))
print("The area of a circle with radius {} cm is {} cm square".format(radius, PI * radius ** 2))

Note: in python, square is represented by **

You can use the various compilers to run the C and Python programs.