ITHelpself / CPlusPlus

0 stars 0 forks source link

15. Flow control #16

Open ITHelpself opened 4 years ago

ITHelpself commented 4 years ago

char c = getchar();
bool confirmed;
switch (c) {
case 'y':
confirmed = true;
break;
case 'n':
confirmed = false;
break;
default:
std::cout << "invalid response!\n";
abort();
}
ITHelpself commented 4 years ago

char c = getchar();
bool confirmed;
switch (c) {
case 'y':
confirmed = true;
break;
case 'n':
confirmed = false;
break;
default:
std::cout << "invalid response!\n";
abort();
}
ITHelpself commented 4 years ago

try {
std::vector<int> v(N);
// do something
} catch (const std::bad_alloc&) {
std::cout << "failed to allocate memory for vector!" << std::endl;
} catch (const std::runtime_error& e) {
std::cout << "runtime error: " << e.what() << std::endl;
} catch (...) {
std::cout << "unexpected exception!" << std::endl;
throw;
}
ITHelpself commented 4 years ago

void print_asterisks(int count) {
if (count < 0) {
throw std::invalid_argument("count cannot be negative!");
}
while (count--) { putchar('*'); }
}
ITHelpself commented 4 years ago

try {
// something risky
} catch (const std::bad_alloc&) {
std::cerr << "out of memory" << std::endl;
} catch (...) {
std::cerr << "unexpected exception" << std::endl;
// hope the caller knows how to handle this exception
throw;
}
ITHelpself commented 4 years ago

// this function might propagate a std::runtime_error,
// but not, say, a std::logic_error
void risky() throw(std::runtime_error);
// this function can't propagate any exceptions
void safe() throw();
ITHelpself commented 4 years ago

unsigned int predecessor(unsigned int x) {
return (x > 0) ? (x - 1) : (throw std::invalid_argument("0 has no predecessor"));
}
ITHelpself commented 4 years ago

char c = getchar();
bool confirmed;
switch (c) {
case 'y':
confirmed = true;
break;
case 'n':
confirmed = false;
break;
default:
std::cout << "invalid response!\n";
abort();
}
ITHelpself commented 4 years ago

class Base {
// ...
// we want to be able to delete derived classes through Base*,
// but have the usual behaviour for Base's destructor.
virtual ~Base() = default;
};
ITHelpself commented 4 years ago

std::vector<int> v(N); // if an exception is thrown here,
// it will not be caught by the following catch block
try
{
    std::vector<int> v(N); // if an exception is thrown here,
    // it will be caught by the following catch block
    // do something with v
}
catch (const std::bad_alloc &)
{
    // handle bad_alloc exceptions from the try block
}
ITHelpself commented 4 years ago

int x;
std::cout << "Please enter a positive number." << std::endl;
std::cin >> x;
if (x <= 0) {
    std::cout << "You didn't enter a positive number!" << std::endl;
    abort();
}
ITHelpself commented 4 years ago

int x;
std::cin >> x;
if (x%2 == 0) {
     std::cout << "The number is even\n";
} else {
    std::cout << "The number is odd\n";
}
ITHelpself commented 4 years ago

if (true) { /* code here */ } // evaluate that true is true and execute the code in the brackets
if (false) { /* code here */ } // always skip the code since false is always false
ITHelpself commented 4 years ago

if(istrue()) { } // evaluate the function, if it returns true, the if will execute the code
if(isTrue(var)) { } //evaluate the return of the function after passing the argument var
if(a == b) { } // this will evaluate the return of the experssion (a==b) which will be true if
equal and false if unequal
if(a) { } //if a is a boolean type, it will evaluate for its value, if it's an integer, any non
zero value will be true,
ITHelpself commented 4 years ago

if (a && b) { } // will be true only if both a and b are true (binary operators are outside the
scope here
if (a || b ) { } //true if a or b is true
ITHelpself commented 4 years ago

if (a== "test") {
//will execute if a is a string "test"
} else {
// only if the first failed, will execute
}
ITHelpself commented 4 years ago

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
}
ITHelpself commented 4 years ago

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;
}
ITHelpself commented 4 years ago

switch(conditon){
case 1: block1;
case 2: block2;
case 3: block3;
default: blockdefault;
}
ITHelpself commented 4 years ago

switch(condition){
case 1: block1;
break;
case 2: block2;
break;
case 3: block3;
break;
default: blockdefault;
break;
}
ITHelpself commented 4 years ago

if(condition1){
....
if(condition2){
.......
break;
}
...
}
ITHelpself commented 4 years ago

for(int i=0;i<10;i++){
if(i%2==0)
continue;
cout<<"\n @"<<i;
}
ITHelpself commented 4 years ago

goto label;
..
.
label: statement;
ITHelpself commented 4 years ago

int num = 1;
STEP : do
{
    if (num % 2 == 0)
    {
        num = num + 1;
        goto STEP;
    }
    cout << "value of num : " << num << endl;
    num = num + 1;
}
while (num < 10)
    ;
ITHelpself commented 4 years ago

void exit (int exit code);
ITHelpself commented 4 years ago

int f() {
return 42;
}
int x = f(); // x is 42
int g() {
return 3.14;
}
int y = g(); // y is 3
ITHelpself commented 4 years ago

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
}
ITHelpself commented 4 years ago

int main(int argc, char** argv) {
if (argc < 2) {
std::cout << "Missing argument\n";
return EXIT_FAILURE; // equivalent to: exit(EXIT_FAILURE);
}
}