Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

If Else Statment-Fundamentals of Computer-Lecture Slides, Slides of Computer Fundamentals

The course covers important and advance elements of C and C plus plus programming language. This course provides the student with the skills required to design, code, and test and execute programs of simple to intermediate complexity. It includes: Selection, Statement, Flow, Chart, Nested, Pseudocode, Compound, Syntax, Errors, Logic, Block

Typology: Slides

2011/2012

Uploaded on 07/31/2012

karthik
karthik 🇮🇳

4.6

(16)

95 documents

1 / 17

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
.
18
3.6 The ifelse Selection Statement
Flow chart of the ifelse selection statement
Nested ifelse statements
Test for multiple cases by placing ifelse selection
statements inside ifelse selection statement
Once condition is met, rest of statements skipped
Deep indentation usually not used in practice
true false
print “Failed” print “Passed”
grade >= 60
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download If Else Statment-Fundamentals of Computer-Lecture Slides and more Slides Computer Fundamentals in PDF only on Docsity!

3.6 The if … else Selection Statement

• Flow chart of the if…else selection statement

• Nested if…else statements

– Test for multiple cases by placing if…else selection

statements inside if…else selection statement

– Once condition is met, rest of statements skipped

– Deep indentation usually not used in practice

false true

print “Failed” print “Passed”

grade >= 60

3.6 The if … else Selection Statement

– Pseudocode for a nested if…else statement

If student’s grade is greater than or equal to 90

Print “A”

else

If student’s grade is greater than or equal to 80

Print “B”

else

If student’s grade is greater than or equal to 70

Print “C”

else

If student’s grade is greater than or equal to 60

Print “D”

else

Print “F”

3.6 The if … else Selection Statement

• Block:

– Compound statements with declarations

• Syntax errors

– Caught by compiler

• Logic errors:

– Have their effect at execution time

– Non-fatal: program runs, but has incorrect output

– Fatal: program exits prematurely

3.7 The while Repetition Statement

• Repetition structure

– Programmer specifies an action to be repeated while some

condition remains true

– Psuedocode:

While there are more items on my shopping list

Purchase next item and cross it off my list

– while loop repeated until condition becomes false

3.8 Formulating Algorithms

(Counter-Controlled Repetition)

• Counter-controlled repetition

– Loop repeated until counter reaches a certain value

– Definite repetition: number of repetitions is known

– Example: A class of ten students took a quiz. The grades

(integers in the range 0 to 100) for this quiz are available to

you. Determine the class average on the quiz

– Pseudocode:

Set total to zero

Set grade counter to one

While grade counter is less than or equal to ten

Input the next grade

Add the grade into the total

Add one to the grade counter

Set the class average to the total divided by ten

Print the class average

OutlineOutline

fig03_06.c (Part 1 of 2)

3.9 Formulating Algorithms with Top-

Down, Stepwise Refinement

• Problem becomes:

Develop a class-averaging program that will process an

arbitrary number of grades each time the program is run.

– Unknown number of students

– How will the program know to end?

• Use sentinel value

– Also called signal value, dummy value, or flag value

– Indicates “end of data entry.”

– Loop ends when user inputs the sentinel value

– Sentinel value chosen so it cannot be confused with a regular

input (such as -1 in this case)

3.9 Formulating Algorithms with Top-

Down, Stepwise Refinement

• Top-down, stepwise refinement

– Begin with a pseudocode representation of the top :

Determine the class average for the quiz

– Divide top into smaller tasks and list them in order:

Initialize variables

Input, sum and count the quiz grades

Calculate and print the class average

• Many programs have three phases:

– Initialization: initializes the program variables

– Processing: inputs data values and adjusts program variables

accordingly

– Termination: calculates and prints the final results

3.9 Formulating Algorithms with Top-

Down, Stepwise Refinement

• Refine Calculate and print the class average to

If the counter is not equal to zero

Set the average to the total divided by the counter

Print the average

else

Print “No grades were entered”

3.9 Formulating Algorithms with Top-

Down, Stepwise Refinement

Initialize total to zero

Initialize counter to zero

Input the first grade

While the user has not as yet entered the sentinel

Add this grade into the running total

Add one to the grade counter

Input the next grade (possibly the sentinel)

If the counter is not equal to zero

Set the average to the total divided by the counter

Print the average

else

Print “No grades were entered”

OutlineOutline

fig03_08.c (Part 2 of 2)

OutlineOutline

Program Output

Enter grade, -1 to end: 75 Enter grade, -1 to end: 94 Enter grade, -1 to end: 97 Enter grade, -1 to end: 88 Enter grade, -1 to end: 70 Enter grade, -1 to end: 64 Enter grade, -1 to end: 83 Enter grade, -1 to end: 89 Enter grade, -1 to end: - Class average is 82.

Enter grade, -1 to end: - No grades were entered