Assignment C++ (Nur Syahira Binti Mohd Sa'ady)
Assignment C++ (Nur Syahira Binti Mohd Sa'ady)
Assignment C++ (Nur Syahira Binti Mohd Sa'ady)
QUESTIONS
1.
2.
A.
Go to :-
In C++ programming, goto statement is used for altering the normal sequence of
program execution by transferring control to some other part of the program.
In syntax above, label is an identifier. When goto label; is encountered, the control of program jumps
to label: and executes the code below it.
In syntax above, label is an identifier. When goto label; is encountered, the control of
program jumps to label: and executes the code below it.
Example 1: goto Statement:/* C++ program to demonstrate the working of goto statement. */
/* This program calculates the average of numbers entered by user. */
/* If user enters negative number, it ignores that number and
calculates the average of number entered before it.*/
# include <iostream>
usingnamespace std;
int main(){
float num, average, sum =0.0;
int i, n;
cout<<"Maximum number of inputs: ";
cin>>n;
for(i=1; i <= n;++i){
cout<<"Enter n"<<i<<": ";
cin>>num;
if(num <0.0){
goto jump;/* Control of the program moves to jump; */
}
sum += num;
}
jump:
average=sum/(i-1);
cout<<"\nAverage = "<<average;
return0;
}
B.
While
#include<stdio.h>
int main()
{
int i;
for (i = 0; i < 10; i++)
{
printf ("Hello\n");
printf ("World\n");
}
return 0;
}
#include<stdio.h>
int main()
{
int counter, howmuch;
scanf("%d", &howmuch);
counter = 0;
while ( counter < howmuch)
{
counter++;
printf("%d\n", counter);
}
return 0;
}
Lets 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. If
the input is ten, then 1 through 10 will be printed on the screen. A last thing you have to remember is to
increment the counter inside the loop (counter++). If you forget this the loop becomes infinitive.
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.
Thats why we get 5 numbers.
++i will increment the value of i, but is using the incremented value to test against < 5. Thats why we get
4 numbers.
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.
C.
}
return 0;
}
In the example above, the while loop will run, as long i is smaller then twenty. In the while loop there is an
if 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;.
D.
While True
These loops are used when one wants to loop forever and the breaking out condition from loop is not known.
Certiain conditions are set inside the loop along with either break or return statements to come out of the
loop. 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). It
depends upon the logic of the programmer only what he/she wants to do.
E.
Do / While
Output:
Out of loop
Output:
while vs do-while
Out of loop
F.
Jump / Loop
Cause a certain piece of program to be executed a certain number of times. Consider these
scenarios:
-You want to execute some code/s certain number of time.
-You want to execute some code/s certain number of times depending upon input from user.
Example :
Output :
Enter a positive integer : 5
Factorial of 5 = 120
G.
If / Else
An if statement can be followed by an optional else statement, which executes when the boolean
expression is false.
Syntax:
The syntax of an ifelse statement in C++ is :
If (Boolean_expression)
{
// statement (s) will excute if the Boolean expression is true
}
else
{
// statement (s) will execute if the Boolean expression is false
}
If the Boolean expression evaluates to true, then the if block of code will be executed, otherwise else block of
code will be executed.
Example:
#include <iostream>
using namespace std;
int main ()
{
// local variable declaration:
int a = 100;
// check the boolean condition
if( a < 20 )
{
// if condition is true then print the following
cout << "a is less than 20;" << endl;
}
else
{
// if condition is false then print the following
cout << "a is not less than 20;" << endl;
}
cout << "value of a is : " << a << endl;
return 0;
}
When the above code is compiled and executed, it produces the following result:
a is not less than 20;
value of a is : 100
An if can have zero or one else's and it must come after any else if's.
An if can have zero to many else if's and they must come before the else.
Once an else if succeeds, none of he remaining else if's or else's will be tested.
Syntax:
The syntax of an if...else if...else statement in C++ is:
if(boolean_expression 1)
{
// Executes when the boolean expression 1 is true
}
else if( boolean_expression 2)
{
// Executes when the boolean expression 2 is true
}
else if( boolean_expression 3)
{
// Executes when the boolean expression 3 is true
}
else
{
// executes when the none of the above condition is true.
}
Example:
#include <iostream>
using namespace std;
int main ()
{
// local variable declaration:
int a = 100;
// check the boolean condition
if( a == 10 )
{
// if condition is true then print the following
cout << "Value of a is 10" << endl;
}
else if( a == 20 )
{
// if else if condition is true
cout << "Value of a is 20" << endl;
}
else if( a == 30 )
{
// if else if condition is true
cout << "Value of a is 30" << endl;
}
else
{
// if none of the conditions is true
cout << "Value of a is not matching" << endl;
}
cout << "Exact value of a is : " << a << endl;
return 0;
}
When the above code is compiled and executed, it produces the following result:
Value of a is not matching
Exact value of a is : 100
Ghg