Marks Based Division
Finding division of a student based on marks using if-else-if ladder
Learning Objectives
- To use if-else-if ladder to user mutually exclusive range of marks to display messages related to conditions.
- Use of const modifier for case of maximum marks.
Source Code
|
|
Run Output
Code Understanding
const int MM = 50;
MM or maximum marks has been given a const modifier to an int as we do not want that maximum marks to be modified anywhere during the program flow.
per = marks/MM*100;
Calculating percentage based on marks entered by the user. Since all variables are of float type, this expression will work well. If marks are given in integer then this will not yield correct result.
if (per >=60) cout << “You Got Ist Div.”;
This is first condition for declaring a student as 1st division.
else if (per <60 && per >=45) cout << “You Got IInd Div.”;
This is second if-ladder condition for marks between the range 60 to 45 for declaring IInd division.
else if (per <45 && per >=33) cout << “You Got IIIrd Div.”;
This is third level check of if-else ladder for range 45 to 33 to declare IIIrd Division.
else cout << “Sorry! you are fail!”;
Finally this the leftoer range of below 33% too display the fail condition
Notes
- Mutually exclusive ranges are important for creating a if-else-if ladder.
Common Errors
- Marks ranges are set in a way that no particular marks create confusing decision state. For. e.g. if the condition is like this –
if (per >=60) cout << “You Got Ist Div.”;
else if (per <=60 && per >=45) cout << “You Got IInd Div.”;Then the marks 60 will actually produce 1st Division but as per logic it might fall in second division case also. - Some student do not test their programs for all test-cases. If it is not done then errors on boundary values are most common. The boundary values in terms of percentage for this example are 59, 60,61, 44,45,46, 32,33,34 . You can note that these values are actually given test boundaries with 1 plus and 1 minus value. Boundary value testing ensures correct use of comparison operators.
Suggested Filename(s): marks-based-division.cpp, division-rank.cpp
Created: 27-Dec-2017 02:17 | Updated: 1-Sep-2026 16:38|
