C if condition:


Basic syntax of if conditions:



if(  //condition  ){


work


}


Logic in flow Chart:






How if statement works:


The if conditions statement works ,when our given condition (  ) are true.

When it is false ,then if conditions block will be skipped normally.

Block is the braces { }.


Example:


Question-1: Write a C program .When sum of two number is greater than 10,

print Yes.



Solutions:

here you see we don't use curly braces. Because we needed only single

line of code which will executed


#include<stdio.h>
int main(){
    int a=5,b=6;

    if((a+b)>10)
    printf("Yes");
}




Note: If we don't use {} our code will execute the next one single statement.

And if conditions will not make a block.


Question-2: Write a C program .When sum of two number is greater than 10,

print Yes and print print the sum and minus of to variable.



Solutions:


#include<stdio.h>
int main(){
    int a=5,b=6;

    if((a+b)>10){
    printf("Yes");
    printf("\nsum of tow variable:%d",a+b);
    printf("\nminus of tow variable:%d",a-b);
   

    }

}


In here we used curly braces ,so that our code can execute all line. mainly we create a block







Note: it is not necessary to must use else in ours code:



More example from GitHub: Click here



10 exercise of this topics:


Post a Comment

Previous Post Next Post