Académique Documents
Professionnel Documents
Culture Documents
QUESTIONS
C++ go to statement :
1. Go to statement is used for unconditional jump.
2. Go to statement makes difficult to trace the control flow of program
3. Go to statement should be avoided in order to make smoother program.
Example #1 : Goto Statement :
#include <iostream>
using namespace std;
int main ()
{
int num = 1;
STEP:do
{
if( num == 5)
{
num = num + 1;
goto STEP;
}
cout << "value of num : " << num << endl;
num = num + 1;
}
b. While
A while loop statement repeatedly executes a target statement as long
as a given condition is true.
The syntax of a while loop in C++ is:
while(condition)
{
statement(s);
}
Here, statement(s) may be a single statement or a block of statements.
The condition may be any expression, and true is any non-zero value.
The loop iterates while the condition is true.
When the condition becomes false, program control passes to the line
immediately following the loop.
c. Break and continue
break Statement
In C programming, break is used in terminating the loop immediately
after it is encountered. The break statement is used with conditional if
statement.
Example of break statement
Write a C program to find average of maximum of n positive numbers
entered by user. But, if the input is negative, display the
average(excluding the average of negative input) and end the
program.
/* C program to demonstrate the working of break statement by
terminating a loop, if user inputs negative number*/
# include <stdio.h>
int main(){
float num,average,sum;
int i,n;
printf("Maximum no. of inputs\n");
scanf("%d",&n);
for(i=1;i<=n;++i){
printf("Enter n%d: ",i);
scanf("%f",&num);
if(num<0.0)
break;
//for loop breaks if num<0.0
sum=sum+num;
}
average=sum/(i-1);
printf("Average=%.2f",average);
return 0;
}
continue Statement
d. While true
I often use this code pattern:
while(true) {
//do something
if(<some condition>) {
break;
}
}
Another programmer told me that this was bad practice and that I
should replace it with the more standard:
while(!<some condition>) {
//do something
}
His reasoning was that you could "forget the break" too easily and have
an endless loop. I told him that in the second example you could just as
easily put in a condition which never returned true and so just as easily
have an endless loop, so both are equally valid practices.
Further, I often prefer the former as it makes the code easier to read
when you have multiple break points, i.e. multiple conditions which get
out of the loop.
e. Do/While
In most computer programming languages, a do while loop is a control
flow statement that executes a block of code at least once, and then
repeatedly executes the block, or not, depending on a given boolean
condition at the end of the block. Note though that unlike most
languages, Fortran's do loop is actually the same as the for loop.
The do while construct consists of a process symbol and a condition.
First, the code within the block is executed, and then the condition is
evaluated. If the condition is true the code within the block is executed
again. This repeats until the condition becomes false. Because do while
loops check the condition after the block is executed, the control
structure is often also known as a post-test loop. Contrast with the
while loop, which tests the condition before the code within the block is
g. If/else
elseif, as its name suggests, is a combination of if and else. Like else, it
extends an if statement to execute a different statement in case the
original if expression evaluates to FALSE. However, unlike else, it will
execute that alternative expression only if the elseif conditional
expression evaluates to TRUE. For example, the following code would
display a is bigger than b, a equal to b or a is smaller than b:
<?php
if ($a > $b) {
echo "a is bigger than b";
} elseif ($a == $b) {
echo "a is equal to b";
} else {
echo "a is smaller than b";
}
?>
There may be several elseifs within the same if statement. The first
elseif expression (if any) that evaluates to TRUE would be executed. In
PHP, you can also write 'else if' (in two words) and the behavior would
be identical to the one of 'elseif' (in a single word). The syntactic
meaning is slightly different (if you're familiar with C, this is the same
behavior) but the bottom line is that both would result in exactly the
same behavior.
The elseif statement is only executed if the preceding if expression and
any preceding elseif expressions evaluated to FALSE, and the current
elseif expression evaluated to TRUE.