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

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.

Flowchart loop
Flowchart loop

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

Save dollars while statement
Save dollars 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

Flowchart do-while statement
Flowchart do-while statement

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”]