#include<iostream>
#include<iomanip>
#include<string>
#include<sstream>
#include<cassert>
using std::cout;
using std::cin;
using std::endl;
using std::setw;
using std::string;
using std::stringstream;
#ifndef NDEBUG
#define assert_year(year) do { assert (1900 <= (year)); } while(0)
#define assert_month(month) do { assert ((month) && ((month) <= 12)); } while(0)
#define assert_day(day) do { assert ((day) && ((day) <= 31)); } while(0)
#else
#define assert_year(year) ((void)0)
#define assert_month(month) ((void)0)
#define assert_day(day) ((void)0)
#endif NDEBUG
bool is_leap_year (size_t year)
{
assert_year (year);
if (year%4) return false; // If not a multiple of 4 then common year.
if (year%100) return true; // If a multiple of 4 but not of 100 then leap year.
if (year%400) return false; // If a multiple of 4 and 100 but not of 400 then common year.
return true; // If a multiple of 4, 100 and 400 then leap year.
}
size_t days_in_month (size_t month, size_t year)
{
assert_month (month);
switch (month)
{
case 2:
return is_leap_year (year) ? 28 : 29;
case 4: case 6: case 9: case 11:
return 30;
}
return 31;
}
size_t day_of_year (size_t day, size_t month, size_t year)
{
assert_day (day);
assert_day (month);
size_t days = day;
while (--month)
days += days_in_month (month, year);
return days;
}
size_t days_in_previous_years (size_t year)
{
assert_year (year);
size_t days = 0;
while (1900 < --year)
days += is_leap_year (year) ? 366 : 365;
return days;
};
size_t days_since_epoch (size_t day, size_t month, size_t year)
{
// Epoch is 1st January 1900
return days_in_previous_years (year) + day_of_year (day, month, year) - 1;
}
size_t day_of_week (size_t day, size_t month, size_t year)
{
return days_since_epoch (day, month, year) % 7;
}
void print_calendar (size_t month, size_t year)
{
assert_month (month);
cout << endl;
switch (month)
{
case 1: cout << "January"; break;
case 2: cout << "February"; break;
case 3: cout << "March"; break;
case 4: cout << "April"; break;
case 5: cout << "May"; break;
case 6: cout << "June"; break;
case 7: cout << "July"; break;
case 8: cout << "August"; break;
case 9: cout << "September"; break;
case 10: cout << "October"; break;
case 11: cout << "November"; break;
case 12: cout << "December"; break;
}
cout << ' ' << year << endl;
cout << "\n Su Mo Tu We Th Fr Sa\n";
size_t start_day = day_of_week (1U, month, year);
size_t days = days_in_month (month, year);
size_t count = start_day % 7U + 1U;
for (size_t i = 1; i <= count; i++)
cout << setw (4) << ' ';
for (size_t i = 1; i <= days; ++i)
{
cout << setw (4) << i;
++count;
if (count == 7)
{
cout << endl;
count = 0;
}
}
if (count)
cout << endl;
cout << endl;
}
size_t ask (string prompt)
{
for (;;)
{
cout << prompt;
string input;
cin >> input;
stringstream ss;
ss << input;
size_t reply;
if (ss >> reply)
return reply;
cout << "Invalid input.\n";
}
}
int main (void)
{
for (;;)
{
enter_month:
size_t month = ask ("Enter a month (1 - 12, 0 to exit): ");
if (!month)
break;
if (month > 12)
{
cout << "Invalid month." << endl;
goto enter_month;
}
enter_year:
size_t year = ask ("Enter a year (>= 1900, 0 to exit): ");
if (!year)
break;
if (year < 1900)
{
cout << "Invalid year." << endl;
goto enter_year;
}
print_calendar (month, year);
}
}
using break; statement
The statement that immediately terminates a loop is the break statement. When encountered, it exits the loop regardless of the loop's condition, allowing the program to continue executing the code following the loop. This can be particularly useful for stopping the loop based on certain conditions that may arise during its execution.
In some programming languages like Python, the indentation of the text indicates how nested it is and is required for a loop to function properly. However, for most languages, indenting the body of the loop is simply for style and readability.
The Break statement should generally get executed in some loop or switch statement. The below code will give compiler error. void main() { printf("12"); break; printf("14"); }
That statement is definitely TRUE. This is the major concern with using while loops and why you need to be very careful using them. A false statement inside the loop would cause its immediate termination. That is desired in all cases.
In Java, you can use the "break" statement within a "for" loop to exit the loop prematurely. When the "break" statement is encountered, the loop will immediately stop executing and the program will continue with the code after the loop.
using break; statement
The statement that immediately terminates a loop is the break statement. When encountered, it exits the loop regardless of the loop's condition, allowing the program to continue executing the code following the loop. This can be particularly useful for stopping the loop based on certain conditions that may arise during its execution.
If you are using for loop for(;;); or you can also define condition and iterations but the loop has to close there itself without any statement inside it. In the similar way you can define while and do while loop without any statement.
In some programming languages like Python, the indentation of the text indicates how nested it is and is required for a loop to function properly. However, for most languages, indenting the body of the loop is simply for style and readability.
The Break statement should generally get executed in some loop or switch statement. The below code will give compiler error. void main() { printf("12"); break; printf("14"); }
The syntax for writing a loop in pseudo code typically involves using keywords like "for", "while", or "do-while" to indicate the type of loop, followed by the loop condition and the code block to be executed within the loop.
That statement is definitely TRUE. This is the major concern with using while loops and why you need to be very careful using them. A false statement inside the loop would cause its immediate termination. That is desired in all cases.
The statement used to take control to the beginning of a loop is typically the continue statement. When executed, it skips the remaining code in the current iteration of the loop and jumps back to the loop’s condition check to determine if the loop should execute again. This is commonly used in loops like for and while in programming languages such as Python, Java, and C++.
Programming elemental structural building blocks. Selection- This structure is used when a set of statements needs to be executed upon fulfillment of some conditions. The following two statements are used to implement selection: l The if statement / if-else statement l The switch statement Iteration-The execution of a piece of code iteratively. I.e. execution of code (a piece of code) more than once to achieve your results. Achieved by using: l The for loop l The while loop and l The do while loop Sequential- Execution of a program sequentially one by one. this according to programming. by, Prize Mutua, Kenya Programming elemental structural building blocks. Selection- This structure is used when a set of statements needs to be executed upon fulfillment of some conditions. The following two statements are used to implement selection: l The if statement / if-else statement l The switch statement Iteration-The execution of a piece of code iteratively. I.e. execution of code (a piece of code) more than once to achieve your results. Achieved by using: l The for loop l The while loop and l The do while loop Sequential- Execution of a program sequentially one by one. this according to programming. by, Prize Mutua, Kenya
A = 5do{statement;A = A + 1;} while (A < 10)
For loop: A for loop is a control flow statement that repeats a block of code a set number of times based on a predefined condition. It is commonly used when you know in advance how many iterations are needed. While loop: A while loop is another control flow statement that repeats a block of code as long as a specified condition is true. It is useful when you do not know in advance how many times the code needs to be executed.