CP 04 Control Flow

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 114

BITS Pilani

K K Birla Goa Campus

CSF111: Computer Programming

Control Flow: Branching

Swaroop Joshi

2023

Control flow
Control flow

✤ The order in which statements in a


program are executed is called the
ow of control or control ow
fl
fl
Control flow

✤ The order in which statements in a


program are executed is called the
ow of control or control ow

✤ So far we have only seen sequential


execution: statements execute one
after the other in the order in which
they appear in the program
fl
fl
Control flow

✤ The order in which statements in a


program are executed is called the int main() {
ow of control or control ow printf(“Lunch Time!”);
eat();
printf(“Back to work.”);
✤ So far we have only seen sequential return 0;
execution: statements execute one }
after the other in the order in which
they appear in the program
fl

fl

Control flow

✤ The order in which statements in a


program are executed is called the int main() {
ow of control or control ow printf(“Lunch Time!”);
eat();
printf(“Back to work.”);
✤ So far we have only seen sequential return 0;
execution: statements execute one }
after the other in the order in which
they appear in the program
fl

fl

Consider the following task


Consider the following task

✤ Hogwarts wants to grade their OWLs the muggle


way this year and has recruited you as a
programmer!
Consider the following task

✤ Hogwarts wants to grade their OWLs the muggle


way this year and has recruited you as a
programmer!

✤ Write a program that assigns grades to students


based on the following (simpli ed) scale

✤ Outstanding (O): 91-100

✤ Exceeds Expectations (E): 81-90

✤ Acceptable (A): 71-80

✤ Poor (P): 70 and below


fi
Consider the following task
Design recipe
✤ Hogwarts wants to grade their OWLs the muggle
way this year and has recruited you as a Data int marks
programmer!

✤ Write a program that assigns grades to students


based on the following (simpli ed) scale

✤ Outstanding (O): 91-100

✤ Exceeds Expectations (E): 81-90

✤ Acceptable (A): 71-80

✤ Poor (P): 70 and below


fi
Consider the following task
Design recipe
✤ Hogwarts wants to grade their OWLs the muggle
way this year and has recruited you as a Data int marks
programmer! Returns a letter grade for the
Purpose and
Header given marks
✤ Write a program that assigns grades to students char grade(int marks)
based on the following (simpli ed) scale

✤ Outstanding (O): 91-100

✤ Exceeds Expectations (E): 81-90

✤ Acceptable (A): 71-80

✤ Poor (P): 70 and below


fi
Consider the following task
Design recipe
✤ Hogwarts wants to grade their OWLs the muggle
way this year and has recruited you as a Data int marks
programmer! Returns a letter grade for the
Purpose and
Header given marks
✤ Write a program that assigns grades to students char grade(int marks)
based on the following (simpli ed) scale
‘O’ grade(95)
✤ Outstanding (O): 91-100 ‘E’ grade(90)
Examples
‘A’ grade(71)
✤ Exceeds Expectations (E): 81-90 ‘P’ grade(70)

✤ Acceptable (A): 71-80

✤ Poor (P): 70 and below


fi
Consider the following task
Design recipe
✤ Hogwarts wants to grade their OWLs the muggle
way this year and has recruited you as a Data int marks
programmer! Returns a letter grade for the
Purpose and
Header given marks
✤ Write a program that assigns grades to students char grade(int marks)
based on the following (simpli ed) scale
‘O’ grade(95)
✤ Outstanding (O): 91-100 ‘E’ grade(90)
Examples
‘A’ grade(71)
✤ Exceeds Expectations (E): 81-90 ‘P’ grade(70)
???
✤ Acceptable (A): 71-80
Body // make a decision based on
✤ Poor (P): 70 and below some values; cannot be seq.
fi
Consider a simpler task
Consider a simpler task

✤ Given two integers, return the


