Conditions in C

Conditions in C OR in any programing languages are important to understand as condition means you want to run some code but in a specific condition. Let's consider a variable and we make a simple condition on it.

#include <stdio.h>
int main() { 
int $percent= 70;
if($percent > 60){
    printf("Congratulation you are pass");
}
    return 0;
}

i am adding a full running code so students can run it any c compiler environment. For understanding code structure first line is use to add a header file and it contains most of the c language functions and then we use int main function. Main function is basically starter of C application if you have multiple function in your coding but when you run it in a c compiler it searches for main function and start running application from main function so you can say main is your c program starting point and as we don't define any return type so at the end, we should return 0.

let's back on main point conditions in c we are here only giving one condition so if variable is having value greater than 60 then it's prints otherwise nothing shown so let add the else case here.

#include <stdio.h>
int main() { 
 int $percent= 60;
 if($percent > 60){
    printf("Congratulation you are pass");
 }
 else{
     printf("Bad! you are Fail");
 }
    return 0;
}

In the above example i give variable value to 60 as our condition is greater than 60 so else case will run and Bad message will print.

We can run multiple Conditions in C with nested if. Condition block will be like this.

if(condition){
 // code block
}
else if(condition){
// code block
}
// else if as many as you need
else{
// if above no condition run then else will run
}

We may have as many as condition here and at then there might be else block so we can use or not else block it's depended on our requirements some time we need else but some time we don't need so if required use it otherwise we can skip it.

#include <stdio.h>
int main() { 
 int $percent= 60;
 if($percent < 33){
    printf("Bad! you are Fail");
 }
 else if($percent < 60){
     printf("Congratulation you are pass with grade C");
 }
 else if($percent < 80){
     printf("Congratulation you are pass with grade D");
 }
else if($percent <= 100){
     printf("Congratulation you are pass with grade A");
 }
 else{
     printf("Value is greater than 100");
 }
    return 0;
}

We have multiple comparing cases e.g. (==, >, <, >=, <=, != ) etc... will discuss it more in related lectures. One point i need to explain here when i start condition is less than 33 so condition cover from below value if you start from less than 100 then every time first condition will run and according to condition rule when one condition is true in all nested block then compiler will stop and skip all other condition so when make conditions, we need to understand this point fully.