#include <stdio.h>
#include <conio.h> void main(void)
{
int i, in, is = 0; printf("Enter number n: "); scanf("%d", &in);
is = 0;
Maybe you are interested!
-
Basic Programming - Hanoi Industrial College - 12 -
Database Programming - Hanoi Industrial Vocational College - 22 -
Database Programming - Hanoi Industrial Vocational College - 1 -
Database Programming - Hanoi Industrial Vocational College - 14 -
Basic Programming with Python - 2
for(i = 0; i<=in; i++)
{
if (i % 2 != 0) //if i is equal is = is + i; //or is += i;
}
printf("Total: %d", is); getch();
}
Print results to screen
Enter number: 5 Total: 9.
Note 3:
We can combine the commands in the for body into the for command. However, when programming, we should write the for command with 3 simple expressions and the commands executed in the for body should be one line each so that later it can be read again easily, understood easily, and corrected easily.
Example 4: Some examples of changing loop control variables.
- Change the control variable from 1 to 100, increasing by 1 each time:
for(i = 1; i <= 100; i++)
- Change the control variable from 100 to 1, decreasing by 1 each time:
for(i = 100; i >= 1; i--)
- Change the control variable from 7 to 77, each time increasing by 7:
for(i = 7; i <= 77; i += 7)
- Change the control variable from 20 to 2, decreasing by 2 each time:
for(i = 20; i >= 2; i –= 2)
Example 5: Read a series of characters on the keyboard, count the number of characters entered. End when encountering a dot '.' .
Write a program
/* Read a series of numbers on the keyboard, enter the number of numbers. End when encountering a period */
#include <stdio.h>
#include <conio.h>
#define DAU_CHAM '.' void main(void)
{
char c; int idem;
for(idem = 0; (c = getchar()) != DAU_CHAM; ) idem++;
printf("Compare number: %d.n", idem); getch();
}
Print results to screen
Afser.
Number of words: 5.
Note 4: The for loop lacks expression 3
Example 6: Read a series of characters on the keyboard, count the number of characters entered. End when encountering a dot '.' ..
Write a program
/* Read a series of numbers on the keyboard, enter the number of numbers. End when encountering a period */
#include <stdio.h>
#include <conio.h>
#define DAU_CHAM '.' void main(void)
{
char c;
int idem = 0; for(; ;)
{
}
c = getchar();
if (c == DAU_CHAM) //enter the dot break; //exit the loop
idem++;
printf("Compare number: %d.n", idem); getch();
}
Print results to screen
Afser.
Number of words: 5.
Note 5: The for loop is missing all three expressions.
2. Break statement
Normally, the break command is used to exit a loop that does not specify a stopping condition or you want to stop the loop according to a condition you specify. Using the break command to exit a loop is often used in conjunction with the if command. The break command is used in for, while, do…while, switch . The break command exits the loop that contains it.
Attention:
When there are multiple nested loops or switches, the break statement only exits the deepest (innermost) loop containing the break operator.
Any break operator can be replaced by a goto operator with an appropriate label
break is often used when the for loop can perform fewer iterations than the predetermined number of iterations, in the body of a switch and in cases where we have to check the termination condition of the while loop inside its body.
Example 7: Same as example 6
Use the break statement in a switch to skip the remaining next statements.
3. Continue command
In contrast to the break operator (used to exit a loop), the continue statement is used to start a new loop of the deepest loop containing that operator. Used in for, while, do…while loops . When the continue statement executes, control will be transferred to the conditional expression of the nearest loop. That is, going back to the top of the loop, all the following statements in the loop containing continue will be skipped and not executed.
When encountering the continue operator inside the body of a for operator, the machine will skip the remaining statements in the for body (after continue) and move on to execute expression 3 to start the next new loop.
When encountering the continue operator inside the body of a while or do…while loop, the machine will skip the remaining commands in the loop body (after continue) and move to the Expression Check after the while keyword to start the next loop (if the Expression is still true ) or exit the loop (if the Expression is false ).
Example 8: Enter a series of integers from the keyboard until you reach 0 then stop. Print the sum of all positive integers.
Write a program
/* Enter a string of integers from the keyboard until it reaches 0. Print the sum of all integers */
#include <stdio.h>
#include <conio.h> void main(void)
{
int in, itong = 0; for(; ;)
{
printf("Enter an integer: "); scanf("%d", &in);
if (in < 0)
continue; //in < 0 go back to the beginning of the loop if (in == 0)
break; break; //in = 0 exit lap itong += in;
}
printf("Total: %d.n", itong); getch();
}
Print results to screen
Enter an integer: -8
Enter an integer: 9
Enter an integer: -7
Enter an integer: 3
Enter an integer: 0
Total: 12
_
4. The while statement
The loop executes repeatedly while the expression is true.
Command syntax
while (expression) block of code;
The while keyword must be written in lowercase
If a block of commands includes 2 or more commands, it must be placed in { brackets.
}
Flowchart

First the expression is checked if it is false then the while loop ends
(block of statements is not executed once)
If true, execute the block of code;
Repeat expression testing
+ Expression: can be one expression or many sub-expressions. If there are many sub-expressions, they are separated by commas (,) and the truth or falsity of the expression is determined by the last sub-expression.
+ The while body (command block) can contain one or more other control structures.
+ In the while body, you can use the continue command to move to the beginning of the loop (skipping the remaining statements in the body).
+ To exit the while loop at will, you can use the following commands:
break , goto, return like for statement .
Example 9: Write a program to print the sentence "Example using while loop" 3 times.
Write a program
/* Program prints the sentence "Example using while loop" 3 times */
#include <stdio.h>
#include <conio.h>
#define MSG "For use with lap while.n" void main(void)
{
int i = 0;
while (i++ < 3)
printf("%s", MSG); getch();
}
Print results to screen
Because of using lap while. Because of using lap while. Because of using lap while.
Example 10: Write a program to calculate the sum of integers from 1 to n, with n entered from the keyboard.
Write a program
/* Program to calculate the sum of integers from 1 to n */
#include <stdio.h>
#include <conio.h> void main(void)
{
int i = 0, in, is = 0; printf("Enter number n: "); scanf("%d", &in);
while (i++ < in)
is = is + i; //or is += i; printf("Total: %d", is);
getch();
}
Print results to screen
Enter number: 5 Total: 15.
Example 11: Replace the line for(; (c = getchar()) != DAU_CHAM; ) in example 4 with the line while ((c = getchar()) != DAU_CHAM)) Run the program again, observe and comment on the results.
5. The do…while statement
The loop executes repeatedly until the expression is false.
Command syntax
command block;
while (expression);
The keywords do and while must be written in lowercase.
If a block of commands includes 2 or more commands, they must be placed in brackets { }
Flowchart





