Docsity

Introduction to Pseudocode and Problem Solving in Programming, Slides of Computer Engineering and Programming

Partial preview of the text

Download Introduction to Pseudocode and Problem Solving in Programming and more Slides Computer Engineering and Programming in PDF only on Docsity!

The if Statement

  • if (Grade >= 50)

printf(“Pass”)

1. First evaluate the relationship operator in the

brackets

2. If the statement is “true” then print pass and

proceed to next command

3. If the statement is “false” then skip the statement

under the if statement and proceed to next

command

  • Conditions in if statements are formed by using the

equality operators and relational operators

Grade>=

Enter Grade

Print Pass

True

False

Solving a Problem

  • Generally, one has to understand what is required
  • Then write a Pseudocode
  • A pseudocode is a set of instructions in normal English language that one has to follow to solve a problem
  • There can be multiple pseudocode iterations until the pseudocode can be translated to code
  • Example:
  • A teacher has a class of 5 students who wrote an exam. Write a program that allows the

teacher to calculate the number of students that passed and those that failed from their

grade marks.

Solution

• Second iteration

  • Declare the variables,
    • Grade - a float data type to store the grade input,
    • pass - a counter to store the number of students that passed
    • fail - a counter to store the number of students that failed
  • Initialise the counters, pass and fail, to zero
  • Prompt the teacher to enter each grade mark
  • For each grade
    • if the grade mark is greater or equal to 50, then increase the pass counter
    • if the grade mark is less than 50, then increase the fail counter
  • Display the number of students that passed and those that failed

Code

Code

Grade is chosen to be a float data type because it is a real number

pass and fail are integers -> they are counters

pass and fail have to be initiated to 0 so that the counter can count up from 0

If the grade is greater or equal to 50, then increment the pass counter by 1. Note “%f” in scanf.

If the grade is less than 50, then increase the fail counter by 1

Note : A left brace, { , begins the body of each if statement and a corresponding right brace, } , ends each if statement’s body. Any number of statements can be placed in the body of an if statement.

Code

Repeat for all grades and display the output

Note: %f can be used also in printf to display numbers with decimal places

  • Sample output 1 Sample output
  • Sample output