Programming C: Posts 5 – Loop structure
Notification, The article was updated details, complete and easy to understand in Programming C : Posts 6 - The for loop in C and Programming C: Posts 7 - while Loop, do…while trong C
Content
A.Lenh unconditional branch
1.Break statement:
Usually the break statement to exit the loop does not define the conditions you want to stop or stop loop under conditions you specify Using the break statement to exit the loop is often used in conjunction with an if statement. The command used for break, while, do…while, switch. Break command exit the loop containing it.
In the previous article we use the break statement in switch to skip the rest the next statement.
2.Goto
Goto command to move to a certain command behind the label specified. The label is the name of the command precede the command that we should jump to. Goto combined with an if statement can also create a loop.
Goto command syntax: goto label;
Syntax label: entitled : command;
#include <stdio.h> int main(){ int a, kt; tieptuc : printf("Nhap a = "); scanf("%d", &a); switch(a) { case 1: printf("Mot"); break; case 2: printf("Hai"); break; case 3: printf("Ba"); break; case 4: printf("Bon"); break; case 5: printf("Nam"); break; default: printf("Khong phai so tu 1 den 5"); } printf("\nBan muon tiep tuc? 1 de tiep tuc, 0 de dung: "); scanf("%d", &kt); if(kt) goto tieptuc; return 0; }
However when you use goto to note the following points:
-If goto and labels in 1 goto function is only allowed to jump in the function and not allowed to jump to another function.
-Do not use goto to jump from outside to inside a block but was allowed to jump out of the blocks.
3.Continue statement
Used in the loop, while, do…while. When the continue command execution will hand over control of the conditional expression of the loop closest. That is the upside to the top of the loop, All orders come later in the loop will continue to be ignored contains no enforcement.
Now we are going to learn the command loop so that more clearly the role of the unconditional branch instruction
[qads]
B.Cac command loop
1.Orders for
Loop determine perform repeated a number of times determined by a (or a string) act.
The expression :
- Expressions 1: initialize the initial value for the variable control.
- Expressions 2: is logically related to the current conditions continue to loop.
- Expressions 3: use change assignments tribien control.
How it works:
- Step 1: Determining the value of the expression 1
- Step 2: Determining the value of the expression 2 (test conditions ie true or false 1 or 0). Depending on the value of the expression 2 which follow 2 direction:
- + If false (0) will exit the loop
- + If properly (1) will perform in the ring for the command block to the meeting point } at the end of the loop or continue statement will irrigate step 3.
- Step 3: Calculate the value of expression 3, then go back to step 2.
Noted:
- Expressions 1 always be calculated only once when dialed for.
- Expressions 2, 3 and for possible relatives repeatedly.
- Expressions 1, 2, 3 must be separated by a semicolon ( ; ), expressions may be absent 1,2 or both 3 but this must be raised semicolon.
- If the expression 2 no, loop is considered to be always right. Want to escape the loop in order to take a break, goto hoặc return.
- For each expression can be written as a series of human expressions separated by commas. Then the child is determined expression from left to right. As of right and wrong in the expression of the sequence first expression 2 is determined by the final expression.
- In the body for (block command) may contain one or more other control structures, another loop.
- When having a break statement, deepest loop structure will exit.
- In the body there for thedung goto exit the loop to the desired location.
- In the body can use for return to return to a certain function.
- In the body can use for the command continue to move to the top of the loop (skip the remaining statements in the body).
Examples of commands for.
In the example below I will perform on arrays (will learn in the next post) Therefore, if you are not clear about the array is just interested in the command structure for Our need not concern arrays.
VD1: Use within for in the number of atoms 1 to 10
#include <stdio.h> int main(){ int i; for (i = 1; i <= 10; i++) { printf("%-5d", i); } return 0; }
VD2: Use a for loop to reverse the array in which the expression of a for loop is a sequence of human expressions separated by commas.
#include <stdio.h> int main(){ int i, j, n = 4, a[] = {1, 3, 7, 2}; // mang a gom 4 phan tu /* Xuat mang ban dau*/ printf("Mang ban dau: "); for (i = 0; i < n; i++) { printf("%-5d",a[i]); } printf("\n"); /* Dao nguoc mang*/ for (i = 0, j = n - 1; i <= j; i++, j--) { // doi cho 2 phan tu a[i] va a[j] int temp = a[i]; a[i] = a[j]; a[j] = temp; } /* Xuat mang sau khi dao nguoc*/ printf("Mang dao nguoc: "); for (i = 0; i < n; i++) { printf("%-5d",a[i]); } printf("\n"); return 0; }
VD3: Use a for loop in order to reverse the array in the absence of such expressions 2, we use the break statement to exit the loop
#include <stdio.h> int main(){ int i, j, n = 4, a[] = {1, 3, 7, 2}; // mang a gom 4 phan tu /* Xuat mang ban dau*/ printf("Mang ban dau: "); for (i = 0; i < n; i++) { printf("%-5d",a[i]); } printf("\n"); /* Dao nguoc mang*/ for (i = 0, j = n - 1; ; i++, j--) { if (i >= j) break; // doi cho 2 phan tu a[i] va a[j] int temp = a[i]; a[i] = a[j]; a[j] = temp; } /* Xuat mang sau khi dao nguoc*/ printf("Mang dao nguoc: "); for (i = 0; i < n; i++) { printf("%-5d",a[i]); } printf("\n"); return 0; }
VD4: Illustrations continue statement in a for loop.
#include <stdio.h> int main(){ int i, n = 4, a[] = {1, -3, -7, 2}; // mang a gom 4 phan tu printf("Cac so duong trong mang: "); for (i = 0; i < n; i++) { if (a[i] <= 0) continue; printf("%-5d",a[i]); } return 0; }
2. While statement
I can understand simple: While the expression is true, then do command, the wrong exit
- Expressions: can be an expression or the expression. If the child is more expressions separated by commas (,) and wrongs of expression is determined by the final expression.
- In the body while (block command) may contain one or more other control structures.
- While in the body can use the continue command To move to the top of the loop (set too the remaining statements in the body).
- If the expression is a constant non-zero, it is true and happening infinite loop. Want to get rid of the while loop can optionally use the break statement, goto, return as the command for.
CEO: Enter password:
#include <stdio.h> #define PASS "nguyenvanquan7826" // dat pass int main(){ char pass[100]; /* Ham strcmp tra ve ket qua la 0 khi 2 chuoi bang nhau */ while(strcmp(pass, PASS) != 0) { printf("Nhap pass: "); gets(pass); } printf("Mat khau dung roi!"); return 0; }
3. Order by while
I can understand simple: Make command in the correct expression, everyone wrong exit.
From here we have commented: while loop may not execute if the expression in the first place wrong, while it will also order by executing at least 1 although expression was wrong from the start.
CEO:
#include <stdio.h> int main(){ do { printf("khoi lenh duoc thuc hien\n"); } while(5 > 8); return 0; }
Exercise
Posts 1: Write a program to input from the keyboard 1 sequence of integers. Check and print out all the prime numbers in sequence.
Posts 2: Write a program that shows conversations 2 people. When one of 2 say goodbye, stop.
[rps-include post=”2703″ shortcodes=”false”]
In the example of part 2 if (kt) goto tieptuc; why I did not see the expression compares it remains true dear
Kt variables are variables that condition you. Default in C, 1 Other integer 0 Equivalent to right, number 0 is wrong. So just call if(kt) is okay.
Waiter, %d and% different -5d place ạ ? I can substitute% to% -6d -5d or others đc not it ?
Dou minus denotes that aligns left, otherwise right-aligned. You instead of always đk.
Waiter apparently not used in strcmp command in ubuntu is not it ? I use Geany it reported “strcmp was not declared in this scope”.
I still use that.
em dùng trong dev C cũng bị lỗi như vậy
à code a bị thiếu khai báo #include nên nó lỗi như vậy
In the example section 2, printf(“\nDo you want to continue? 1 to continue, 0 easy to use: “); assuming you do not use 0 or 1 which uses letters like c or k, I wrote how that he?
That's just the job alone. Also enter the scanf command you type char is đk.
scanf(“%c”,c);
In the example section 2, printf(" NDo you wish to continue? 1 to continue, 0 easy to use: "); he asked me why only enter 0 it to stop !
Because the loop condition that you. You see the behavior of the loop, but that's ok.
s to finish work 1 lệnh if v a? e do 2 or more if it goes on despite being close interlocking braces r
So review your code alone.
e asked for Turbo code in C can write the image generated by the program are not? và nếu được thì lệnh Do..while làm nhiệm vụ với khoảng bao nhiêu vòng lặp?
This is clearly not his. Chưa thử bao giờ 😉
if (kt) goto tieptuc;
anh cho em hỏi sao ko phải là if(kt==1) goto tieptuc?
if (kt) là sao ạ @@
Trong ngôn ngữ C thì 0 có nghĩa là sai, 1 có nghĩa là đúng. Do vậy if(kt) hay if(kt == 1) là như nhau nhé.
for (i = 0, j = n-1 ; in <= j; i , j–) {
anh comment cho cái code này với ạ, em ko hiểu 🙁
Trong vòng for này thì vẫn có 3 biểu thức ngăn cách nhau bởi dấu chấm phẩy. Nhưng mỗi biểu thức lại có nhiều lệnh:
+/ Expressions 1 including 2 lệnh khởi tạo i = 0 và j = n -1;
+/ Expressions 2 including 1 biểu thức so sánh i <= j +/ Biểu thức 3 gồm 2 lệnh là tăng i và giảm j.
Waiter, nhưng nếu như vậy.
i=0, j =3 . i=1, j=2, i=2, j=1 thì nó đã dừng có nghĩa nó chỉ swap 2 lần sao nó lại swap đc 4 lần luôn ạ 🙁
swap 2 lần thì nó đổi được 4 số rồi. mỗi lần 2 số mà.
Ban đầu là: 1, 3, 7, 2
Lần 1: 2 3 7 1
Lần 2: 2 7 3 1
anh giải thích rõ như debug đc ko ạ 🙁 em vẫn ko hiểu tại sao nó swap đc như vậy
honey is i = 0, j=3, then 1 swap cho 2(corresponding order 0 1 2 3 in the array), then i = 1, j = 2, then swap 3 and 7 equivalent ( 1 2 in the array)
not his right @@
Right.
%-5d is the star so he
Means use 5 space to perform integer and left flat (minus)
Use a loop to enter 1 number, if entered incorrectly, then start in again, how could so, e mistyped it out always.
The problem is entered incorrectly, then how is wrong, conditional use. Suggests using a while loop or do-while
#include
#include
int main()
{
int a = 9, b = 1+(a++);
printf(” a = %d t b = %d”, the,b);
return 0;
}
Let me ask how to print it for a = 10 sir.
Ah because in your code there's a ++. a ++ would be equivalent to a = a + 1 so the value of a increases from 9 to 10 is correct.
Hey guys, can I ask you a question? I don't understand n in for(i=0;in<n;i )
does that mean as a.anh can answer a.Em households do not thank
No there 1 to limit the running variable i. when i < n thì vòng lặp còn hoạt động, i >= N, the loop stops.
Could you please ask me instead of using it
int temp = a[in];
the[in] = A[j];
the[j] = temp;
I used each a[in] = A[j]; severance resulting 2 7 7 2 instead of 2 7 3 1 why is that sir?
Because such use permutations can not be 2 number. I imagine there 2 glass of water: lemonade and coffee, convert 2 mugs How?
/*
——————————–
Failed to execute “F:\Nhat (IT)\Bai 5 Cau Bong trick lap Widower 5.2.1.exe”:
Error 216: This version of %1 is not compatible with the version of Windows you’
re running. Check your computer’s system information to see whether you need a x
86 (32-bit) or x64 (64-bit) version of the program, and then contact the softwar
e publisher.
Press any key to continue . . . */
Ad me asked how to remedy this with sir
Looks like the dev-c of you are not compatible with win. Check to see if you win 32 bits are used 32 bit nhé.
Yes, thank you Nguyenvanquan7826, I fix it sir
// Batp 5.3 Viet program that calculates the inverse of the first integer N by the formula T = 1 + 1/2 + 1/3 +…+1/N
#include
int main (){
int i,n;
float s;
s=0;
printf (“Click the number N la: “);
scanf (“%-5d”,&n);
for (i=1;in<=n;i ){
s+=(float)1/in;
}
printf ("Tong cua day so la: %0.2f",with);
return 0;
}
Brothers and sisters let me ask, e to write the code and run the program on, but you enter test n = 2 competition program returns the result as 15.40 not 1.50, I do not know at what point error, Her only hope he dùm Kids. E thanks!
Orders entered with% n Why -5d?
Thank you very much for your Army, Then you calculate sir!
As of right and wrong in the expression of the sequence first expression 2 is determined by the final expression.
–Ask yourself, you have examples for the current attention on not it???
CEO: for (int i = 0, j = 0; in < 3, j < 8 ; i++, j++) thì điều kiện dừng sẽ phụ thuộc vào j < 8 chứ không phải i < 3.
Can I ask you something?…When to use for sir
when you need to do something over and over again, especially knowing the number of iterations in advance.