Introduction to C
Data Types in C
Operators in C
Hello World Program
Loops in C
Single Dimensional Array
Two Dimesnional Arrays
Array Example Programs
We use if statement when we have to enter in a section of code on the basis of some condition. For example, we use if statement to check if the username & password entered by user is correct then we allow him to do the particular task. Otherwise that area is restricted for him/her.
Whatever we write inside the if statement will execute only when the condition is true. If the condition is false, it will not execute. True returns a non-zero number(say 1) and false evaluates to zero.
The structure of 'if' statement is as follows:-
Note:- dashed lines show that some code will be there.
Now, if the condition is true then only the code written inside of if will execute. Otherwise it will not execute.
Now, here we see that 20 > 6 is true. So, it will go inside of if and will execute the printf statement and print the statement 'Condition is true'.
We use else when the condition in if evaluates to zero. But, its not necessary to put else everywhere you used if. It depends on your requirement. Suppose there is a condition. On the basis of that condition, you want to perform some operations. If the condition is true then some operations and if the condition is false then some different operations.
Syntax for if else
Output:- As here, the condition in if is not true, so else part will execute and it will print "condition is false" on the screen.
We use elseif when we have to execute some different instructions on the basis of some different conditions.
Syntax for if, else, else if is as follows:-
Example with a program using if, else, else if
C Program to enter percentage of marks and assign grade accordingly.
Another Example: C Program to check a number is odd or even
Note: If find any issues/errors with the tutorial above, please immediately mail it to firstname.lastname@example.org