Assigment (Mohamad Akram)
Assigment (Mohamad Akram)
Bjarne Stroustrups
a. Go to
A ) Go to : goto label;
... .. ...
... .. ...
... .. ...
label:
statement;
... .. ...
Reason to Avoid goto Statement
The goto statement gives power to jump to any part of program but,
makes the logic of the program complex and tangled. In modern
programming, goto statement is considered a harmful construct and a bad
programming practice.
The goto statement can be replaced in most of C++ program with the use
of break and continue statements.
B ) While:
float num1,num2;
cout<<"Select an operator either + or - or * or / \n";
cin>>o;
cout<<"Enter two operands: ";
cin>>num1>>num2;
switch(o) {
case '+':
cout<<num1<<" + "<<num2<<" = "<<num1+num2;
break;
case '-':
cout<<num1<<" - "<<num2<<" = "<<num1-num2;
break;
case '*':
cout<<num1<<" * "<<num2<<" = "<<num1*num2;
break;
case '/':
cout<<num1<<" / "<<num2<<" = "<<num1/num2;
break;
default:
C ) Break:
// C++ Program to demonstrate working of break statement
#include <iostream>
using namespace std;
int main() {
float number, sum = 0.0;
while (true) {
if (number != 0.0) {
sum += number;
}
else {
break; // terminating the loop if number equals to 0.0
}
}
cout<<"Sum = "<<sum;
return 0;
}
In this C++ program, the test expression is always true. The user is asked to enter a number
which is stored in variable number. If the user enters any number other than 0, that number is
added to sum and stored to it and again user is asked to enter another number. When user
enters 0, the test expression inside if statement is false and body of else is executed which
terminates the loop. Finally, the sum is displayed.
C ) Continue :
// C++ Program to demonstrate working of continue statement
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 10; ++i) {
if ( i == 6 || i == 9) {
continue;
}
cout<<i<<"\t";
}
return 0;
}
D ) While True :
#include <iostream>
int main() {
int number, i = 1, factorial = 1;
cout<< "Enter a positive integer: ";
cin >> number;
//factorial = factorial * i;
++i;
}
E ) Do / While :
do {
statement/s;
}
while (test expression);
The statement/s inside body of loop is executed at least once, that is, the statement/s inside
braces { } is executed at least once. Then the test expression is checked. If the test expression
is true, the body of loop is executed. This process continues until the test expression becomes
false. Since the body of loop is placed before the test expression in do...while loop, the body
of loop is executed at least once.
F ) Jump / Loop :
for(initialization statement; test expression; update statement) {
code/s to be executed;
}
The initialization statement is executed only once at the beginning of the for loop.
Then the test expression is checked by the program. If the test expression is false, for
loop is terminated. But if test expression is true then the code/s inside body of for loop
is executed and then update expression is updated. This process repeats until test
expression is false.
G ) If / else :
if ( a < b ) {
a = b;
}
else {
a = -b;
}
The if...else executes body of if when the test expression is true and executes the body
of else if test expression is false