Académique Documents
Professionnel Documents
Culture Documents
a ) Go to
The labeled statement designated by identifier must be in the current
function. All identifier names are members of an internal namespace and
therefore do not interfere with other identifiers.
A statement label is meaningful only to a goto statement; otherwise,
statement labels are ignored. Labels cannot be redeclared.
It is good programming style to use the break, continue,
and return statements instead of the goto statement whenever possible.
However, because the break statement exits from only one level of a loop,
you might have to use a goto statement to exit a deeply nested loop.
For more information about labels and the goto statement, see Labeled
Statements and Using Labels with the goto Statement.
if ( i == 3 )
goto stop;
}
}
// This message does not print:
printf_s( "Loop exited. i = %d\n", i );
stop:
printf_s( "Jumped to stop. i = %d\n", i );
b) WHILE
How while loop works in C++ Programming?
The while loop checks whether the test expression is true or not. If it is true,
code/s inside the body of while loop is executed,that is, code/s inside the
braces { } are executed. Then again the test expression is checked whether
test expression is true or not. This process continues until the test expression
becomes false.
#include <iostream>
using namespace std;
int main() {
int number, i = 1, factorial = 1;
cout<< "Enter a positive integer: ";
cin >> number;
while ( i <= number) {
factorial *= i;
//factorial = factorial * i;
++i;
}
cout<<"Factorial of "<<number<<" = "<<factorial;
return 0;
}
Output
Enter a positive integer: 4
Factorial of 4 = 24
In this program, user is asked to enter a positive integer which is stored in
variable number (supposed user entered 4). Here is the working of while loop
in this program:
1.
2.
3.
4.
5.
do {
statement/s;
}
while (test expression);
Output
Enter a number: 4
Enter a number: 3.4
Enter a number: 6.7
Enter a number: -4.5
Enter a number: 0
Sum = 9.6
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 sumand 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.
d) WHILE TRUE
In C++,a while loop is a control flow statement that allows code to be executed
repeatedly based on a given boolean condition. The while loop can be thought of as
a repeating if statement.
Syntax
while ( condition ) {
Code to execute while the condition is true
}
Example Program
/* Example Program For While Loop In C++
little drops @ thiyagaraaj.com
Coded By:THIYAGARAAJ MP
*/
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
// Variable Declaration
int a;
// Get Input Value
cout<<"Enter the Number :";
cin>>a;
int counter = 1;
//while Loop Block
while (counter <= a)
{
cout<<"Execute While "<<counter<<" time"<<endl;
counter++;
}
// Wait For Output Screen
getch();
return 0;
}
Sample Output:
Enter the Number :4
Execute While 1 time
Execute While 2 time
Execute While 3 time
Execute While 4 time
e ) Do / While
Executes a statement repeatedly until the specified termination condition evaluates
to zero
Syntax
do statement
while ( expression ) ;
Do / While :The test of the termination condition is made after each execution of the loop;
therefore, a do-while loop executes one or more times, depending on the value of
the termination expression. The do-while statement can also terminate when
a break, goto, or return statement is executed within the statement body.
The expression must have arithmetic or pointer type. Execution proceeds as follows:
1. The statement body is executed.
2. Next, expression is evaluated. If expression is false, the do-while statement
terminates and control passes to the next statement in the program.
If expression is true (nonzero), the process is repeated, beginning with step 1.
The following sample demonstrates the do-while statement:
// do_while_statement.cpp
#include <stdio.h>
int main()
{
int i = 0;
do
{
printf_s("\n%d",i++);
} while (i < 3);
}
f) JUMP/LOOP
In C++ a for loop is a programming language statement which allows code to be
repeatedly executed. A for loop is classified as an iteration statement.
Syntax:
for ( variable initialization; condition; variable increment/assignment ) {
Code to execute while the condition is true
}
Example Program
/* Example Program For for Loop In C++
little drops @ thiyagaraaj.com
Coded By:THIYAGARAAJ MP
*/
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
// Variable Declaration
int a;
// Get Input Value
cout<<"Enter the Number :";
cin>>a;
//for Loop Block
for (int counter = 1; counter <= a; counter++)
{
cout<<"Execute "<<counter<<" time"<<endl;
}
// Wait For Output Screen
getch();
return 0;
}
Sample Output:
Enter the Number :5
Execute 1 time
Execute 2 time
Execute 3 time
Execute 4 time
Execute 5 time
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 if...else statement in C++ is:
if(boolean_expression)
{
// statement(s) will execute if the boolean expression is true
}
else
{
// statement(s) will execute if the boolean expression is false
}
Example:
#include <iostream>
using namespace std;
int main ()
{
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.
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>
return 0;
}
When the above code is compiled and executed, it produces the following
result: