Programming C: Posts 7 – WHILE loop, do…while trong C

In the previous post, we became acquainted with the loop with the utility that we repeated several times. This article we continue to learn 2 Other types of loops have similar functionality using loop but there is a little different loop while and do…while.

Are you aware of the websites that we need to sign in? As facebook or google simple. If you write the wrong username or password will not be able to sign despite how many times you enter more. Every wrong is they catch us in again. Obviously in qualifying for we often see a for loop repeat some specific times from 1 to n,… But the log does not know how many times that only new is true, the new ending. That is how the while loop and so…while.

1. WHILE loop

While loop structure

WHILE loop

Structure, flowcharts, and how it works while loop

Example 1:

Write a program in any number until it stops enter negative numbers.

#include <stdio.h>

int main()
{
    int x = 1; // make x >= 0 is true -> while will run

    while (x >= 0)
    {
        printf("Nhap vao mot so : ");
        scanf("%d", &x);
    }

    printf("Ok, Program is finish!\n");

    return 0;
}

Noted: Above you just declare just assign a value to the variable x is 1 to ensure that the original while loop conditional right to execute the command inside, then each entry, then enter that's what x received. If not set to start 1 it can x will receive 1 random value and can make the loop sound fails.

Result:

Enter some : 4
Enter some : 5
Enter some : 2
Enter some : 0
Enter some : 1
Enter some : -3
Ok, Program is finish!

Example 2:

Print the numbers 9 about 0 Descending. The number is located on 1 current.

Normally you would think of loop. ok standard. But here I want you to know how to use the while well done.

#include <stdio.h>

int main()
{
    int n = 10;

    while (n--)
    {
        printf("%d ", n);
    }

    printf("\n");

    return 0;
}

Very simple. But note a bit of condition in the while loop in the code above is what?

That is the other n 0 or not. when writing while (n--) ie check other n 0 or not or can be understood as the right n (right are 0) the print job number n. still work n-- then just subtract n 1 alone unit. And an important note in the results:

9 8 7 6 5 4 3 2 1 0

Why 0 still in print while under analysis, when n = 0 ie condition is false, wrong, they must escape rather? It is due to expression n-- our. When n = 1 then n-- will n value 0. But by the expression except in the back n it made after the inspection order n = 0. Ie when n = 1 the expression while(n--) still correct and then drop the new n 0 and perform the job, so when will print printed numbers 0.

2. Loop…while

Loop structure…while

Loop ... while

Structure, flowcharts, and how it works loop…while

Example 1

Save before your facebook password. Write a program to enter a password to login.

#include <stdio.h>
#include <string.h> // for strcmp function

int main()
{
    char p[20] = "Iloveyou";      // pass saved
    char pass[20];              // pass must enter

    do 
    {
        printf("Enter your password: ");
        gets(pass);
    } while ( strcmp(p, pass) != 0);

    printf("Ok. You are login success!\n");

    return 0;
}

Result:

Enter your password: offices
Enter your password: Nguyen Van agencies
Enter your password: iloveyou
Enter your password: iloveyou
Ok. You are login success!

Noted: In the above code, I have used the function strcmp to compare 2 Chain, This function is located in the library string.h

Strcmp function compares strings and string p pass and gives results:

  • 1 If p is greater than the pass
  • 0 If p same pass
  • -1 If p is less than pass

So we have conditions strcmp(p, pass) != 0 ie 2 This sequence is not identical.

Detailed comparison 2 Chain, your reference at all Compare 2 strings in C – strcmp

3. Compare, using loop, while, do…while

  • Loop commonly used to know the number of iterations determined.
  • Usually while loop, do…while using the unknown number of iterations
  • When calling the while loop, do…while, if the condition is false, while loop will not be performed once, but loop…while realizable 1 time.

Exercise

  1. Write a program that repeatedly enter a character work and print out the ASCII code of that character, when to enter 0 then stop. ( Refer ASCII encoding )
  2. Write a program that prints out letters: “Do you love me?”, And for users to enter c (have) or k (not). When entering c, stop program, even if they themselves begin entering reenter k ^^. (Send girlfriend / boyfriend)
  3. Enter 1 number, analysis of which achievement of the prime factors.
  4. Write a program to find USCLN, BSCNN of 2 number.