Hey guys, what is up. In this particular post, we will be talking about switch statement in c++. Also about goto, break, continue and errors in c++.

Switch statement in c++

A switch statement in c++ is a multiple-choice selection statement. It will be executed according to the user choice. Also, each switch case must include the break keyword.

Syntax – 

switch (expression)

 {

Case 1: Statement 1;

                        break;

          Case 2: Statement 2;

                        break;

Case n: Statement n;

                        break;

          default: Statement;

                        break; 

}

Example of switch statement in c++

#include<iostream.h>

#include<conio.h>

void main()

{

           clrscr();

           int a;

  cout<<“enter your choice(1 or 2): “;

           cin>>a;

           switch(a)

           {

                   case 1:

                             cout<< “i am good”;

                             break;

                    case 2:

                             cout<< “i am bad”;

                             break;

                    default:

  cout<< “invalid choice”:

                              break;

          }

          getch();

}

Output:

output switch statement c++
output switch statement c++ 2

Break statement in c++

Break statements is used to terminate the loop. It is used in loops and switch cases. Keyword ‘break’ is used in the break statement. Through this statement, control is transferred outside the loop.

Example of break statement

#include<iostream.h>

#include<conio.h>

void main()

{

 clrscr();

          int n;

           for(n=1; n<10; n++)

 //loop is use here and we will give brief info about loops in the upcoming posts

{

                   cout<< n << ‘\t’;

                   if(n==7)

                   {

                             cout<< “countdown aborted!”;

                             break;

                   }

          }

          cout<< ‘\n’;

          getch();

}

Output:

swith statement c++ break

Continue statement in c++

 continue statement is used to continue the next iteration in the loop. It is only used in loops. The keyword ‘continue’ is used in the continue statement. Control goes to the starting of the loop.

Example of continue statement

#include<iostream.h>

#include<conio.h>

void main()

{

          int n;

          for(n=1; n<=10; n++)

          {

                   if(n==7)

                   {

                             continue;

                   }

                   cout<<n<<‘\t’; 

          } 

          getch();

}

Output: 

output continue

Goto statement in c++

Goto statement is a jump statement that is used to transfer the program control from one location to another location. 

Syntax

goto label;

—-

—-

label: 

Example of goto statement

#include<iostream.h>

#include<math.h>

void main()

{

          clrscr();

          int a;

          int b;

          tryagain: cout<<“enter a positive number: “;

          cin>>a;

          b=sqrt(a); //square root is represented by sqrt it is pre-defined in math.h

if(a<0)

          {

                   goto tryagain;

          }

          cout<<“square root of number is “<<b;

          getch();

}

Output:

output switch statement c++ goto

Errors/bugs in c++

Whenever a program is unable to perform its desired task then it contains an error/bug.

Types of errors:

  • Syntax error: These errors occurs when programming language rules are violated. For example, a semicolon is missing. They always occur at compilation time hence they are called compile-time errors.
  • Semantics error: An error occurs when a statement is not meaningful. For instance, n+m=x.
  • Type error: C++ defines all errors of a particular datatype when one type of variable is used or given a different type of value then it results in a type error. For example, n=9.0 where n is defined using integer datatype
  • Runtime error/execution error: Errors occurring during execution are runtime errors. For instance, n=5,m=0; n/m=error.
  • Logical error: It occurs when incorrect logic is used. The programmer is able to locate such an error after seeing the wrong output. For example: subtract 2 numbers but the answer shows the multiplication of 2 numbers. These errors are not pointed by the compilers.

In the upcoming post, you will be getting information related to technology. If you haven’t read about if-else in c++, then go and read this. You can also read previous posts by scrolling downwards. Also, stay tuned for the upcoming post

Categories:

1 Comment

Eekshit Chawla · April 25, 2021 at 1:08 pm

Thankyou for this

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *