Control Statements
" NESTED IF " STATEMENT
#include <iostream>
using namespace std;
int main(){
int x, y, z;
cout << " Enter three numbers \n";
cin >> x >> y >> z ;
if ( x > y )
{
if (x >z) {
cout << " The greatest number is : " << x; }
else
cout << " The greatest number is : " <<z;
}
else {
if (y > z){
cout << " The greatest number is : "<< y; }
else {
cout<<" The greatest number is : " << z; }
}
}
OUTPUT
Enter three numbers
1
48
87
The greatest number is : 87
How It Works
Variable Declaration:
- The program starts by declaring three integer variables:
x,y, andz. These variables will store the three numbers entered by the user.
- The program starts by declaring three integer variables:
User Input:
- The program prompts the user to input three numbers. The
cinstatement reads these numbers from the standard input (usually the keyboard) and stores them in the variablesx,y, andz.
- The program prompts the user to input three numbers. The
Conditional Statements:
The
if-elsestatement is used to compare the three numbers and determine which one is the greatest.The first
ifchecks ifxis greater thany. If true, the program checks ifxis also greater thanz. If both conditions are true,xis the greatest number.If
xis not greater thanz, thenzmust be the greatest.If
xis not greater thany, the program then checks ifyis greater thanz. If true,yis the greatest number. If not,zis the greatest.
Key Concepts Demonstrated
Nested
ifStatements:- The program uses nested
ifstatements to handle the decision-making process. This nesting allows the program to consider multiple conditions in a structured and logical way.
- The program uses nested
User Input and Output:
- The
cinandcoutfunctions are essential for interacting with the user, taking input and displaying output. These are fundamental functions in C++ that you'll use frequently.
- The
Basic Logical Operations:
- The program employs simple logical comparisons (
>) to evaluate which of the three numbers is the largest. Understanding these basic operations is crucial as they form the backbone of more complex algorithms.
- The program employs simple logical comparisons (
Enhancing the Program
This program serves as an excellent foundation for understanding conditional logic and user interaction in C++. As you progress, you might explore how to handle equal values (e.g., if two or more numbers are the same) or how to use arrays and loops to handle more than three numbers.
Conclusion
This small yet powerful C++ program is an excellent exercise for beginners. It introduces you to important programming concepts such as conditional statements, user input, and output, which are vital for building more complex programs in the future. Happy coding!
"IF – ELSE IF – ELSE" STATEMENT
#include <iostream>
using namespace std;
int main() {
int marks;
cout << "Enter total marks : ";
cin >> marks;
if (marks >= 90)
cout << "A Grade";
else if (marks >= 80)
cout << "B Grade";
else if (marks >= 70)
cout << "C Grade";
else if (marks >= 60)
cout << "D Grade";
else
cout << "Failed";
return 0;
}
OUTPUT
Enter total marks : 98
A Grade
or
Enter total marks : 25
Failed
How the Program Works
Variable Declaration:
- The program starts by declaring an integer variable
marksto store the total marks entered by the user.
- The program starts by declaring an integer variable
User Input:
- The
coutstatement prompts the user to enter their total marks, and thecinstatement captures this input and stores it in themarksvariable.
- The
Conditional Statements:
The program uses a series of
if-else ifstatements to determine the grade based on the input marks.A Grade: If the marks are 90 or above, the program assigns an "A Grade".
B Grade: If the marks are 80 or above but less than 90, the program assigns a "B Grade".
C Grade: If the marks are 70 or above but less than 80, the program assigns a "C Grade".
D Grade: If the marks are 60 or above but less than 70, the program assigns a "D Grade".
Failed: If the marks are below 60, the program returns "Failed".
Program Termination:
- The
return 0;statement signals that the program has successfully completed its execution.
- The
Key Concepts Highlighted
Conditional Logic:
- The heart of this program lies in the use of conditional (
if-else if-else) statements, which allow the program to make decisions based on the user's input. This decision-making process is a cornerstone of programming, enabling more complex and responsive applications.
- The heart of this program lies in the use of conditional (
User Interaction:
- The program demonstrates basic input and output operations. By using
cinandcout, the program interacts with the user, making it dynamic and engaging.
- The program demonstrates basic input and output operations. By using
Simplicity and Clarity:
- This program is straightforward and easy to understand, making it a perfect example for beginners. It doesn't overwhelm with complexity, but rather, it reinforces the fundamentals.
Enhancements and Considerations
While this program is functional and achieves its purpose, there are ways it could be enhanced:
Input Validation: Currently, the program assumes that the user will enter valid marks. Adding input validation could make the program more robust, ensuring that only valid marks (e.g., non-negative integers within a certain range) are accepted.
Handling Multiple Students: The program could be extended to handle multiple students by using loops or arrays, making it more versatile in real-world applications.
Customized Grade Thresholds: Allowing the user to define their grade thresholds could make the program adaptable to different grading systems.



