Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CPlusPlusNotesForProfessionals.pdf
Скачиваний:
47
Добавлен:
20.05.2023
Размер:
5.11 Mб
Скачать

//will execute if a is a string "test"

}else {

//only if the first failed, will execute

for multiple choices :

if (a=='a') {

//if a is a char valued 'a' } else if (a=='b') {

//if a is a char valued 'b' } else if (a=='c') {

//if a is a char valued 'c' } else {

//if a is none of the above

}

however it must be noted that you should use 'switch' instead if your code checks for the same variable's value

Section 15.10: goto

Jumps to a labelled statement, which must be located in the current function.

bool f(int arg) {

bool result = false;

hWidget widget = get_widget(arg); if (!g()) {

// we can't continue, but must do cleanup still goto end;

}

// ...

result = true; end:

release_widget(widget); return result;

}

Section 15.11: Jump statements : break, continue, goto, exit

The break instruction:

Using break we can leave a loop even if the condition for its end is not fulfilled. It can be used to end an infinite loop, or to force it to end before its natural end

The syntax is

break;

Example: we often use break in switch cases,ie once a case i switch is satisfied then the code block of that condition is executed .

switch(conditon){ case 1: block1; case 2: block2; case 3: block3;

default: blockdefault;

}

GoalKicker.com – C++ Notes for Professionals

81

in this case if case 1 is satisfied then block 1 is executed , what we really want is only the block1 to be processed but instead once the block1 is processed remaining blocks,block2,block3 and blockdefault are also processed even though only case 1 was satified.To avoid this we use break at the end of each block like :

switch(condition){ case 1: block1;

break; case 2: block2;

break; case 3: block3;

break; default: blockdefault;

break;

}

so only one block is processed and the control moves out of the switch loop.

break can also be used in other conditional and non conditional loops like if,while,for etc;

example:

if(condition1){

....

if(condition2){

.......

break;

}

...

}

The continue instruction:

The continue instruction causes the program to skip the rest of the loop in the present iteration as if the end of the statement block would have been reached, causing it to jump to the following iteration.

The syntax is

continue;

Example consider the following :

for(int i=0;i<10;i++){ if(i%2==0)

continue; cout<<"\n @"<<i;

}

which produces the output:

@1 @3 @5 @7 @9

i this code whenever the condition i%2==0 is satisfied continue is processed,this causes the compiler to skip all the remaining code( printing @ and i) and increment/decrement statement of the loop gets executed.

GoalKicker.com – C++ Notes for Professionals

82

The goto instruction:

It allows making an absolute jump to another point in the program. You should use this feature carefully since its execution ignores any type of nesting limitation. The destination point is identified by a label, which is then used as an argument for the goto instruction. A label is made of a valid identifier followed by a colon (:)

The syntax is

goto label;

..

.

label: statement;

Note: Use of goto statement is highly discouraged because it makes di cult to trace the control flow of a program, making the program hard to understand and hard to modify.

Example :

int num = 1; STEP:

do{

if( num%2==0 )

{

num = num + 1;

GoalKicker.com – C++ Notes for Professionals

83

goto STEP;

}

cout << "value of num : " << num << endl; num = num + 1;

}while( num < 10 );

output :

value of num : 1 value of num : 3 value of num : 5 value of num : 7 value of num : 9

whenever the condition num%2==0 is satisfied the goto sends the execution control to the beginning of the do-while loop.

The exit function:

exit is a function defined in cstdlib. The purpose of exit is to terminate the running program with an specific exit code. Its prototype is:

void exit (int exit code);

cstdlib defines the standard exit codes EXIT_SUCCESS and EXIT_FAILURE.

Section 15.12: return

Returns control from a function to its caller.

If return has an operand, the operand is converted to the function's return type, and the converted value is returned to the caller.

int f() { return 42;

}

int x = f(); // x is 42 int g() {

return 3.14;

}

int y = g(); // y is 3

If return does not have an operand, the function must have void return type. As a special case, a void-returning function can also return an expression if the expression has type void.

void f(int x) {

if (x < 0) return; std::cout << sqrt(x);

}

int g() { return 42; } void h() {

return f(); // calls f, then returns return g(); // ill-formed

}

When main returns, std::exit is implicitly called with the return value, and the value is thus returned to the

GoalKicker.com – C++ Notes for Professionals

84

execution environment. (However, returning from main destroys automatic local variables, while calling std::exit directly does not.)

int main(int argc, char** argv) { if (argc < 2) {

std::cout << "Missing argument\n";

return EXIT_FAILURE; // equivalent to: exit(EXIT_FAILURE);

}

}

GoalKicker.com – C++ Notes for Professionals

85