Difference between while & do while loops in C

C  Loop control statement

Loop control statements in C are used to perform looping operations until the given condition is true. Control comes out of the loop statements once condition becomes false.

Types of loop control statements in C:

There are 3 types of loop control statements in C language. They are,
    1. for
    2. while
    3. do-while

Output:

0 1 2 3 4 5 6 7 8 9

Example program (while loop) in C:

      In while loop control statement, loop is executed until condition becomes false.

Output:

3 4 5 6 7 8 9

Example program (do while loop) in C:

      In do..while loop control statement, while loop is executed irrespective of the condition for first time. Then 2nd time onwards, loop is executed until condition becomes false.

Output:

Value of i is 1
Value of i is 2
Value of i is 3
Value of i is 4

Difference between while & do while loops in C:

S.no
while
do while
1 Loop is executed only when condition is true. Loop is executed for first time irrespective of the condition. After executing while loop for first time, then condition is checked.

Special Operator in C

Special Operators in C:

    • Below are some of special operators that C language offers.
 S.no
Operators
Description
1
&
This is used to get the address of the variable.
Example : &a will give address of a.
2
*
This is used as pointer to a variable.
Example : * a  where, * is pointer to the variable a.
3
Sizeof ()
This gives the size of the variable.
Example : size of (char) will give us 1.

Example program for & and * operators in C:

    • In this program, “&” symbol is used to get the address of the variable and “*” symbol is used to get the value of the variable that the pointer is pointing to. 

Output:

50

Example program for sizeof() operator in C:

    • sizeof() operator is used to find the memory space allocated for each C data types.

Output:

Storage size for int data type:4
Storage size for char data type:1
Storage size for float data type:4
Storage size for double data type:8

Relational Operators in C

Relational operators in C:

Relational operators are used to find the relation between two variables. i.e. to compare the values of two variables in a C program.

S.no
Operators
Example 
Description
1
>
x > y
x is greater than y
2
<
x < y
x is less than y
3
>=
x >= y
x is greater than or equal to y
4
<=
x <= y
x is less than or equal to y
5
==
x == y
x is equal to y
6
!=
x != y
x is not equal to y

    • In this program, relational operator (= =) is used to compare 2 values whether they are equal are not.
    • If both values are equal, output is displayed as ” values are equal”. Else, output is displayed as “values are not equal”.
    • Note : double equal sign (= =) should be used to compare 2 values. We should not single equal sign (=).

Output:

m and n are not equal