Selection Statements
C/C++ supports two types of selection statements: if and switch.if
The general form of the if statement isif (expression) {
statement;
}
else
{ statement;
}
For Example.
#include<iostream.h>
using namespace std;
int main() {
int a;
cout<< " enter No" ;
cin>>a;
if(a == 10) {
cout<< " OK" ;
}
else{
cout<< "not equal to 10";
}
return 0;
}
Note: I advice you to not use Turbo C++. use Visual C++ Express 2010. Hear is link http://en.softonic.com/s/c-programming-language-full-version-free-software
Nested if
A nested if is an if that is the target of another if or else. Nested ifs are very common in programming. In a nested if, anelse statement always refers to the nearest if statement that is within the same block as the else and that is not already associated with an else. For example,
#include<iostream.h>
using namespace std;
int main() {
int a;
cout<< " enter No" ;
cin>>a;
if(a == 10) {
cout<< " OK" ;
}
else{
if(a>=10){
cout<< "no. is less then 10";
}
else{
cout<< " not valid no. "
}
}
return 0;
}
switch
C/C++ has a built-in multiple-branch selection statement, called switch, which successively tests the value of an expression against a list of integer or character constants. When a match is found, the statements associated with that constant are executed. The general form of the switch statement isswitch (expression)
{ case constant1: statement
break;
case constant2: statement
break;
case constant3: statement
break;
.
.
.
default statement sequence }
The break statements inside the switch statement are optional. They terminate the statement sequence associated with each constant. If the break statement is omitted, execution will continue on into the next case's statements until either a break or the end of the switch is reached. For example, the following function uses the "drop through" nature of the cases to simplify the code for a device-driver input handler:
For Example:
#include<iostream.h>
using namespace std;
void main()
{
int a;
cout<< " enter No" ;
cin>>a;
switch(a){
case 1: cout<< "enter no is 1 ";
break;
case 3: cout<< "enter no is 3 ";
break;
case 25: cout<< "enter no is 25 ";
break;
default:cout<<" no match try again";
}
return 0;
}
Iteration Statements
In C/C++, and all other modern programming languages, iteration statements (also called loops) allow a set of instructions to be executed repeatedly until a certain condition is reached. This condition may be predefined (as in the for loop), or open-ended (as in the while and do-while loops).
The for Loop
The general design of the for loop is reflected in some form or another in all procedural programming languages. However, in C/C++, it provides unexpected flexibility and power. The general form of the for statement is
for(initialization; condition; increment) {
statement;
}
for loop only run if condition is true.
For Example
#include <iostream.h>
int main(void) {
int x;
cout<< " enter No" ;
cin>>x;
for(x=1; x <= 100; x++) {
cout<<"no is"<<x:
}
return 0;
}
Infinite for Loop Write Following code
for( ; ; ) {
cout<<"This loop will run forever.\n"
}
Note: Nested for can be used.
The while Loop
The second loop available in C/C++ is the while loop. Its general form is
while(condition){
statement;
}
where statement is either an empty statement, 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 of code immediately following the loop.
The do-while Loop
Unlike for and while loops, which test the loop condition at the top of the loop, the do-while loop checks its condition at the bottom of the loop. This means that a do-while loop always executes at least once. The general form of the do-while loop is
do{ statement; }
while(condition);
Although the curly braces are not necessary when only one statement is present, they are usually used to avoid confusion (to you, not the compiler) with the while. The do-while loop iterates until condition becomes false. The following do-while loop will read numbers from the keyboard until it finds a number less than or equal to 100.
int a;
do {
cin>>a;
}
while(num > 100);
Note: Here, the do-while loop is a good choice because you will always want a menu function to execute at least once. After the options have been displayed, the program will loop until a valid option is selected.
Jump Statements
C/C++ has four statements that perform an unconditional branch: return, goto, break, and continue. Of these, you may use return and goto anywhere in your program. You may use the break and continue statements in conjunction with any of the loop statements. As discussed earlier in this chapter, you can also use break with switch.
The return Statement
The return statement is used to return from a function. It is categorized as a jump statement because it causes execution to return (jump back) to the point at which the call to the function was made. A return may or may not have a value associated with it. If return has a value associated with it, that value becomes the return value of the function. In C, a non-void function does not technically have to return a value. If no return value is specified, a garbage value is returned. However, in C++, a non-void function must return a value. That is, in C++, if a function is specified as returning a value, any return statement within it must have a value associated with it. (Even in C, if a function is declared as returning a value, it is good practice to actually return one.) The general form of the return statement is
return expression;
The expression is present only if the function is declared as returning a value. In this case, the value of expression will become the return value of the function. You can use as many return statements as you like within a function. However, the function will stop executing as soon as it encounters the first return. The } that ends a function also causes the function to return. It is the same as a return without any specified value. If this occurs within a non-void function, then the return value of the function is undefined. A function declared as void may not contain a return statement that specifies a value. Since a void function has no return value, it makes sense that no return statement within a void function can return a value.
The goto
Statement Since C/C++ has a rich set of control structures and allows additional control using break and continue, there is little need for goto. Most programmers' chief concern about the goto is its tendency to render programs unreadable. Nevertheless, although the goto statement fell out of favor some years ago, it occasionally has its uses. There are no programming situations that require goto. Rather, it is a convenience, which, if used wisely,can be a benefit in a narrow set of programming situations, such as jumping out of a set of deeply nested loops. The goto is not used outside of this section. The goto statement requires a label for operation. (A label is a valid identifier followed by a colon.) Furthermore, the label must be in the same function as the goto that uses it—you cannot jump between functions. The general form of the goto statement is
goto label;
.
.
.
label:
where label is any valid label either before or after goto. For example, you could create a loop from 1 to 100 using the goto and a label, as shown here:
x = 1;
loop1:
x++;
if(x<100)
{goto loop1;}
The exit( ) Function
Although exit() is not a program control statement, a short digression that discusses it is in order at this time. Just as you can break out of a loop, you can break out of a program by using the standard library function exit(). This function causes immediate termination of the entire program, forcing a return to the operating system. In effect, the exit() function acts as if it were breaking out of the entire program. The general form of the exit() function is
void exit(int return_code);
The value of return_code is returned to the calling process, which is usually the operating system. Zero is generally used as a return code to indicate normal program termination. Other arguments are used to indicate some sort of error. You can also use the macros EXIT_SUCCESS and EXIT_FAILURE for the return_code. The exit() function requires the header stdlib.h. A C++ program may also use the new-style header <cstdlib>. Programmers frequently use exit() when a mandatory condition for program execution is not satisfied. For example, imagine a virtual reality computer game that requires a special graphics adapter. The main() function of this game might look like this:
#include <stdlib.h>
int main(void)
{
if(!virtual_graphics()) exit(1);
play();
/* ... */
}
The continue Statement
The continue statement works somewhat like the break statement. Instead of forcing termination, however, continue forces the next iteration of the loop to take place, skipping any code in between. For the for loop, continue causes the conditional test and increment portions of the loop to execute. For the while and do-while loops, program control passes to the conditional tests.
Block Statements
Block statements are simply groups of related statements that are treated as a unit. The statements that make up a block are logically bound together. Block statements are also called compound statements. A block is begun with a { and terminated by its matching }. Programmers use block statements most commonly to create a multistatement target for some other statement, such as if. However, you may place a block statement anywhere you would put any other statement. For example, this is perfectly valid (although unusual) C/C++ code:
#include <iostudio.h>
int main(void) {
int i;
{ /* a block statement */
i = 120;
cout<< i);
}
return 0;
}
Comments
Post a Comment