greater of the two.
Consider a simpler task
Design recipe
✤ Given two integers, return the Data first, second both ints
greater of the two.
Consider a simpler task
Design recipe
✤ Given two integers, return the Data first, second both ints
greater of the two. Returns the larger of the two given
Purpose and
Header numbers
int max(int first, int second)
Consider a simpler task
Design recipe
✤ Given two integers, return the Data first, second both ints
greater of the two. Returns the larger of the two given
Purpose and
Header numbers
int max(int first, int second)
1 max (0, 1)
0 max(0, -1)
Examples
1 max(1,1)
-1 max(-1, -1)
Consider a simpler task
Design recipe
✤ Given two integers, return the Data first, second both ints
greater of the two. Returns the larger of the two given
Purpose and
Header numbers
int max(int first, int second)
1 max (0, 1)
0 max(0, -1)
Examples
1 max(1,1)
-1 max(-1, -1)
???
Body // make a decision based on some
values; cannot be seq.
Consider a simpler task
Design recipe
✤ Given two integers, return the Data first, second both ints
greater of the two. Returns the larger of the two given
Purpose and
Header numbers
Is rst > int max(int first, int second)
second? 1 max (0, 1)
0 max(0, -1)
Examples
1 max(1,1)
-1 max(-1, -1)
???
Body // make a decision based on some
values; cannot be seq.
fi
Consider a simpler task
Design recipe
✤ Given two integers, return the Data first, second both ints
greater of the two. Returns the larger of the two given
Purpose and
Header numbers
Is rst > int max(int first, int second)
second? 1 max (0, 1)
0 max(0, -1)
Examples
1 max(1,1)
-1 max(-1, -1)
max = rst ???
Body // make a decision based on some
values; cannot be seq.
fi
fi
Consider a simpler task
Design recipe
✤ Given two integers, return the Data first, second both ints
greater of the two. Returns the larger of the two given
Purpose and
Header numbers
Is rst > int max(int first, int second)
second? 1 max (0, 1)
0 max(0, -1)
Yes Examples
1 max(1,1)
-1 max(-1, -1)
max = rst ???
Body // make a decision based on some
values; cannot be seq.
fi
fi
Consider a simpler task
Design recipe
✤ Given two integers, return the Data first, second both ints
greater of the two. Returns the larger of the two given
Purpose and
Header numbers
Is rst > int max(int first, int second)
second? 1 max (0, 1)
0 max(0, -1)
Yes Examples
1 max(1,1)
-1 max(-1, -1)
max = rst max = second ???
Body // make a decision based on some
values; cannot be seq.
fi
fi
Consider a simpler task
Design recipe
✤ Given two integers, return the Data first, second both ints
greater of the two. Returns the larger of the two given
Purpose and
Header numbers
Is rst > int max(int first, int second)
second? 1 max (0, 1)
0 max(0, -1)
Yes No Examples
1 max(1,1)
-1 max(-1, -1)
max = rst max = second ???
Body // make a decision based on some
values; cannot be seq.
fi
fi
A branching instruction: if-else

✤ Given two integers, return the


greater of the two.

Is rst >
second?

Yes No

max = rst max = second


fi
fi
A branching instruction: if-else

✤ Given two integers, return the int max(int first, int second) {
greater of the two.

Is rst >
second?

Yes No

}
max = rst max = second
fi
fi

A branching instruction: if-else

✤ Given two integers, return the int max(int first, int second) {
greater of the two. int max = 0;

Is rst >
second?

Yes No
return max;
}
max = rst max = second
fi
fi

A branching instruction: if-else

✤ Given two integers, return the int max(int first, int second) {
greater of the two. int max = 0;
if (first > second) {
Is rst >
}
second?

Yes No
return max;
}
max = rst max = second

fi
fi

A branching instruction: if-else

✤ Given two integers, return the int max(int first, int second) {
greater of the two. int max = 0;
if (first > second) {
Is rst >
}
second?

Yes No
return max;
}
max = rst max = second

