How to resolve the algorithm Flow-control structures step by step in the C++ programming language
How to resolve the algorithm Flow-control structures step by step in the C++ programming language
Table of Contents
Problem Statement
Document common flow-control structures.
One common example of a flow-control structure is the goto construct. Note that Conditional Structures and Loop Structures have their own articles/categories.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Flow-control structures step by step in the C++ programming language
First Code Snippet
This code snippet contains an infinite loop that prints "Hello, World!" in the console. It uses the goto
statement to jump to the label LOOP
at the beginning of the code, so it will continuously print the message until the program is terminated.
#include <iostream>
int main()
{
LOOP:
std::cout << "Hello, World!\n";
goto LOOP;
}
Second Code Snippet
This code snippet demonstrates exception handling in C++
-
Function
foo
:- Throws an exception with the value
7
. - Attempts to print a message after throwing the exception, but this code is never executed.
- Throws an exception with the value
-
Function
bar
:- Calls function
foo
. - The exception thrown by
foo
is caught insidebar
, but its execution is skipped.
- Calls function
-
Function
baz
:- Calls function
bar
. - Catches any exception thrown from
bar
and re-throws it. - The remaining code in
baz
is skipped due to the re-thrown exception.
- Calls function
-
Function
foobar
:- Calls function
baz
. - Catches different types of exceptions:
char const*
: This catch block would execute iffoo
had thrown achar const*
.int
: Catches and handles theint
exception thrown byfoo
.
- Calls function
-
Function
main
:- Calls function
foobar
. - Contains a catch-all block that would be executed if
foobar
threw an unhandled exception, but this never happens.
- Calls function
The program demonstrates the use of try-catch blocks to handle exceptions and the ability to re-throw exceptions from within a catch block.
Source code in the cpp programming language
#include <iostream>
int main()
{
LOOP:
std::cout << "Hello, World!\n";
goto LOOP;
}
#include <iostream>
#include <ostream>
void foo()
{
std::cout << "Going to throw an exception.\n";
throw 7; // almost any object can be thrown, including ints
std::throw << "This output will never execute.\n";
}
void bar()
{
std::cout << "Going to call foo().\n";
foo();
std::cout << "This will be skipped by the exception coming from foo.\n";
}
void baz()
{
try // everything thrown from inside the following code block
{ // will be covered by the following catch clauses
std::cout << "Going to call bar().\n";
bar();
std::cout << "This will be skipped by the exception coming from foo.\n";
}
catch(...) // a simple catch-all, but doesn't give access to the thrown exception
{
std::cout << "An exception occured. I'll just throw it on.\n";
throw; // without an argument, the caught exception is re-thrown
}
std::cout << "This will not be executed due to the re-throw in the catch block\n";
}
void foobar()
{
try
{
baz();
}
catch(char const* s)
{
std::cout << "If foo had thrown a char const*, this code would be executed.\n";
std::cout << "In that case, the thrown char const* would read " << s << ".\n";
}
catch(int i)
{
std::cout << "Caught an int, with value " << i << " (should be 7).\n";
std::cout << "Not rethrowing the int.\n";
}
catch(...)
{
std::cout << "This catch-all doesn't get invoked because the catch(int) above\n"
<< "already took care of the exception (even if it had rethrown the\n"
<< "exception, this catch-all would not be invoked, because it's\n"
<< "only invoked for exceptions coming from the try block.\n";
}
std::cout << "This will be executed, since the exception was handled above, and not rethrown.\n";
}
int main()
{
try
{
foobar();
}
catch(...)
{
std::cout << "The main function never sees the exception, because it's completely handled\n"
<< "inside foobar(). Thus this catch-all block never gets invoked.\n";
}
}
You may also check:How to resolve the algorithm Iterated digits squaring step by step in the Python programming language
You may also check:How to resolve the algorithm Vampire number step by step in the Raku programming language
You may also check:How to resolve the algorithm Write float arrays to a text file step by step in the Haskell programming language
You may also check:How to resolve the algorithm ABC problem step by step in the SenseTalk programming language
You may also check:How to resolve the algorithm Death Star step by step in the Nim programming language