C PROGRAMMING: CONTROL STATEMENTS
There are two types of control statements:
1. Decision control statements
2. Loop control statements
Now let’s begin this tutorial with a program which takes a number as an input and if the number is even it will print “even number” else it’ll print “odd number”.
#include int main() { int num; printf(“enter the number\n”); scanf(“%d”,&num); if(num%2==0) { printf(“even number”); } else { printf(“odd number”); } return 0; }
Now, we have the program which checks whether a number is even or odd or simply we can say it is divisible by 2 or not. So let’s break the code and start understanding.
#include<stdio.h>
This is a pre-processor directive which imports header file “stdio.h”. It is a header file which contains standard input and output library functions such as “printf”,”scanf” etc.
int main() {
Every C program has a “main” function. Every “main” function returns an integer value so its data type is “int”. In some cases it can be void as supported by the compiler. The parentheses after main keyword can arguments between .Them we shall discuss about it once we get started with functions
The opening braces denote the beginning of the function definition which contains the statements that belong to the main function.
int num;
“num” is a variable whose data type is “int”. Any variable used in a program must be declared first, here the keyword “int” specifies the type of variable “num”, that is, integer type of data or in other words num is a variable that holds integer value . To end statements in c we use semicolon (;).
printf(“enter the number\n”);
This is a library functions defined in “stdio.h”. It just prints the strings written within the double quotes (“…”) on the screen. “\n” is known as new line character, used to take the cursor to the next line.
scanf(“%d”,&num);
This is a library functions defined in “stdio.h”. It takes the input from the user and stores it in a variable. Here, “%d” is a format specifier used for integer data type, this means, the user input is taken as an integer value and later it is stored at the address of the variable num. “&num” denotes the address of the variable “num”.
if(num%2==0) { printf(“even number”); }
Now here comes the main motive for which I have written this article. “if” statements ,it is a decision control statements .It’s syntax is “if(condition)”.In our program the condition is “num%2==0”.here “%” is a modular division operator. It is an arithmetic operator which is used to obtain the remainder when the number to the left hand side is divided by the number on the right hand side. Now if the “if” condition is satisfied then it will execute the statement written within the curly braces.
else { printf(“odd number”); }
If the condition within the “if” doesn’t satisfy then it will execute the statement within the curly braces after the “else” keyword, that is, the “printf” statement in this case.
return 0; }
As we have noticed earlier that the “main” function defined with an integer data type, this simply means that it returns an integer value. Here in this case it is zero, zero means success. If the “main” function failed to do its desired work then, in that case we can return a non-zero number which would indicate a failure.
That’s it for this article we will continue with control statements in my next article.
Thanks for reading, hope you enjoyed. Any doubts or any comments are always welcome…
No comments:
Post a Comment