Page 48 - Modular_V1.1_Flipbook
P. 48
break can be used inside switch body but cannot be used inside if body. For example,
void main() {
int n;
cout << "Enter any number : ";
cin >> n;
if (n==0) break;// wrong! Cannot use break with if
}
The above program when compiled will produce an error: Misplaced break.
MORE SOLVED PROGRAMS
Program 7: To find the largest in two numbers.
#include<iostream.h>
void main(){
int a, b;
cout<<"Enter two numbers: ";
cin>>a;
cin>>b;
if (a>b)
cout<<"a is greater than b";
else
cout<<"b is greater than a";
}
Program 8: To take a character as input and display the season name according to the input
character.
#include<iostream.h>
#include<conio.h>
void main(){
char code;
clrscr();
cout << "\n\t Enter a character ";
cin >> code;
switch (code){
case 'd': cout<<"\nThe season is dry";
break;
case 'w': cout<<"\nThe season is winter";
break;
case 'r': cout<<"\nThe season is rainy";
break;
default: cout<<"\nWrong code";
}
}
46 Touchpad MODULAR (Version 1.1)-X

