COMPUTER PROGRAMING
The C++ programming language has a history going back to 1979, when Bjarne Stroustrup was doing work for his Ph.D. thesis. One of the languages Stroustrup had the opportunity to work with was a language called Simula, which as the name implies is a language primarily designed for simulations. The Simula 67 language - which was the variant that Stroustrup worked with - is regarded as the first language to support the object-oriented programming paradigm. Stroustrup found that this paradigm was very useful for software development, however the Simula language was far too slow for practical use.
Shortly thereafter, he began work on "C with Classes", which as the name implies was meant to be a superset of the C language. His goal was to add object-oriented programming into the C language, which was and still is a language well-respected for its portability without sacrificing speed or low-level functionality. His language included classes, basic inheritance, inlining, default function arguments, and strong type checking in addition to all the features of the C language.
The first C with Classes compiler was called Cfront, which was derived from a C compiler called CPre. It was a program designed to translate C with Classes code to ordinary C. A rather interesting point worth noting is that Cfront was written mostly in C with Classes, making it a self-hosting compiler (a compiler that can compile itself). Cfront would later be abandoned in 1993 after it became difficult to integrate new features into it, namely C++ exceptions. Nonetheless, Cfront made a huge impact on the implementations of future compilers and on the Unix operating system.
In 1983, the name of the language was changed from C with Classes to C++. The ++ operator in the C language is an operator for incrementing a variable, which gives some insight into how Stroustrup regarded the language. Many new features were added around this time, the most notable of which are virtual functions, function overloading, references with the & symbol, the const keyword, and single-line comments using two forward slashes (which is a feature taken from the language BCPL).
In 1985, Stroustrup's reference to the language entitled The C++ Programming Language was published. That same year, C++ was implemented as a commercial product. The language was not officially standardized yet, making the book a very important reference. The language was updated again in 1989 to include protected and static members, as well as inheritance from several classes.
In 1990, The Annotated C++ Reference Manual was released. The same year, Borland's Turbo C++ compiler would be released as a commercial product. Turbo C++ added a plethora of additional libraries which would have a considerable impact on C++'s development. Although Turbo C++'s last stable release was in 2006, the compiler is still widely used.
In 1998, the C++ standards committee published the first international standard for C++ ISO/IEC 14882:1998, which would be informally known as C++98. The Annotated C++ Reference Manual was said to be a large influence in the development of the standard. The Standard Template Library, which began its conceptual development in 1979, was also included. In 2003, the committee responded to multiple problems that were reported with their 1998 standard, and revised it accordingly. The changed language was dubbed C++03.
In 2005, the C++ standards committee released a technical report (dubbed TR1) detailing various features they were planning to add to the latest C++ standard. The new standard was informally dubbed C++0x as it was expected to be released sometime before the end of the first decade. Ironically, however, the new standard would not be released until mid-2011. Several technical reports were released up until then, and some compilers began adding experimental support for the new features.
In mid-2011, the new C++ standard (dubbed C++11) was finished. The Boost library project made a considerable impact on the new standard, and some of the new modules were derived directly from the corresponding Boost libraries. Some of the new features included regular expression support (details on regular expressions may be found here), a comprehensive randomization library, a new C++ time library, atomics support, a standard threading library (which up until 2011 both C and C++ were lacking), a new for loop syntax providing functionality similar to foreach loops in certain other languages, the auto keyword, new container classes, better support for unions and array-initialization lists, and variadic templates.
Written by Albatross.
1.How to convert franhiet into celciss.
Shortly thereafter, he began work on "C with Classes", which as the name implies was meant to be a superset of the C language. His goal was to add object-oriented programming into the C language, which was and still is a language well-respected for its portability without sacrificing speed or low-level functionality. His language included classes, basic inheritance, inlining, default function arguments, and strong type checking in addition to all the features of the C language.
The first C with Classes compiler was called Cfront, which was derived from a C compiler called CPre. It was a program designed to translate C with Classes code to ordinary C. A rather interesting point worth noting is that Cfront was written mostly in C with Classes, making it a self-hosting compiler (a compiler that can compile itself). Cfront would later be abandoned in 1993 after it became difficult to integrate new features into it, namely C++ exceptions. Nonetheless, Cfront made a huge impact on the implementations of future compilers and on the Unix operating system.
In 1983, the name of the language was changed from C with Classes to C++. The ++ operator in the C language is an operator for incrementing a variable, which gives some insight into how Stroustrup regarded the language. Many new features were added around this time, the most notable of which are virtual functions, function overloading, references with the & symbol, the const keyword, and single-line comments using two forward slashes (which is a feature taken from the language BCPL).
In 1985, Stroustrup's reference to the language entitled The C++ Programming Language was published. That same year, C++ was implemented as a commercial product. The language was not officially standardized yet, making the book a very important reference. The language was updated again in 1989 to include protected and static members, as well as inheritance from several classes.
In 1990, The Annotated C++ Reference Manual was released. The same year, Borland's Turbo C++ compiler would be released as a commercial product. Turbo C++ added a plethora of additional libraries which would have a considerable impact on C++'s development. Although Turbo C++'s last stable release was in 2006, the compiler is still widely used.
In 1998, the C++ standards committee published the first international standard for C++ ISO/IEC 14882:1998, which would be informally known as C++98. The Annotated C++ Reference Manual was said to be a large influence in the development of the standard. The Standard Template Library, which began its conceptual development in 1979, was also included. In 2003, the committee responded to multiple problems that were reported with their 1998 standard, and revised it accordingly. The changed language was dubbed C++03.
In 2005, the C++ standards committee released a technical report (dubbed TR1) detailing various features they were planning to add to the latest C++ standard. The new standard was informally dubbed C++0x as it was expected to be released sometime before the end of the first decade. Ironically, however, the new standard would not be released until mid-2011. Several technical reports were released up until then, and some compilers began adding experimental support for the new features.
In mid-2011, the new C++ standard (dubbed C++11) was finished. The Boost library project made a considerable impact on the new standard, and some of the new modules were derived directly from the corresponding Boost libraries. Some of the new features included regular expression support (details on regular expressions may be found here), a comprehensive randomization library, a new C++ time library, atomics support, a standard threading library (which up until 2011 both C and C++ were lacking), a new for loop syntax providing functionality similar to foreach loops in certain other languages, the auto keyword, new container classes, better support for unions and array-initialization lists, and variadic templates.
Written by Albatross.
1.How to convert franhiet into celciss.
#include
<iostream>
using
namespace std;
main ()
{
int f,c;
cout<<"enter
the of f =";
cin>>f;
c=(f-32)*9/5;
cout<<"the
value of c"<<c;
}
2.How to find average
of five subject??
#include
<iostream>
using namespace std;
main ()
{
int math,eng,computer,urdu,ist,avg;
cout<<"enter
the mark of math sub =";
cin>>math;
cout<<"enter
the mark of eng sun =";
cin>>eng;
cout<<"enter
the mark of computer =";
cin>>computer;
cout<<"enter
the mark of urdu =";
cin>>urdu;
cout<<"enter
mark of ist =";
cin>>ist;
avg
=(math+eng+computer+urdu+ist)/5;
cout<<"avg
is:"<<avg;
}
3.How to the volume of
cylinder???
#include <iostream>
using namespace std;
main ()
{
float h,r,vol;
cout<<"enter the vlaue of
h=";
cin>>h;
cout<<"enter the vlaue of r =";
cin>>r;
vol=3.14*h*r*r;
cout<<"the of vol
="<<vol;
}
4.How to fine age to
year,month,days??
#include <iostream>
using namespace std;
main ()
{
int age,month,year,day;
cout<<"enter the vlaue of
age =";
cin>>age;
year=age;
month=year*12;
cout<<"enter the month
="<<month<<endle;
day=month*30;
cout<<"age in day
="<<day;
}
5.find the area of
circle???
#include <iostream>
using namespace std;
main ()
{
float r,area;
cout<<"entre the vlaue of r =";
cin>>r;
area=3.14*r*r;
cout<<"The area of circle ="<<area;
}
6.convert salary in dollary??
#
include <iostream>
using
namespace std;
main()
{
int
salary;
float
dollar;
cout<<"Enter
the salary =";
cin>>salary;
dollar
= salary/105;
cout<<"salary
in dollar ="<<dollar;
}
7.how to
find mark of subject??
#include<iostream>
using
namespace std;
main
();
{
cout<<"subjeci"<<"\mark\t"<<"\nenglish\t"<<78<<"\nmath\t"<<90;
}
Q.8 Write a program in which accept principle,rate and time
from user and printe simple intrest.
#include<iostream>
using
namespace std;
main()
{
int
pi,r,t,i;
cout<<"enter
the principle =";
cin>>pi;
cout<<"enter
the rate =";
cin>>r;
cout<<"enter
the time =";
cin>>t;
i=(pi*r*t)/100;
cout<<"simple
intrest in : "<<i;
}
9.write a
programe which accept character and display its ASCII values.
#include<iostream>
using
namespace std;
main()
{
char
ch;
cout<<"enter
the character :";
cin>>ch;
cout<<"
ASCII is equilant to :"<<static_cast<int>(ch);
}
10.Write a
program to swap the value of two programs??
#include<iostream>
using namespace std;
main ()
{
int a,b,temp;
cout<<"enter two
values :";
cin>>a>>b;
temp=a;
a=b;
b=temp;
cout<<"After swapping the
number :";
cout<<a<<" "<<b;
}
11.write a
program to check whether the number positvie or negative(using ? :ternary
operator).
#include<iostream>;
using namespace std:
main ()
{
int a;
cout<<"enter two non integer numbers
:";
cin>>a;
(a)?cout<<"positive
number":<<"negative number";
}
12.how to
swapping the two values without use third varainle??
#include<iostream>
using
namespace std;
main()
{
int
a,b;
cout<<"enter
the two values :";
cin>>a>>b;
a=a+b;
b=a-b;
a=a-b;
cout<<"After
swapping the values :";
cout<<a<<"
"<<b;
}
}
13.write a program which give three
input and find the greatest number??
#
include <iostream>
using
namespace std;
main()
{
int
a,b,c,greater;
cout<<"enter
the three values :";
cin>>a>>b>>c;
greater=(a>b&&a>c)?a:(b>c)?b:c;
cout<<"the
greatest vlaue is ="<<greater;
}
14.write a program
which accept as integer and dispay total number of Notes 500,100,50,20,10,5 and
1.
# inlude
<iostream>
using
namespace std;
main()
{
int amount,R500,R100,R50,R20,R10,R5,R1;
cout<<"enter
the amount : ";
R5=amount/500;
amount=amount%500;
R100=amount/100;
amount=amount%100;
R50=amount/50;
amount=amount%50;
R20=amount/20;
amount=amount%20;
R10=amount/10;
amount=amount%10;
R5=amount/5;
amount=amount%5;
amount=R1;
cout<<"R500
: "<<R500<<"R100 : "<<R100<<"R50 :
"<<R50<<"R20 : "<<R20<<"R10 :
"<<R10<<"R5 : "<<R5<<" R1 :
"<<R1;
}
15.Write a program to
accept days in integer and display total number of year,month and day in it.
# include
<iostream>
Using namespace std;
Main()
{
Int day,y,m,d;
Cout<<” Enter day
:”;
Cin>>day;
Y=day/365;
day=day%365;
m=day/30;
days=day%30;
cout<<” Year :
“<<y<<”Month :”<<m<<”Day :”<<d;
}
16. In a town the
percentage of men is 52.The percentage of total literacy is 48.The percentage
of literate men is 35 of the total population.
Write a program to find
the total number of illiterate woman and men .If the population of the town is
80000.
# include
<iostream>
Using namespace std;
Main()
{
Int population men,
population women, literacy, literate men, literate woman, illiterate men,
illiterate woman;
Population men= (80000*52)/100;
Cout<<”population of men in
town = “<<population men;
Population woman=80000-population
men;
Cout<<”population of women in
town = “<<population woman;
Literacy= (80000*48)/100;
Cout<<”total literacy in town =
“<<literacy;
Literate men= (80000*35)/100;
Cout<<”literate men in town
=”<<literate men;
Illiterate men = population men –
literate men;
Cout<<”illiterate man in town =
“<<illiterate;
Literate women = literacy –literate
men;
Cout<<”literate woman in town =
“<<literate woman;
Illiterate woman = population woman
–literate woman;
Cout<<”illiterate woman in town
= “<<illiterate woman;
}
17.In five –digits number is input
throught the keybord. Write a prpgram to reverses the numbers.
# include <iostream>
using namespace std;
main()
{
int
num,d4,d3,d2,d1;
cout<<"enter
the number = ";
cin>>num;
d4=num%10;
num=num/10;
d3=num%10;
num=num/10;
d2=num%10;
num=num/10;
cout<<d4<<d3<<d2<<num;
}
18.If a four digits input throught
the keyboard . write a program to calculate the sum of its digits.
# include <iostream>
using namespace std;
main()
{
int
num,d4,d3,d2,d1,sum;
cout<<"enter
the number = ";
cin>>num;
d4=num%10;
num=num/10;
d3=num%10;
num=num/10;
d2=num%10;
num=num/10;
cout<<d4<<d3<<d2<<num;
sum=num+d4+d3+d2;
cout<<"\nsum
= "<<sum;
}
18. if total selling price of 15 items and total profits
earned on them is input through the keyboard ,write a program to find the cost
price of on item.
#include<iostream>
using namespace std;
main()
{
float cost, sale, costitem, profit;
cout<<"Please enter total selling price of 15 items: ";
cin>>sale;
cout<<"\nNow enter total profit on 15 items: ";
cin>>profit;
cost = sale - profit;
costitem = cost / 15;
cout<<"\nCost price per item: "<<costitem;
}
1. Write a program to find the factorial value of any number entered through the keyboard
#include<iostream>
using namespace std;
int main()
{
int n,fact=1;
cout<<"Enter any number : ";
cin>>n;
while(n>=1)
{
fact*=n;
n--;
}
cout<<"Factorial :"<<fact;
}
2. Write a program to print out all Armstrong numbers between 1 and 500. If sum of cubes of each digit of the number is equal to the number itself, then the number is called an Armstrong number. For example, 153 = ( 1 * 1 * 1 ) + ( 5 * 5 * 5 ) + ( 3 * 3 * 3 )
#include<iostream>
using namespace std;
int main()
{
int n,digit1,digit2,digit3;
for(int i=1;i<=500;i++)
{
digit1=i/100;
digit2=i/10 - digit1*10;
digit3=i%10;
if(digit1*digit1*digit1 + digit2*digit2*digit2 + digit3*digit3*digit3 == i)
cout<<i<<endl;
}
}
3. Write a program to print all prime numbers from 1 to 300.
int i,j,d=0;
cout<<"Display the prime number 1 to 300 "<<endl;
for(i=1;i<=300;i++)
{
for(j=2;j<=i/2;j++)
{
if(i%j==0)
{
d++;
break;
}}
if(d==0)
cout<<"\t \t"<<i<<"\n \n";
d=0;
}}
4. Write a program to print the multiplication table of the number entered by the user
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter a positive integer: ";
cin >> n;
for (int i = 1; i <= 10; ++i) {
cout << n << " * " << i << " = " << n * i << endl;
}
}
5. Write a program to generate Fibonacci number up to 500. Consider the following sequence of numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34,....
# include <iostream>
using namespace std;
main()
{
int first=0,second=1,third;
for(int i=0;i<500;i++)
{
if(i<=1)
{
third=i;
}
else
{
third=first+second;
first=second;
second=third;
}
cout<<third<<" ";
}}
6. Write a program using loop to read five numbers and finds their sum and average.
#include <iostream>
using namespace std;
main()
{
int i,n,sum=0;
float avg;
cout<<"Input the 5 numbers : \n";
for (i=1;i<=5;i++)
{
cout<<"Number is :"<<i<<endl;
cin>>n;
sum =sum+n;
}
avg=sum/5;
cout<<"The sum of 5 no is : "<<sum<<"\nThe Average is : \n"<<avg<<endl;
}
7. Write a program to finds the sum of the first n positive integers
#include <iostream>
using namespace std;
main()
{
int n, count, sum = 0;
cout<<"Enter the value of n(positive integer: ";
cin>>n;
for(count=1; count <= n; count++)
{
sum = sum + count;
}
cout<<"Sum of first n natural numbers is: "<< sum;
}
8. Write a program to finds the sum as many positive integers as user want to enter. Use break and continue statement.
# include <iostream>
using namespace std;
int main()
{
int i;
double n, sum = 0.0;
for(i=1; i <= 10; ++i)
{
cout<<"Enter number: ";
cin>>n;
// If user enters negative number, loop is terminated
if(n < 0.0)
{
break;
}
sum += n; // sum = sum + number;
}
cout<<"Sum = "<<sum;
return 0;
}
9. Write program that Counts zeros, odds, and evens using loop. This program counts the number of odd and even numbers. The program also counts the number of zeros
#include<iostream>
using namespace std ;
main()
{
int odd=0;
int zero=0;
int even=0;
int i;
int num;
for(i=1;i<=5;i++)
{
cout<<"enter the number:"<<endl;
cin>>num;
if(num!=0 && num%2==0)
{
even++;
}
else if(zero==0)
{
zero++;
}
else
odd++;
}
cout<<"even number:"<<even <<endl;
cout<<"zero number:"<<zero <<endl;
cout<<"odd number:"<<odd <<endl;
}
10. Write a program to find ever or odd numbers up-to nth positive number
#include<iostream>
using namespace std ;
main()
{
int i,num;
for(i=num;i>0;i--)
{
cout<<"enter the number:";
cin>>num;
if(num>0 && num%2==0)
cout<<"number is even"<<endl;
else if(num>0 && num%2!=0)
cout<<"number is odd"<<endl;
else
break;
}
11. Write a program to take 10 digit number as input taken through the keyboard, write a program to obtain the sum of all number.
#include <iostream>
using namespace std;
main()
{
long num, temp, digit, sum = 0;
cout<<"Enter the number ";
cin>>num;
temp = num;
while (num > 0)
{
digit = num % 10;
sum = sum + digit;
num /= 10;
}
cout<<"Given number = "<<temp;
cout<<"\nSum of the digits = "<<sum;
}
13. Write a program to generate following patterns
#include<iostream>
using namespace std ;
main()
{
int i,j,s;
cout<<"This program was made by Muhammad Shoaib"<<endl;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
cout<<"*";
}
cout<<endl;
}
cout<<" PATTEREN NO 2"<<endl;
for(i=1;i<=5;i++)
{
for(s=1;s<=i;s++)
{
cout<<" ";
}
for(j=i;j<=5;j++)
{
cout<<"*";
}
cout<<endl;
}
cout<<"PATTEREN NO 3 "<<endl;
int L=10;
for(s=1;s<=5;s++)
{
for(j=1;j<=L;j++)
{
cout<<s*j<<" ";
}
cout<<endl;
}
}
}
switch statement in c++
1. Write a program to find the factorial value of any number entered through the keyboard
#include<iostream>
using namespace std;
int main()
{
int n,fact=1;
cout<<"Enter any number : ";
cin>>n;
while(n>=1)
{
fact*=n;
n--;
}
cout<<"Factorial :"<<fact;
}
2. Write a program to print out all Armstrong numbers between 1 and 500. If sum of cubes of each digit of the number is equal to the number itself, then the number is called an Armstrong number. For example, 153 = ( 1 * 1 * 1 ) + ( 5 * 5 * 5 ) + ( 3 * 3 * 3 )
#include<iostream>
using namespace std;
int main()
{
int n,digit1,digit2,digit3;
for(int i=1;i<=500;i++)
{
digit1=i/100;
digit2=i/10 - digit1*10;
digit3=i%10;
if(digit1*digit1*digit1 + digit2*digit2*digit2 + digit3*digit3*digit3 == i)
cout<<i<<endl;
}
}
3. Write a program to print all prime numbers from 1 to 300.
int i,j,d=0;
cout<<"Display the prime number 1 to 300 "<<endl;
for(i=1;i<=300;i++)
{
for(j=2;j<=i/2;j++)
{
if(i%j==0)
{
d++;
break;
}}
if(d==0)
cout<<"\t \t"<<i<<"\n \n";
d=0;
}}
4. Write a program to print the multiplication table of the number entered by the user
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter a positive integer: ";
cin >> n;
for (int i = 1; i <= 10; ++i) {
cout << n << " * " << i << " = " << n * i << endl;
}
}
5. Write a program to generate Fibonacci number up to 500. Consider the following sequence of numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34,....
# include <iostream>
using namespace std;
main()
{
int first=0,second=1,third;
for(int i=0;i<500;i++)
{
if(i<=1)
{
third=i;
}
else
{
third=first+second;
first=second;
second=third;
}
cout<<third<<" ";
}}
6. Write a program using loop to read five numbers and finds their sum and average.
#include <iostream>
using namespace std;
main()
{
int i,n,sum=0;
float avg;
cout<<"Input the 5 numbers : \n";
for (i=1;i<=5;i++)
{
cout<<"Number is :"<<i<<endl;
cin>>n;
sum =sum+n;
}
avg=sum/5;
cout<<"The sum of 5 no is : "<<sum<<"\nThe Average is : \n"<<avg<<endl;
}
7. Write a program to finds the sum of the first n positive integers
#include <iostream>
using namespace std;
main()
{
int n, count, sum = 0;
cout<<"Enter the value of n(positive integer: ";
cin>>n;
for(count=1; count <= n; count++)
{
sum = sum + count;
}
cout<<"Sum of first n natural numbers is: "<< sum;
}
8. Write a program to finds the sum as many positive integers as user want to enter. Use break and continue statement.
# include <iostream>
using namespace std;
int main()
{
int i;
double n, sum = 0.0;
for(i=1; i <= 10; ++i)
{
cout<<"Enter number: ";
cin>>n;
// If user enters negative number, loop is terminated
if(n < 0.0)
{
break;
}
sum += n; // sum = sum + number;
}
cout<<"Sum = "<<sum;
return 0;
}
9. Write program that Counts zeros, odds, and evens using loop. This program counts the number of odd and even numbers. The program also counts the number of zeros
#include<iostream>
using namespace std ;
main()
{
int odd=0;
int zero=0;
int even=0;
int i;
int num;
for(i=1;i<=5;i++)
{
cout<<"enter the number:"<<endl;
cin>>num;
if(num!=0 && num%2==0)
{
even++;
}
else if(zero==0)
{
zero++;
}
else
odd++;
}
cout<<"even number:"<<even <<endl;
cout<<"zero number:"<<zero <<endl;
cout<<"odd number:"<<odd <<endl;
}
10. Write a program to find ever or odd numbers up-to nth positive number
#include<iostream>
using namespace std ;
main()
{
int i,num;
for(i=num;i>0;i--)
{
cout<<"enter the number:";
cin>>num;
if(num>0 && num%2==0)
cout<<"number is even"<<endl;
else if(num>0 && num%2!=0)
cout<<"number is odd"<<endl;
else
break;
}
11. Write a program to take 10 digit number as input taken through the keyboard, write a program to obtain the sum of all number.
#include <iostream>
using namespace std;
main()
{
long num, temp, digit, sum = 0;
cout<<"Enter the number ";
cin>>num;
temp = num;
while (num > 0)
{
digit = num % 10;
sum = sum + digit;
num /= 10;
}
cout<<"Given number = "<<temp;
cout<<"\nSum of the digits = "<<sum;
}
13. Write a program to generate following patterns
#include<iostream>
using namespace std ;
main()
{
int i,j,s;
cout<<"This program was made by Muhammad Shoaib"<<endl;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
cout<<"*";
}
cout<<endl;
}
cout<<" PATTEREN NO 2"<<endl;
for(i=1;i<=5;i++)
{
for(s=1;s<=i;s++)
{
cout<<" ";
}
for(j=i;j<=5;j++)
{
cout<<"*";
}
cout<<endl;
}
cout<<"PATTEREN NO 3 "<<endl;
int L=10;
for(s=1;s<=5;s++)
{
for(j=1;j<=L;j++)
{
cout<<s*j<<" ";
}
cout<<endl;
}
}
}
switch statement in c++
A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.
Syntax
The syntax for a switch statement in C++ is as follows −
switch(expression) { case constant-expression : statement(s); break; //optional case constant-expression : statement(s); break; //optional // you can have any number of case statements. default : //Optional statement(s); }
The following rules apply to a switch statement −
- The expression used in a switch statement must have an integral or enumerated type, or be of a class type in which the class has a single conversion function to an integral or enumerated type.
- You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.
- The constant-expression for a case must be the same data type as the variable in the switch, and it must be a constant or a literal.
- When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.
- When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.
- Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.
- A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.
Flow Diagram
Example
#include <iostream> using namespace std; int main () { // local variable declaration: char grade = 'D'; switch(grade) { case 'A' : cout << "Excellent!" << endl; break; case 'B' : case 'C' : cout << "Well done" << endl; break; case 'D' : cout << "You passed" << endl; break; case 'F' : cout << "Better try again" << endl; break; default : cout << "Invalid grade" << endl; } cout << "Your grade is " << grade << endl; return 0; }
This would produce the following result −
You passed Your grade is D
Subscribe to:
Posts (Atom)
Write a program, that takes 16-Elements in Two-Dimensional-Array that has 4 rows & 4 columns, then display the diagonal of Matrix.
Write a program, that takes 16-Elements in Two-Dimensional-Array that has 4 rows & 4 columns, then display the diagonal of Matrix. ...