C++ is one of the most versatile languages in the world. It is used nearly everywhere for everything… systems programming (operating systems, device drivers, database engines, embedded, Internet of Things, etc.)

Enter Header Image Headline Here

Saturday, 15 April 2017

C++ switch...case Statement

                               C++ switch...case Statement


The nested if ...else statement allows you to execute a block code among many alternatives. if you are checking on the value of a single variable in nested if.... else statement, it is better to use switch statement.

The switch statement is often faster than nested if....else (not always). Also, the syntax of switch statement is cleaner and easier to understand.


C++ switch......case syntax



When a case constant is found that matches the switch expression, control of the program passes to the block of code associated with that case.

In the above pseudocode, suppouse the value of n is equal to constant2. The compiler will execute the block of code associated with the case statement until the end of switch block, or until the  break ststement is encountered.

The break statement is used to prevent the code running in to the next case.

Flowchart of switch Statement




The  above figur shows how a switch statement works and conditions are checked within the switch case clause.

Example: C++ switch Statement





Output













The - operator entered by the user is stored in o variable. And, two operaands 2.3 and 4.5 are stored in variables num1 and num2 respectively.


Then the control of the program jumps to



Finally, the break statement ends the switch statement.

if  break statement is not used, all cases after the correct case is executed.






C++ goto Statement

                                       C++ goto Statement


In C++ programming, goto statement is user for altering the normal sequence of program execution by transferring control to some other part of the program,

Syntax of goto Statement





In the syntax above, lebel is an identifier. When goto lebel; is encountered, the control of program jumps to label: and executes the code below it.

Example : goto Statement









Output



You can write any C++ program without the use of goto statement and is generally considered a good idea not to use them.

Reason to Avoid goto Statement

The goto statement gives power to jump to any part of program but, mmakes the logic of the program complex and tangled.

In modern programming, goto statement is considered a harmful construct and a bed programming practice.

The goto statement can be replaced in most of C++ program with the use of break  ststement and continue ststement.





C++ continue Statement

                              C++ continue Statement


It is Sometimes necessary to skip a certain test condition within a loop. In such case, continue; statement is used in C++ programming.

Syntax  of  continue:











In practice, continue; statement is almost always used inside a conditional statement.

Working of continue Statement : 


Example : C++ continue


C++ program to display integer from 1 to 10 except 6 and 9.

















Output :




In above program, when i is 6 or 9, execution of statement cout << i << "\t"; is skipped inside the loop using continue; statement.





C++ break Statement

                                      C++ break Statement


In C++, there are two statements break; and continue; specifically to alter the normal flow of a program.

Sometimes, it is desirable to skip the execution of a loop for a certain test condition or terminate it immediately without checking the condition.

For example : Yoy want to loop through data of all aged people except people aged 65. Or you wnat to find the person aged 20.

In Scenarios like these , continue; or a break; statement is used.

C++ break Statement

The break; statement terminates a loop (for, while and do while loop) and a switch statement immediately when it appears.

Syntax of break

break;

In real practice, break statement is almost always used inside the body of conditional statement (if... else) inside the loop.

How break statement works?


Example : C++ break

C++ program to add all number entered by user until user enters 0







































Output



In the above program, the test expression is always true.

The user is asked to enter a number which is stored in the variable number. if the user enters any number other than 0, the number is added to sum and stored to it.

Again, the user is asked to enter another number. When the user enters 0, the test expression inside if statement is false and body of else is executed which terminates the loop.

Finally, the sum is displayed.

C++ Nested if....else

                                      C++ Nested if....else


The if.....else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities.

The nested if...else statement executes allows you to check for multiple test expressions and execute different codes for more than two conditions.


Syntax of Nested if...else









C++   Nested if....else







































Output:







Conditional/Ternary Operator ?:

A ternary operator operates on three operand which can be used instead of if..else statement.

Consider this code:
















You can replace the above code with:

       a = ( a < b ) ? b : -b;

Ternary operator is more readable than a    if...else     statement for short conditions.


Popular Posts

Recent Posts

Categories

Unordered List

Text Widget