do-while Loop In C Programming Language

In this video tutorial lets learn about the general syntax and working of do-while loop in C programming language.

Related Read:
while loop in C programming
For Loop In C Programming Language
Using Scanf in C Program

Video Tutorial: do-while Loop In C Programming Language


[youtube https://www.youtube.com/watch?v=iGZACvW1HhA]

YouTube Link: https://www.youtube.com/watch?v=iGZACvW1HhA [Watch the Video In Full Screen.]

Source Code: do-while Loop In C Programming Language

#include<stdio.h>

int main()
{
    int count = 1;

    do
    {
        printf("Apple\n");
        printf("IBM\n");

    }while(count > 5);

    return 0;
}

Output
Apple
IBM

Note: Even though the while condition is false, the code inside do block gets executed atleast once.

Source Code: do-while Loop In C Programming Language

#include<stdio.h>

int main()
{
    char ch;

    do
    {
        printf("Apple\n");
        printf("IBM\n");

        printf("Do you want to continue?(y/n)");
        scanf("%c", &ch);

        fflush(stdin);

    }while(ch == 'y');

    return 0;
}

Output
Apple
IBM
Do you want to continue?(y/n)y
Apple
IBM
Do you want to continue?(y/n)y
Apple
IBM
Do you want to continue?(y/n)y
Apple
IBM
Do you want to continue?(y/n)y
Apple
IBM
Do you want to continue?(y/n)n

Working of do-while Loop

Unlike in while and for loop, in do-while loop the statements inside do block gets executed atleast once. After executing the statements present in do block atleast once, the condition present in while is checked. If while condition is true, then the block of code in do{} gets executed once again. If condition in while is false then the control exists do-while loop.

Note: Since we might start to input information from the keyboard repeatedly inside do-while block, scanf() method keeps checking the input buffer. And often times it gets confused with the input buffer and thinks that the user has pressed the enter key. To avoid that we flush out the previous buffer present in input device(ex: keyboard) using function fflush(). fflush takes stdin as argument, so that it can clear the buffer of standard input device. fflush(stdin);

Source Code: Infinite Looping in do-while Loop – In C Programming Language

#include<stdio.h>

int main()
{
    do
    {
        printf("Apple\n");
        printf("IBM\n");

    }while(1);

    return 0;
}

Output
do-while loop gets into infinite loop as the condition in while is non-zero number, which means the condition is always true.

Apple
IBM

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Mixins in Jade: Node.js

Mixins in Jade are much like functions/methods in many other programming languages.

mixin-jade-express-nodejs

Basically, if you’re repeating your code at many places, you could place that code inside a mixin and replace your code with the mixin call. This would make your code cleaner and maintainable over long run.

Related Read: Loops and Conditions In Jade: Node.js

mixins in Jade
mixin.jade

1
2
3
4
mixin fetch(users)
 ul
 -each user in users
  li= user

This would get the users as argument – loop through each user and put the individual user in the list item of an unordered list.

include in index file: Jade
index.jade

1
2
3
4
5
6
7
8
9
include mixin
 
- users = ["Apple", "IBM"];
mixin fetch(users)
 
<br /><br />
 
- users = ["Google", "Amazon"];
mixin fetch(users)

Here we have included the mixin.jade file, and now use our fetch mixin and pass-in our array variable, which is converted into an unordered list of items and output on the browser.

Mixins in Jade: Node.js


[youtube https://www.youtube.com/watch?v=g6DPkVpfauc]

YouTube Link: https://www.youtube.com/watch?v=g6DPkVpfauc [Watch the Video In Full Screen.]



Note: Clearly, in real-time applications the array or object come from the database.
Example: You might encounter the situation to list out all the users in your list and list of all the participating companies. Or list of all companies someone has worked at. In such cases you’ll get an array of objects which you need to out put to the user in a well formatted manner. In situations like this, mixins are a great tool to have, to avoid the necesity to repeat your code allover your application.

Have all your mixins in a single file and then include that file wherever you are calling your mixins.