Skip to main content

Conditional Statements in C

Conditional Statements

In c programming language, decision making is used to specify the order in which statements are executed. In C we use conditional statements to achieve this.

There can be any type of control structures. In our daily life, these kinds of control structures generally achieve or used in our decision-making process.

Control Structures types.
If then            If I exercise daily then I will be fit
If else             If today rains then I will play FIFA else I will not play FIFA.
else if     

In programming, we use a conditional statement to make decisions in the same way as in real life.
If Statement Example
if the expression it true evaluate the statement.
 if (expession) {  
     statement  
 }  
C example. You have given a number, check if the number is positive print Positive on the terminal.
 if (number > 0) {  
     printf("Positive");  
 }  
If Else Statement Example
If the expression is true to evaluate statement 1 otherwise evaluate statement 2
 if (expression) {  
     statement 1  
 } else {  
     statement 2  
 }  
C example. You have given a number, check if the number is positive or not positive and print Positive or Not Positive on the terminal.
 if (number > 0) {  
     printf("Positive");  
 } else {  
     printf("Not Positive");
 }  
Else If Example
if expression 1 is true to evaluate statement 1 if expression 1 is false check expression 2 if it is true to evaluate statement 2 otherwise evaluate statement 3
 if (expression 1) {  
     statement 1  
 } else if (expession 2) {  
     statement 2  
 } else {  
     statement 3  
 }  

C example. You have given a number, check number is positive, negative or zero and print on terminal positive, negative and zero in respective cases. 
 if (number > 0) {  
     printf("Positive");  
 } else if (number == 0) {  
     printf("ZERO");  
 } else {  
     printf("Negative"); 
 }  

Example: Take an input from a user and check number is positive negative or zero.
 #include<stdio.h>  
 int main() {  
      int number;  
      scanf("%d", &number);  
      if (number > 0) {  
     printf("Positive");  
      } else if (number == 0) {  
     printf("Zero");  
      } else {  
     printf("Negative");  
      }  
 }  


Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.





Comments

Post a Comment

Popular posts from this blog

Time, Speed and Distance

Time, Speed And Distance  In this post, We will learn about time, speed and distance. Important Formula Distance = Time x Speed Time = Distance/Speed Speed = Distance/time Important Units Distance: meter (m), kilometre (km) Time: Hour (h), second (s) speed: meter/second (m/s), kilometre/hour  (km/h) Important Conversion 1 km = 1000 meter 1 h = 3600 s 1 km/h = (5/18) m/s 1 m/s = (18/5) km/h Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 

Joint Entrance Exam (JEE): Probability Syllabus

JEE: Probability Syllabus Probability  is a very interesting and different topic of mathematics which doesn't require your previous knowledge of any other chapters other than permutation and combination.  Following topics are the part of Jee syllabus Addition and multiplication rules of probability Addition and multiplication rules of probability Conditional Probability Bayes Theorem Independence of events Computation of probability of events using permutations and combinations Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Haryana Public Service Commission: HPSC 2018

Haryana Public Service Commission: HPSC 2018 Company Name         :  Haryana Public Service Commission: HPSC 2018 Recruitment Post Name                       :   Civil Judge State                            :  Haryana Salary                           :  Rs   27,700-44,770 Number of Vacancy   :  107 Eligibility                     :  Bachelor's Degree in Law Last Date to Apply     :  30-09-2018 Application Details    :  Notification Apply Online              :  Apply Here Government Jobs In  Haryana Please write comments if you f...

Securities and Exchange Board of India Recruitment 2018

Securities and Exchange Board of India  Company Name SEBI Post Name Assistant Manager State India Salary RS 28150-55600 Number of Vacancy 120 Eligibility Graduation Last Date 07-10-2018 Application Details Job Notification Apply Online Job Application Securities and Exchange Board of India (SEBI) Recruitment 2018    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.