So far we’ve seen sequential flow of our programs. Next we saw decision control statements like if, if else, else if etc. Now lets learn about loop control statements present in C programming language.
In this video tutorial lets learn about while loop in particular.
Here while is the keyword. Inside parenthesis we need to write the condition. These conditions must evaluate to Boolean value. i.e., true(non-zero number) or false(0);
The ‘body of while’ loop keeps executing until the condition is true. Control exits while loop once the condition is false.
#include < stdio.h >
int main()
{
int count = 1;
while(count <= 5)
{
printf("%d\n", count);
count = count + 1;
}
printf("End of loop\n");
return 0;
}
Output: 1 2 3 4 5 End of loop
In above C source code, variable count is initialized to 1. Inside while condition we check if variable count is less than or equal to 5. Until variable count is less than or equal to 5, the while loop keeps executing. Inside body of the while loop we increment the value of count by 1 upon each execution of the loop. The condition is checked on execution of the loop. Once variable count is more than 5, the condition becomes false count <= 5 (once count value is 6, the condition becomes false), the execution control exits while loop.
Note: Here the loop execution counter is called ‘loop counter’ or ‘index variable’.
In this video tutorial we will show you how to use Conditional Operator. They are also called Ternary Operators as they take 3 arguments.
General Form of Ternary Operator
(expression_1) ? (expression_2) : (expression_3);
expression_1 is a comparison/conditional argument. expression_2 is executed/returned if expression_1 results in true, expression_3 gets executed/returned if expression_1 is false.
Ternary operator / Conditional Operator can be assumed to be shortened way of writing an if-else statement.
C Program: Ternary Operator / Conditional Operator
#include < stdio.h >
int main()
{
int a = 1, b = 1, c;
// (expression1) ? (expression2) : (expression3);
(a+b) ? (c = 10) : (c = 20);
printf("Value of C is %d\n", c);
return 0;
}
Output: Value of C is 10
a+b is 2 which is non-zero, so it is considered as true. So c = 10 gets executed.
#include < stdio.h >
int main()
{
int a = 1, b = 1, c;
// (expression1) ? (expression2) : (expression3);
(a-b) ? (c = 10) : (c = 20);
printf("Value of C is %d\n", c);
return 0;
}
Output: Value of C is 20
a-b is 0 which is zero, so it is considered as false. So c = 20 gets executed.
#include < stdio.h >
int main()
{
int a = 1, b = 1, c;
// (expression1) ? (expression2) : (expression3);
c = (a+b) ? (10) : (20);
printf("Value of C is %d\n", c);
return 0;
}
Output: Value of C is 10
#include < stdio.h >
int main()
{
int a = 1, b = 1, c;
// (expression1) ? (expression2) : (expression3);
c = (a-b) ? (10) : (20);
printf("Value of C is %d\n", c);
return 0;
}
We illustrate the use of if else statement in this video tutorial with the help of a division of 2 numbers example program.
Division of 2 Numbers: if else construct in C
Single statement in both if and else block
#include < stdio.h >
int main()
{
int a, b;
printf("Enter value of a and b for division (a/b)\n");
scanf("%d%d", &a, &b);
if( b != 0)
printf("Division of %d and %d is %d\n", a, b, a/b);
else
printf("You can't divide a number by 0\n");
return 0;
}
Output 1: Enter value of a and b for division (a/b) 10 2 Division of 10 and 2 is 5
Output 2: Enter value of a and b for division (a/b) 50 0 You can’t divide a number by 0
Note: When condition for if is true, if block gets executed. When condition in if is false, else block code gets executed.
Multiple statements in both if and else block
#include < stdio.h >
int main()
{
int a, b, c;
printf("Enter value of a and b for division (a/b)\n");
scanf("%d%d", &a, &b);
if( b != 0)
{
c = a / b;
printf("Division of %d and %d is %d\n", a, b, c);
}
else
{
printf("You can't divide a number by 0\n");
printf("Please run the program once again and give proper value\n");
}
return 0;
}
Output 1: Enter value of a and b for division (a/b) 10 2 Division of 10 and 2 is 5
Output 2: Enter value of a and b for division (a/b) 50 0 You can’t divide a number by 0 Please run the program once again and give proper value
In above example we are enclosing if and else block code in curly braces as both if and else has multiple lines of code or block of code to be executed.
Scanf(): For user input
In above c program we are asking user to enter the values for variable a and b. You can know more about scanf() method/function in this video tutorial: Using Scanf in C Program
if else Construct in C: Division of 2 numbers logic
So far we’ve seen that the code in C program gets executed in sequential order i.e., as and when the code appears in the source code. But today lets see how we can change the sequence of or order of execution using decision control instruction like if statement.
General form of if statement
if( some_condition ) statement;
By default, if statement executes only the next instruction present under it. But if you want to execute some block of code, then you must enclose it inside curly braces as shown below.
in above code, some_condition can be anything from a simple true(non-zero number) or false(0) or any expression returning 0 or any non-zero number.
Statements inside if statements curly braces can be anything from multi-line statements, function calls, some arithmetic operations etc.
#include < stdio.h >
int main()
{
int a;
printf("IBM\n");
printf("Enter value for a \n");
scanf("%d", &a);
if(a > 10)
{
printf("Microsoft\n");
printf("Oracle\n");
}
printf("Amazon\n");
printf("Apple\n");
return 0;
}
Output: IBM Enter value for a 5 Amazon Apple
Here user entered value for variable a is less than 10. So the code inside if block doesn’t get executed. So printing of Microsoft and Oracle gets skipped.
#include < stdio.h >
int main()
{
int a;
printf("IBM\n");
printf("Enter value for a \n");
scanf("%d", &a);
if(a > 10)
{
printf("Microsoft\n");
printf("Oracle\n");
}
printf("Amazon\n");
printf("Apple\n");
return 0;
}
Output: IBM Enter value for a 14 Microsoft Oracle Amazon Apple
For the same program, user entered value for variable a as 14, which is greater than 10. So all the company names gets printed on the console window as shown in above output section.
We can even remove condition statement inside if statement
#include < stdio.h >
int main()
{
int a;
printf("IBM\n");
printf("Enter value for a \n");
scanf("%d", &a);
if(a)
{
printf("Microsoft\n");
printf("Oracle\n");
}
printf("Amazon\n");
printf("Apple\n");
return 0;
}
Output 1: IBM Enter value for a 1 Microsoft Oracle Amazon Apple
Output 2: IBM Enter value for a 0 Amazon Apple
Output 3: IBM Enter value for a -5 Microsoft Oracle Amazon Apple
In above example, when user enters 0 the condition becomes false, so the block of code inside if statement doesn’t get executed. When the user enters any number other than 0, its considered as true, so the code inside if statement gets executed.
Scanf(): For user input
In above c program we are asking user to enter the values for variable a and b. You can know more about scanf() method/function in this video tutorial: Using Scanf in C Program
Since “names” collection was empty, we inserted 2 documents into it. Now we applied drop() method on the collection, which drops all the document present in the collections at once. It also removes the document/index/content present inside “system.indexes” collection.
Note: If you want to remove/drop all the documents present inside the collection, make use of drop() method, as it removes all the documents at once, its more efficient than remove({}) method which removes documents one by one. Use remove() method, when you want to remove one or a set of documents from the collection.