fi
fi

A branching instruction: if-else

✤ Given two integers, return the int max(int first, int second) {
greater of the two. int max = 0;
if (first > second) {
Is rst > max = first;
}
second?

Yes No
return max;
}
max = rst max = second

fi
fi

A branching instruction: if-else

✤ Given two integers, return the int max(int first, int second) {
greater of the two. int max = 0;
if (first > second) {
Is rst > max = first;
} else {
second?
max = second;
Yes No }
return max;
}
max = rst max = second

fi
fi

A branching instruction: if-else

✤ Given two integers, return the int max(int first, int second) {
greater of the two. int max = 0;
if (first > second) {
Is rst > max = first;
} else {
second?
max = second;
Yes No }
return max;
}
max = rst max = second

fi
fi

A branching instruction: if-else

✤ Given two integers, return the int max(int first, int second) {
greater of the two. int max = 0;
if (first > second) {
Is rst > max = first;
} else {
second?
max = second;
Yes No }
return max;
}
max = rst max = second

fi
fi

Anatomy of an if-else statement

prev_stmt prev_stmt

if (test) {
test
if_block
T F
} else {
if_block else_block
else_block

}
next_stmt
next_stmt

Anatomy of an if-else statement


Boolean
expression
prev_stmt prev_stmt

if (test) {
test
if_block
T F
} else {
if_block else_block
else_block

}
next_stmt
next_stmt

Anatomy of an if-else statement


Boolean
expression
prev_stmt prev_stmt

if (test) {
Zero or more
test statements
if_block
T F
} else {
if_block else_block
else_block

}
next_stmt
next_stmt

Anatomy of an if-else statement


Boolean
expression
prev_stmt prev_stmt

if (test) {
Zero or more
test statements
if_block
T F
} else {
Zero or more
if_block else_block
else_block statements

}
next_stmt
next_stmt

Anatomy of an if-else statement


Boolean
expression
prev_stmt prev_stmt

if (test) {
Zero or more
test statements
if_block
T F
} else {
Zero or more
if_block else_block
else_block statements

} if-else is a compound statement


next_stmt does NOT end with a semicolon
next_stmt

Tracing an if-else statement


Tracing an if-else statement Trace only the portions of the
statement that get executed
Tracing an if-else statement Trace only the portions of the
statement that get executed

Program State
Tracing an if-else statement Trace only the portions of the
statement that get executed

Program State
rst = 1, second = 0
fi
Tracing an if-else statement Trace only the portions of the
statement that get executed

Program State
rst = 1, second = 0
int max = 0;
fi
Tracing an if-else statement Trace only the portions of the
statement that get executed

Program State
rst = 1, second = 0
int max = 0;
rst = 1, second = 0, max = 0
fi
fi
Tracing an if-else statement Trace only the portions of the
statement that get executed

