Sunday, February 18, 2024

INTRODUCTION TO CONDITIONAL AND CONTROL STATEMENTS IN C++

Introduction to conditional and control statements in C++


  • INTRODUCTION TO CONDITIONAL AND SCRATCH STATEMENTS 
  • FEATURES OF SWITCH STATEMENT
  • LIMITATION OF THE SWITCH STATEMENT
  • CONCLUSION

The flow of the program is controlled by conditional statements in C++. If the condition is true within the if statement, the body of the statement is executed. If false, else is used to specify an alternative course of action. You can check multiple criteria in a row using the else-if statement and learn C++ from scratch. Use a switch for multidirectional branching based on the result of an expression.

There are a few types of conditional statements such as:

·       If

·       If else

·       If else if

·       Nested if

·       Switch statements

 

    1)      If statements

 

It is only being executed when the condition is true.

For better understanding let’s give a try on program with syntax.

 

Syntax:

If(condition)

{

(code to be executed)

}

 

Let us try a program:

#include <iostream>  

using namespace std;  

   

int main () {  

   int num = 10;    

            if (num % 2 == 0)    

            {    

                cout<<"It is an even number";    

            }   

   return 0;  

}  

 

Output

It is an even number

 

    2)      If-else statement

The if-else statement also tests the condition. It is executed when the block if the condition is true otherwise the block is executed.

 

For better understanding let’s give a try on program with syntax

Syntax:

 If(condition)

{

(code to be executed-true)

}

Else

{

(code to be executed-false)

}

 

Let us try one program for it

#include <iostream>  

using namespace std;  

int main () {  

   int num = 11;    

            if (num % 2 == 0)    

            {    

                cout<<"It is even number";    

            }   

            else  

            {    

                cout<<"It is odd number";    

            }  

   return 0;  

}  

 

Output

It is odd number

 

    3)   If else if ladder

This conditional statement executes one condition from multiple statements

 

For better understanding let’s give a try on program with syntax

Syntax:

 

if(condition1){    

//code to be executed if condition1 is true    

}else if(condition2){    

//code to be executed if condition2 is true    

}    

else if(condition3){    

//code to be executed if condition3 is true    

}    

...    

else{    

//code to be executed if all the conditions are false    

}   

 

Program

#include <iostream>  

using namespace std;  

int main () {  

       int num;  

       cout<<"Enter a number to check grade:";    

       cin>>num;  

            if (num <0 || num >100)    

            {    

                cout<<"wrong number";    

            }    

            else if(num >= 0 && num < 50){    

                cout<<"Fail";    

            }    

            else if (num >= 50 && num < 60)    

            {    

                cout<<"D Grade";    

            }    

            else if (num >= 60 && num < 70)    

            {    

                cout<<"C Grade";    

            }    

            else if (num >= 70 && num < 80)    

            {    

                cout<<"B Grade";    

            }    

            else if (num >= 80 && num < 90)    

            {    

                cout<<"A Grade";    

            }    

            else if (num >= 90 && num <= 100)    

            {    

                cout<<"A+ Grade";  

            }    

    }    

 

 

Output

Enter the number to check grade

66

Grade C

Enter the number to check grade

-2

Wrong number

 4)   Switch statements

A switch statement is a powerful control structure in C++ that lets you run several code sections depending on the outcome of an expression. When you have to choose among several possibilities, it offers an advanced and effective substitute for sequential if-else statements.

The C++ switch executes one statement out of several conditions. It is like an if-else-if ladder statement in C++.

 

For better understanding let’s give a try on program with syntax

Syntax:

switch(expression){      

case value1:      

 //code to be executed;      

 break;    

case value2:      

 //code to be executed;      

 break;    

......      

      

default:       

 //code to be executed if all cases are not matched;      

 break;    

}    

 


 

Program

 

#include <iostream>  

using namespace std;  

int main () {  

       int num;  

       cout<<"Enter a number to check grade:";    

       cin>>num;  

           switch (num)    

          {    

              case 10: cout<<"It is 10"; break;    

              case 20: cout<<"It is 20"; break;    

              case 30: cout<<"It is 30"; break;    

              default: cout<<"Not 10, 20 or 30"; break;    

          }    

    }   

Output

Enter the number

10

It is 10

Enter the number

50

It is not 10,20 or 30


Features of the switch statement

 

The Switch statement has several functions in C++. Some of the main features of the C switch statement are:

 

The falling behavior of the C++ switch is one of its most important features. Control passes to the next case unless a break statement is used to terminate the case block. After that, subsequent cases are processed until an interruption is detected or the end of the switch block is reached. This feature can be intentionally used to share common code in multiple scenarios.

 

The Switch statement and ability to simplify code readability and maintainability is one of its main advantages. Comparing a series of nested if-else statements with a switch can create clearer and more organized code in many situations. Each case entry gives the program a unique and unambiguous path to follow, improving the code base and overall readability. This is very useful when working with large and complex programs where maintaining logical flow is critical for readability. This is very useful when working with large and complex programs where maintaining logical flow is critical.

 

Another significant benefit of a change clause is efficiency. If done correctly, a switch can often be more effective than an if-else-if sequence. That efficiency is due to the compiler's ability to optimize the switch to produce more efficient machine code, which can result in faster execution time. It is important to note that the actual speed improvement may vary depending on conditions and compilers.

 

Limitations of the switch statement

 

The switch in C++ has several limitations. Some of the main limitations of the switch in C are:

The switch has several limitations, so it is important to know them and the industry standards. For example, switch expression and expression must be of integral or enumeration. This limits its ability to work with other data types such as strings or floating-point integers. Additionally, variables or expressions cannot be used as case identifiers, because each case identifier must reflect a constant value known at compile time.

 

The best practice is to add a default handler to the switch to fully cover cases. This case handles cases where none of the previous cases match the value of the expression. When none of the predefined situations apply, the included default case prevents unexpected behavior and provides a clear path to action.

 

Conclusion

 

The C++ switch is a flexible construct that makes it easy for programs to handle different scenarios. It's clear letter notation and concise syntax make the code easy to understand and maintain, especially with many possible outcomes. The switch improves the organization of program logic by providing an immediate mapping between cases and operations.

 

The switch has performance advantages over if-else-if ladders because the compiler can optimize it for faster execution. Developers should be aware of its limitations, such as the need for integral or enumerated expression types and constant case values.

 

It is recommended to include a default handler in the switch to handle inappropriate conditions and for efficient and elegant handling. By following best practices and understanding its complexity, developers can take advantage of the switch and its benefits to create more organized, efficient, and understandable C++ code

No comments:

Post a Comment

Featured Post

ASSOCIATION RULE IN MACHINE LEARNING/PYTHON/ARTIFICIAL INTELLIGENCE

Association rule   Rule Evaluation Metrics Applications of Association Rule Learning Advantages of Association Rule Mining Disadvantages of ...

Popular