C do while loop
Description
do...while loops are almost same like while loops, except the condition, is checked at the end of the loop, instead of at the beginning. Therefore the code in the do-while loop will always be executed at least once. The general form is shown below :
{
statement(s);
}while (condition)
The sequence of operations is as follows :
1. execute the code within the braces
2. check the condition (boolean expression) and if it is true, go to step 1 and repeat
3. this repetition continues until the condition (boolean expression) evaluates to false.
See the flowchart :
Nested do while loop
The following program will print out a multiplication table of numbers 1,2,…,n. The outer do-while loop is the loop responsible for iterating over the rows of the multiplication table. The inner loop will, for each of the values of colnm, print the row corresponding to the colnm multiplied with rownm. Here we use the special "\t" character within printf() function to get a clear output.
#include <stdio.h>
main()
{
int rownm,nrow,colnm;
rownm=1;
colnm=1;
printf("Input number of rows for the table : ");
scanf("%d",&nrow);
do
{
colnm=1;
do
{
printf("%d\t",rownm*colnm);
colnm++;
}
while(colnm<=nrow);
rownm++;
printf("\n");
}
while(rownm<=nrow);
}
Output:
Previous: C while loop
Next: C array
C Programming: Tips of the Day
Static variable inside of a function in C
The scope of variable is where the variable name can be seen. Here, x is visible only inside function foo().
The lifetime of a variable is the period over which it exists. If x were defined without the keyword static, the lifetime would be from the entry into foo() to the return from foo(); so it would be re-initialized to 5 on every call.
The keyword static acts to extend the lifetime of a variable to the lifetime of the programme; e.g. initialization occurs once and once only and then the variable retains its value - whatever it has come to be - over all future calls to foo().
Ref : https://bit.ly/3fOq7XP
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework