ASSIGNMENT: C++ PROGRAMMING QUESTIONS 1. Who is written C++ ? Bjarne Stroustrup is written C++.He born on December 30, 1950 is acomputer scientist and creator of theC++programming language.He is a Danishcomputer scientist ,most notable for the creation and development of the widely used C++programming language. [4] He is a Distinguished Research Professor and holds the College of Engineering Chair in Computer Science at Texas A&M University, [5] is a visiting professor atColumbia University ,and works atMorgan Stanleyas a Managing Director in New York . [6][7][8] Educations of Bjarne Stroustrup is Stroustrup has amaster's degreeinmathematicsandcomputer science(1975) fromAarhus U niversity,Denmark, and aPh.D.in computer science (1979) from theUniversity of Cambridge,England. [5] His thesis advisor in Cambridge wasDavid Wheeler. [9][10] Care er of him isStroustrup began developing C++ in 1978 (then called "Cwith Classes"), and, in his own words, "invented C++, wrote its early definitions, and produced its first implementation... chose and formulated the design criteria for C++, designed all its major facilities, and was responsible for the processing of extension proposals in the C++ standards committee." [11] Stroustrup also wrote a textbook for the language, The C++ Programming Language.Stroustrup was the head ofAT&T B ell Labs' Large-scale Programming Research department, from its creation until late 2002. Stroustrup was elected member of the National Academy of Eng ineeringin 2004. He is a Fellow of the ACM(1994) and anIEEEFellow. He works atTexas A&M Universityas a Distinguished Professor where he holds the College of Engineering Endowed Chair inComputer Science. [12][13] He is also a visiting faculty in Computer Science Department at Columbia University. [14] ITMO Universitynoble doctor since 2013 [15]0
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
8/16/2019 ASSIGNMENT C++ (NUR SYAHIRA BINTI MOHD SA'ADY)
The while loop can be used if you don’t know how many times a loop must run. Here is an example:
#include<stdio.h>
int main()
{
int counter, howmuch;
scanf("%d", &howmuch);
counter = 0;
while ( counter < howmuch)
{
counter++;
printf("%d\n", counter);
}
return 0;
}
Let’s take a look at the example: First you must always initialize the counter before the while loop starts (
counter = 1). Then the while loop will run if the variable counter is smaller then the variable “howmuch”. Ifthe input is ten, then 1 through 10 will be printed on the screen. A last thing you have to remember is toincrement the counter inside the loop (counter++). If you forget this the loop becomes infinitive.
8/16/2019 ASSIGNMENT C++ (NUR SYAHIRA BINTI MOHD SA'ADY)
As said before (after the for loop example) it makes a difference if prefix incrementing (++i) or postfix
incrementing (i++) is used with while loop. Take a look at the following postfix and prefix increment
while loop example:
#include<stdio.h>int main(void) {
int i;
i = 0;
while(i++ < 5) {
printf("%d\n", i);
}
printf("\n");
i = 0;
while(++i < 5) {
printf("%d\n", i);}
return 0;
}
The output of the postfix and prefix increment example will look like this:
1
2
3
4
5
1
2
3
4
i++ will increment the value of i, but is using the pre-incremented value to test against < 5.That’s why we get 5 numbers.++i will increment the value of i, but is using the incremented value to test against < 5. That’s why we get4 numbers.
8/16/2019 ASSIGNMENT C++ (NUR SYAHIRA BINTI MOHD SA'ADY)
The “do while loop” is almost the same as the while loop. The “do while loop” has the following form:
do
{do something;
}
while (expression);
Do something first and then test if we have to continue. The result is that the loop always runs once.(Because the expression test comes afterward). Take a look at an example:
#include<stdio.h>
int main()
{
int counter, howmuch;scanf("%d", &howmuch);
counter = 0;
do
{
counter++;
printf("%d\n", counter);
}
while ( counter < howmuch);
return 0;
}
Note: There is a semi-colon behind the while line.
8/16/2019 ASSIGNMENT C++ (NUR SYAHIRA BINTI MOHD SA'ADY)
Break and continueTo exit a loop you can use the break statement at any time. This can be very useful if you want to stop
running a loop because a condition has been met other than the loop end condition. Take a look at the
following example:
#include<stdio.h>
int main()
{
int i;
i = 0;
while ( i < 20 )
{
i++;if ( i == 10)
break;
}
return 0;
}
In the example above, the while loop will run, as long i is smaller then twenty. In the while loop there is anif statement that states that if i equals ten the while loop must stop (break).
With “continue;” it is possible to skip the rest of the commands in the current loop and start from the
top again. (the loop variable must still be incremented). Take a look at the example below:
#include<stdio.h>
int main()
{
int i;
i = 0;
while ( i < 20 )
{
i++;
continue;printf("Nothing to see\n");
}
return 0;
}
In the example above, the printf function is never called because of the “continue;”.
8/16/2019 ASSIGNMENT C++ (NUR SYAHIRA BINTI MOHD SA'ADY)
These loops are used when one wants to loop forever and the breaking out condition from loop is not knowCertiain conditions are set inside the loop along with either break or return statements to come out of theloop. For example:
while(true){//run this code
if(condition satisfies)
break; //return;
}
These loops are just like any other while loop with condition to stop the loop is in the body of the
while loop otherwise it will run forever (which is never the intention of a part of the code until required so). depends upon the logic of the programmer only what he/she wants to do.
E. Do / While
Difference between while and do-while loop?
->do-while loop is similar to while loop, however there is one basic difference between them – do-while runs a
least one even if the test condition is false at first place. let’s understand this with an example –
Using while loop:
main(){
int i=0
while(i==1){
printf("while vs do-while");}
printf("Out of loop");
}
Output:Out of loop
Same example using do-while loop
main()
{int i=0
do {
printf("while vs do-while\n");
}while(i==1);
printf("Out of loop");}
8/16/2019 ASSIGNMENT C++ (NUR SYAHIRA BINTI MOHD SA'ADY)