TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
Program to illustrate Switch, Goto and Exit statements.
1. PROGRAM TO CONTINUE CALCULATION TILL USER WANTS. USING SWITCH , EXIT AND GOTO
STATEMENTS.
#include<iostream.h> //used for cout and cin
#include<conio.h> //used for getch() and clrscr()
#include<stdlib.h> //used for exit()
void main()
{
char a;
int x, y,ch;
clrscr();
start: //linked with goto statement
cout<<"n Enter the value of x:";
cin>>x;
cout<<"n Enter the value of y:";
cin>>y;
cout<<"n Enter your choice: n 1: sum n 2: sub n 3: mul n 4 div";
cin>>ch;
switch(ch)
{
case 1:
cout<<"n Sum of x and y is:"<<x+y;
break;
case 2:
cout<<"n Sub of x and y is:"<<x-y;
break;
case 3:
cout<<"n Mul of x and y is:"<<x*y;
break;
case 4:
cout<<"n Div of x and y is:"<<x/y;
break;
default :
cout<<"Thankyou";
}
cout<<"n Enter Y to continue";
cin>>a;
if(a=='y' ||a=='Y')
{
goto start; //goto the position where “start” is defined(starting of program).
}
else
{
exit(0);
}
getch();
}