Skip to main content

Command Palette

Search for a command to run...

Control Statements

Updated
5 min readView as Markdown

" 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

  1. Variable Declaration:

    • The program starts by declaring three integer variables: x, y, and z. These variables will store the three numbers entered by the user.
  2. User Input:

    • The program prompts the user to input three numbers. The cin statement reads these numbers from the standard input (usually the keyboard) and stores them in the variables x, y, and z.
  3. Conditional Statements:

    • The if-else statement is used to compare the three numbers and determine which one is the greatest.

    • The first if checks if x is greater than y. If true, the program checks if x is also greater than z. If both conditions are true, x is the greatest number.

    • If x is not greater than z, then z must be the greatest.

    • If x is not greater than y, the program then checks if y is greater than z. If true, y is the greatest number. If not, z is the greatest.

Key Concepts Demonstrated

  1. Nested if Statements:

    • The program uses nested if statements to handle the decision-making process. This nesting allows the program to consider multiple conditions in a structured and logical way.
  2. User Input and Output:

    • The cin and cout functions are essential for interacting with the user, taking input and displaying output. These are fundamental functions in C++ that you'll use frequently.
  3. 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.

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

  1. Variable Declaration:

    • The program starts by declaring an integer variable marks to store the total marks entered by the user.
  2. User Input:

    • The cout statement prompts the user to enter their total marks, and the cin statement captures this input and stores it in the marks variable.
  3. Conditional Statements:

    • The program uses a series of if-else if statements 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".

  4. Program Termination:

    • The return 0; statement signals that the program has successfully completed its execution.

Key Concepts Highlighted

  1. 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.
  2. User Interaction:

    • The program demonstrates basic input and output operations. By using cin and cout, the program interacts with the user, making it dynamic and engaging.
  3. 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.