Program State
rst = 1, second = 0
int max = 0;
rst = 1, second = 0, max = 0
if (first > second) {
max = first;
fi
fi

Tracing an if-else statement Trace only the portions of the


statement that get executed

Program State
rst = 1, second = 0
int max = 0;
rst = 1, second = 0, max = 0
if (first > second) {
max = first;
rst = 1, second = 0, max = 0 1
fi
fi
fi

Tracing an if-else statement Trace only the portions of the


statement that get executed

Program State
rst = 1, second = 0
int max = 0;
rst = 1, second = 0, max = 0
if (first > second) {
max = first;
rst = 1, second = 0, max = 0 1
} else {
max = second;
fi
fi
fi

Tracing an if-else statement Trace only the portions of the


statement that get executed

Program State
rst = 1, second = 0
int max = 0;
rst = 1, second = 0, max = 0
if (first > second) {
max = first;
rst = 1, second = 0, max = 0 1
} else {
max = second;
fi
fi
fi

Tracing an if-else statement Trace only the portions of the


statement that get executed

Program State
rst = 1, second = 0
int max = 0;
rst = 1, second = 0, max = 0
if (first > second) {
max = first;
rst = 1, second = 0, max = 0 1
} else {
max = second;

}
fi
fi
fi

Tracing an if-else statement Trace only the portions of the


statement that get executed

Program State
rst = 1, second = 0
int max = 0;
rst = 1, second = 0, max = 0
if (first > second) {
max = first;
rst = 1, second = 0, max = 0 1
} else {
max = second;

}
rst = 1, second = 0, max = 1
fi
fi
fi
fi

Tracing an if-else statement Trace only the portions of the


statement that get executed

Program State
rst = 1, second = 0
int max = 0;
rst = 1, second = 0, max = 0
if (first > second) {
max = first;
rst = 1, second = 0, max = 0 1
} else {
max = second;
Else-block skipped for these values
}
rst = 1, second = 0, max = 1
fi
fi
fi
fi

Tracing an if-else statement Trace only the portions of the


statement that get executed

Program State
rst = 1, second = 0
int max = 0;
rst = 1, second = 0, max = 0
if (first > second) {
max = first;
rst = 1, second = 0, max = 0 1
} else {
max = second;
Else-block skipped for these values
} Must show values here
after the entire if-else rst = 1, second = 0, max = 1
fi
fi
fi
fi

Tracing an if-else statement


Tracing an if-else statement
Program State
Tracing an if-else statement
Program State
rst = -1, second = -1
fi
Tracing an if-else statement
Program State
rst = -1, second = -1
int max = 0;
fi
Tracing an if-else statement
Program State
rst = -1, second = -1
int max = 0;
rst = -1, second = -1, max = 0
fi
fi
Tracing an if-else statement
Program State
rst = -1, second = -1
int max = 0;
rst = -1, second = -1, max = 0
if (first > second) {
max = first;
fi
fi

Tracing an if-else statement


Program State
rst = -1, second = -1
int max = 0;
rst = -1, second = -1, max = 0
if (first > second) {
max = first;
fi
fi

Tracing an if-else statement


Program State
rst = -1, second = -1
int max = 0;
rst = -1, second = -1, max = 0
if (first > second) {
max = first;

} else {
max = second;
fi
fi

Tracing an if-else statement


Program State
rst = -1, second = -1
int max = 0;
rst = -1, second = -1, max = 0
if (first > second) {
max = first;

} else {
max = second;
rst = -1, second = -1, max = 0 -1
fi
fi
fi

Tracing an if-else statement


Program State
rst = -1, second = -1
int max = 0;
rst = -1, second = -1, max = 0
if (first > second) {
max = first;

} else {
max = second;
rst = -1, second = -1, max = 0 -1
}
fi
fi
fi

Tracing an if-else statement


Program State
rst = -1, second = -1
int max = 0;
rst = -1, second = -1, max = 0
if (first > second) {
max = first;

} else {
max = second;
rst = -1, second = -1, max = 0 -1
}
rst = -1, second = -1, max = -1
fi
fi
fi
fi

Tracing an if-else statement


Program State
rst = -1, second = -1
int max = 0;
rst = -1, second = -1, max = 0
if (first > second) {
max = first;
If-block skipped for these values

} else {
max = second;
rst = -1, second = -1, max = 0 -1
}
rst = -1, second = -1, max = -1
fi
fi
fi
fi

Tracing an if-else statement


Program State
rst = -1, second = -1
int max = 0;
rst = -1, second = -1, max = 0
if (first > second) {
max = first;
If-block skipped for these values

} else {
max = second;
rst = -1, second = -1, max = 0 -1
} Must show values here
after the entire if-else rst = -1, second = -1, max = -1
fi
fi
fi
fi

Boolean expr: evaluate to TRUE or FALSE


Boolean expr: evaluate to TRUE or FALSE

✤ marks > 90
Boolean expr: evaluate to TRUE or FALSE

✤ marks > 90

✤ grade == ‘E’
Boolean expr: evaluate to TRUE or FALSE

✤ marks > 90

✤ grade == ‘E’

✤ 4
Boolean expr: evaluate to TRUE or FALSE

✤ marks > 90

✤ grade == ‘E’
In C, any non-zero
✤ 4 value is TRUE
Boolean expr: evaluate to TRUE or FALSE

✤ What about ‘marks between 81 and 90’?

✤ marks > 90

✤ grade == ‘E’
In C, any non-zero
✤ 4 value is TRUE
Boolean expr: evaluate to TRUE or FALSE

✤ What about ‘marks between 81 and 90’?

✤ marks >= 81 && marks < 90


✤ marks > 90

✤ grade == ‘E’
In C, any non-zero
✤ 4 value is TRUE
Boolean expr: evaluate to TRUE or FALSE

✤ What about ‘marks between 81 and 90’?

✤ marks >= 81 && marks < 90


✤ marks > 90
✤ Marks below 81 or above 90?
✤ grade == ‘E’
In C, any non-zero
✤ 4 value is TRUE
Boolean expr: evaluate to TRUE or FALSE

✤ What about ‘marks between 81 and 90’?

✤ marks >= 81 && marks < 90


✤ marks > 90
✤ Marks below 81 or above 90?
✤ grade == ‘E’
✤ marks < 81 || marks > 90
In C, any non-zero
✤ 4 value is TRUE
Boolean expr: evaluate to TRUE or FALSE

✤ What about ‘marks between 81 and 90’?

✤ marks >= 81 && marks < 90


✤ marks > 90
✤ Marks below 81 or above 90?
✤ grade == ‘E’
✤ marks < 81 || marks > 90
In C, any non-zero
✤ 4 value is TRUE
✤ Marks not equal to 75?
Boolean expr: evaluate to TRUE or FALSE

✤ What about ‘marks between 81 and 90’?

✤ marks >= 81 && marks < 90


✤ marks > 90
✤ Marks below 81 or above 90?
✤ grade == ‘E’
✤ marks < 81 || marks > 90
In C, any non-zero
✤ 4 value is TRUE
✤ Marks not equal to 75?

✤ marks != 75
Evaluate each of the following expressions
Evaluate each of the following expressions

✤ Given → int x = 5, y = 12;


Evaluate each of the following expressions

✤ Given → int x = 5, y = 12;

✤ x > 0 && x < 10


Evaluate each of the following expressions

✤ Given → int x = 5, y = 12;

✤ x > 0 && x < 10

✤ x <= 0 || x >= 10
Evaluate each of the following expressions

✤ Given → int x = 5, y = 12;

✤ x > 0 && x < 10

✤ x <= 0 || x >= 10

✤ (x – 1) == ((y / 5) + (y % 5))
Evaluate each of the following expressions

✤ Given → int x = 5, y = 12;

✤ x > 0 && x < 10

✤ x <= 0 || x >= 10

✤ (x – 1) == ((y / 5) + (y % 5))

✤ (x != y) || !(x == y)
Write a boolean expression for each of these
✤ Given two integer variables: n and m

✤ n is equal to 3 or 5

✤ n is between 25 and 50, both exclusive

✤ n is below 15 or above 42

✤ n and m both are multiples of 3

✤ n is a multiple of 7 and m is negative

✤ n and m are both positive or both negative

✤ e.g., (4,5), (-2, -3): TRUE; (4, -5), (-2 ,3), (0,0): FALSE
Write a boolean expression for each of these
✤ Given two integer variables: n and m

✤ n is equal to 3 or 5 n==3 || n==5

✤ n is between 25 and 50, both exclusive

✤ n is below 15 or above 42

✤ n and m both are multiples of 3

✤ n is a multiple of 7 and m is negative

✤ n and m are both positive or both negative

✤ e.g., (4,5), (-2, -3): TRUE; (4, -5), (-2 ,3), (0,0): FALSE
Write a boolean expression for each of these
✤ Given two integer variables: n and m

✤ n is equal to 3 or 5 n==3 || n==5

✤ n is between 25 and 50, both exclusive n>25 && n<50

✤ n is below 15 or above 42

✤ n and m both are multiples of 3

✤ n is a multiple of 7 and m is negative

✤ n and m are both positive or both negative

✤ e.g., (4,5), (-2, -3): TRUE; (4, -5), (-2 ,3), (0,0): FALSE
Write a boolean expression for each of these
✤ Given two integer variables: n and m

✤ n is equal to 3 or 5 n==3 || n==5

✤ n is between 25 and 50, both exclusive n>25 && n<50

✤ n is below 15 or above 42 n < 15 || n > 42

✤ n and m both are multiples of 3

✤ n is a multiple of 7 and m is negative

✤ n and m are both positive or both negative

✤ e.g., (4,5), (-2, -3): TRUE; (4, -5), (-2 ,3), (0,0): FALSE
Write a boolean expression for each of these
✤ Given two integer variables: n and m

✤ n is equal to 3 or 5 n==3 || n==5

✤ n is between 25 and 50, both exclusive n>25 && n<50

✤ n is below 15 or above 42 n < 15 || n > 42

✤ n and m both are multiples of 3 n % 3 == 0 && m % 3 == 0

✤ n is a multiple of 7 and m is negative

✤ n and m are both positive or both negative

✤ e.g., (4,5), (-2, -3): TRUE; (4, -5), (-2 ,3), (0,0): FALSE
Write a boolean expression for each of these
✤ Given two integer variables: n and m

✤ n is equal to 3 or 5 n==3 || n==5

✤ n is between 25 and 50, both exclusive n>25 && n<50

✤ n is below 15 or above 42 n < 15 || n > 42

✤ n and m both are multiples of 3 n % 3 == 0 && m % 3 == 0

✤ n is a multiple of 7 and m is negative n % 7 == 0 && m < 0


✤ n and m are both positive or both negative

✤ e.g., (4,5), (-2, -3): TRUE; (4, -5), (-2 ,3), (0,0): FALSE
Write a boolean expression for each of these
✤ Given two integer variables: n and m

✤ n is equal to 3 or 5 n==3 || n==5

✤ n is between 25 and 50, both exclusive n>25 && n<50

✤ n is below 15 or above 42 n < 15 || n > 42

✤ n and m both are multiples of 3 n % 3 == 0 && m % 3 == 0

✤ n is a multiple of 7 and m is negative n % 7 == 0 && m < 0


✤ n and m are both positive or both negative n * m > 0
✤ e.g., (4,5), (-2, -3): TRUE; (4, -5), (-2 ,3), (0,0): FALSE
Variants of if-else: if (without else)

prev_stmt

prev_stmt
test
if (test) {
F
T if_block
if_block
}

next_stmt
next_stmt

Rewrite the max function without else


Rewrite the max function without else

int max(int first, int second) {


int max = first;
if (second > max) {
max = second;
}
return max;
}

Rewrite the max function without else

int max(int first, int second) { Does this code work as expected?
int max = first; Trace it with the test examples!
if (second > max) {
max = second;
}
return max;
}

Rewrite the max function without else

int max(int first, int second) {


int max = first;
if (second > max) {
max = second;
}
return max;
Sometimes a good choice of initial
}
value can make for simpler code

Variants of if-else: if-else chain

prev_stmt
prev_stmt
if (test_1) {
test_1
T
F if_block_1
} else if (test_2) {
test_2 if_block_2
if_block_1 } else {
T F
else_block
if_block_2 else_block }
next_stmt

next_stmt

Variants of if-else: if-else chain

prev_stmt
prev_stmt
if (test_1) {
test_1
T
F if_block_1
} else if (test_2) {
test_2 if_block_2
if_block_1 } else {
T F
else_block
if_block_2 else_block }
next_stmt
Any number of if’s can be
next_stmt chained like this

Revisit the Hogwarts task


✤ Hogwarts wants to grade their OWLs the muggle
way this year and has recruited you as a
programmer!

✤ Write a program that assigns grades to students


based on the following (simpli ed) scale

✤ Outstanding (O): 91-100

✤ Exceeds Expectations (E): 81-90

✤ Acceptable (A): 71-80

✤ Poor (P): 70 and below


fi
Revisit the Hogwarts task
✤ Hogwarts wants to grade their OWLs the muggle
way this year and has recruited you as a
programmer!
✤ Which if-else variant is suitable?
✤ Write a program that assigns grades to students
based on the following (simpli ed) scale ✤ if
✤ Outstanding (O): 91-100
✤ if-else
✤ Exceeds Expectations (E): 81-90
✤ if-else chain
✤ Acceptable (A): 71-80

✤ Poor (P): 70 and below


fi
Revisit the Hogwarts task
✤ Hogwarts wants to grade their OWLs the muggle
way this year and has recruited you as a
programmer!

✤ Write a program that assigns grades to students


based on the following (simpli ed) scale

✤ Outstanding (O): 91-100

✤ Exceeds Expectations (E): 81-90

✤ Acceptable (A): 71-80

✤ Poor (P): 70 and below


fi
char grade

Revisit the Hogwarts task


✤ Hogwarts wants to grade their OWLs the muggle
way this year and has recruited you as a
programmer!

✤ Write a program that assigns grades to students


based on the following (simpli ed) scale

✤ Outstanding (O): 91-100

✤ Exceeds Expectations (E): 81-90

✤ Acceptable (A): 71-80

✤ Poor (P): 70 and below


fi
char grade

Revisit the Hogwarts task marks>90

✤ Hogwarts wants to grade their OWLs the muggle


way this year and has recruited you as a
programmer!

✤ Write a program that assigns grades to students


based on the following (simpli ed) scale

✤ Outstanding (O): 91-100

✤ Exceeds Expectations (E): 81-90

✤ Acceptable (A): 71-80

✤ Poor (P): 70 and below


fi
char grade

Revisit the Hogwarts task marks>90

✤ Hogwarts wants to grade their OWLs the muggle


way this year and has recruited you as a grade='O'
programmer!

✤ Write a program that assigns grades to students


based on the following (simpli ed) scale

✤ Outstanding (O): 91-100

✤ Exceeds Expectations (E): 81-90

✤ Acceptable (A): 71-80

✤ Poor (P): 70 and below


fi
char grade

Revisit the Hogwarts task marks>90

✤ Hogwarts wants to grade their OWLs the muggle


way this year and has recruited you as a grade='O'
programmer!

✤ Write a program that assigns grades to students


based on the following (simpli ed) scale

✤ Outstanding (O): 91-100

✤ Exceeds Expectations (E): 81-90

✤ Acceptable (A): 71-80

✤ Poor (P): 70 and below return grade


fi
char grade

Revisit the Hogwarts task marks>90

✤ Hogwarts wants to grade their OWLs the muggle


way this year and has recruited you as a grade='O' marks>80
programmer!

✤ Write a program that assigns grades to students


based on the following (simpli ed) scale

✤ Outstanding (O): 91-100

✤ Exceeds Expectations (E): 81-90

✤ Acceptable (A): 71-80

✤ Poor (P): 70 and below return grade


fi
char grade

Revisit the Hogwarts task marks>90

✤ Hogwarts wants to grade their OWLs the muggle


way this year and has recruited you as a grade='O' marks>80
programmer!

✤ Write a program that assigns grades to students


grade='E'
based on the following (simpli ed) scale

✤ Outstanding (O): 91-100

✤ Exceeds Expectations (E): 81-90

✤ Acceptable (A): 71-80

✤ Poor (P): 70 and below return grade


fi
char grade

Revisit the Hogwarts task marks>90

✤ Hogwarts wants to grade their OWLs the muggle


way this year and has recruited you as a grade='O' marks>80
programmer!

✤ Write a program that assigns grades to students


grade='E'
based on the following (simpli ed) scale

✤ Outstanding (O): 91-100

✤ Exceeds Expectations (E): 81-90

✤ Acceptable (A): 71-80

✤ Poor (P): 70 and below return grade


fi
char grade

Revisit the Hogwarts task marks>90

✤ Hogwarts wants to grade their OWLs the muggle


way this year and has recruited you as a grade='O' marks>80
programmer!

✤ Write a program that assigns grades to students marks>70


grade='E'
based on the following (simpli ed) scale

✤ Outstanding (O): 91-100

✤ Exceeds Expectations (E): 81-90

✤ Acceptable (A): 71-80

✤ Poor (P): 70 and below return grade


fi
char grade

Revisit the Hogwarts task marks>90

✤ Hogwarts wants to grade their OWLs the muggle


way this year and has recruited you as a grade='O' marks>80
programmer!

✤ Write a program that assigns grades to students marks>70


grade='E'
based on the following (simpli ed) scale

✤ Outstanding (O): 91-100


grade='A'
✤ Exceeds Expectations (E): 81-90

✤ Acceptable (A): 71-80

✤ Poor (P): 70 and below return grade


fi
char grade

Revisit the Hogwarts task marks>90

✤ Hogwarts wants to grade their OWLs the muggle


way this year and has recruited you as a grade='O' marks>80
programmer!

✤ Write a program that assigns grades to students marks>70


grade='E'
based on the following (simpli ed) scale

✤ Outstanding (O): 91-100


grade='A'
✤ Exceeds Expectations (E): 81-90

✤ Acceptable (A): 71-80

✤ Poor (P): 70 and below return grade


fi
char grade

Revisit the Hogwarts task marks>90

✤ Hogwarts wants to grade their OWLs the muggle


way this year and has recruited you as a grade='O' marks>80
programmer!

✤ Write a program that assigns grades to students marks>70


grade='E'
based on the following (simpli ed) scale

✤ Outstanding (O): 91-100


grade='A'
✤ Exceeds Expectations (E): 81-90
grade='P'
✤ Acceptable (A): 71-80

✤ Poor (P): 70 and below return grade


fi
char grade

Revisit the Hogwarts task marks>90

✤ Hogwarts wants to grade their OWLs the muggle


way this year and has recruited you as a grade='O' marks>80
programmer!

✤ Write a program that assigns grades to students marks>70


grade='E'
based on the following (simpli ed) scale

✤ Outstanding (O): 91-100


grade='A'
✤ Exceeds Expectations (E): 81-90
grade='P'
✤ Acceptable (A): 71-80

✤ Poor (P): 70 and below return grade


fi
char grade

Revisit the Hogwarts task marks>90

grade='O' marks>80

grade='E' marks>70

grade='A'
grade='P'

return grade
char grade

Revisit the Hogwarts task marks>90

char grade(int marks) {


char grade; grade='O' marks>80
if (marks > 90) {
grade = 'O';
marks>70
} else if (marks > 80) { grade='E'
grade = 'E';
} else if (marks > 70) {
grade = 'A'; grade='A'
} else {
grade='P'
grade = 'P';
}
return grade;
return grade
}

char grade

Revisit the Hogwarts task marks>90

char grade(int marks) { Simplify using initialisation


char grade; grade='O' marks>80
if (marks > 90) {
grade = 'O';
marks>70
} else if (marks > 80) { grade='E'
grade = 'E';
} else if (marks > 70) {
grade = 'A'; grade='A'
} else {
grade='P'
grade = 'P';
}
return grade;
return grade
}

You might also like