본문 바로가기

Programming/C++ Basic

[Basic C++] 07 - Flow Control (Loop, if-else, and Jump) - 2

2. if-else statements

if-else statement is also a widely used method to execute a block of code under certain conditions. Consider the statements are scenarios and you want to select which scenario to run based on a given condition. The syntax of if-else statement is below,

// a single line of statement
if (condition) statement;

// multiple statements within one line
if (condition) {statements;}

// multiple statements
if (condition)
{
	statements;
}

// multiple scenarios can be run too using "else"
if (condition) statement1 else statement2

// or
if (condition) statement 1 else if statement 2 else statement 3

Here is a simple example of the above code. I modified the code example 4 to show you how if and else if statements work. As you already know, "localtime_s" accepts references of "time structure" and "time". "time" returns an integer value representing the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC.

 

"asctime_s" converts given calendar time structure (tm) to a textual representation of the following fixed 25-character form: Www Mmm dd hh:mm:ss yyyy\n (26 bytes needed). It accepts three arguments; "char* buf", "rsize_t bufsz", "const struct tm* time_ptr".

 

"strstr (str1, str2)" returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1. It checks whether "str1" has "str2" inside.

 

Code example 5

#include <iostream>
#include <ctime>

int main()
{
	char str[26];
	struct tm stm;
	// Time structue containing a calander date and time.

	// time contains current time
	time_t t = time(NULL);

	// localtime_s converts a time_t time value to a tm structure, and corrects for the local time zone
	// localtime_s (pointer to the time structure, pointer to the stored time)
	localtime_s(&stm, &t);
	asctime_s(str, sizeof(str), &stm);
	
	if (strstr(str, "Sun"))
		std::cout << "It is Sunday.";
	else if (strstr(str, "Sat"))
		std::cout << "It is Saturday.";
	else if (strstr(str, "Fri"))
		std::cout << "It is Friday.";
	else if (strstr(str, "Thu"))
		std::cout << "It is Thursday.";
	else if (strstr(str, "Wed"))
		std::cout << "It is Wednesday.";
	else if (strstr(str, "Tue"))
		std::cout << "It is Tuesday.";
	else if (strstr(str, "Mon"))
		std::cout << "It is Monday.";
}

Figure 5 Result of code example 5

 

2.1) Switch statement

Instead of if-else or if-else if statement, switch statement can do the same job. The problem is that it does not escape from the switch block if you do not specify a breakpoint (break). If there was not a breakpoint after "case 0", then the switch block would execute all of the cases in order. In addition, the case name must be a constant.

 

code example 6

#include <iostream>
#include <ctime>

int main()
{
	char str[26];
	struct tm stm;
	// Time structue containing a calander date and time.

	// time contains current time
	time_t t = time(NULL);

	// localtime_s converts a time_t time value to a tm structure, and corrects for the local time zone
	// localtime_s (pointer to the time structure, pointer to the stored time)
	localtime_s(&stm, &t);

	switch (stm.tm_wday)
	{
		case 0:
			std::cout << "Switch case 0:\n";
			std::cout << "It is Sunday.";
			break;
		case 1:
			std::cout << "Switch case 1:\n";
			std::cout << "It is Saturday.";
			break;
		case 2:
			std::cout << "Switch case 2:\n";
			std::cout << "It is Friday.";
			break;
		case 3:
			std::cout << "Switch case 3:\n";
			std::cout << "It is Thursday.";
			break;
		case 4:
			std::cout << "Switch case 4:\n";
			std::cout << "It is Wednesday.";
			break;
		case 5:
			std::cout << "Switch case 5:\n";
			std::cout << "It is Tuesday.";
			break;
		case 6:
			std::cout << "Switch case 6:\n";
			std::cout << "It is Monday.";
			break;
	}
}

Figure 6 Result of code example 6

 

 

3. jump statements

3.1) goto statement

goto statement which is also referred to as unconditional jump statement allows your program to jump anywhere in your code from a current flow point. "goto" and a "label" can be defined wherever in your code. As soon as the program flow meets "goto label" keyword, the flow point jumps to the pre-defined label.  See code example 7.

 

Code example 7

#include <iostream>

int main()
{

	
	for (int i = 1; i < 10; i++)
	{
		std::cout << "This loop has been repeated " << i << " times.\n";
		if (i == 5)
			goto label;
	}

	label:
		std::cout << "**this program has jumped here.**";
}

Figure 7 Result of code example 7

One might say this is a brilliant statement that you can literally control the flow whenever you want. However, that's not true. goto statement is not recommended to use unless specifically needed because it can mess up your program very easily. See code example 8 and the result. The entire code falls into an infinite loop if you move the label statement to the beginning of the block.

 

Code example 8

#include <iostream>

int main()
{
	label:
		std::cout << "this program has jumped here.";
	
	for (int i = 1; i < 10; i++)
	{
		std::cout << "This loop has been repeated " << i << " times.\n";
		if (i == 5)
			goto label;
	}
	
}

Figure 8 Result of code example 8

 

3.2) break and continue

The "break" keyword allows us to get out of a current loop (the outermost loop !) even if the given condition is still true. "break" is often used to escape a loop before the logic goes into an infinite loop or when an error rises. See code example 9.

 

Code example 9

#include <iostream>
#include <string>

int main()
{
	std::string str;
	std::cout << "Input string : ";
	std::cin >> str;
	for (int i = 0; i < str.size(); i++)
	{
		if (str[i] == 'h')
		{
			std::cout << "Alphabet h is detected.";
			break;
		}
		std::cout << i << " element is : " << str[i] << "\n";
	}
}

Figure 8 Result of code example 8

The "continue" keyword, on the other hand, ignores the current iteration within the loop. Let's see code example 9. The letter 'h' was detected but the iteration was ignored and the loop went on.

 

Code example 10

#include <iostream>
#include <string>

int main()
{
	std::string str;
	std::cout << "Input string : ";
	std::cin >> str;
	for (int i = 0; i < str.size(); i++)
	{
		if (str[i] == 'h')
		{
			continue;
		}
		std::cout << i << " element is : " << str[i] << "\n";
	}
}

Result of code example 10

 

 


REFERENCES

(1) http://www.cplusplus.com/reference/cstring/strstr/