if statement in c language || if else in c programming || C notes

if statement in c language || if else in c programming || C notes

 if statement in c language

 If statements are a fundamental concept in programming, and the C programming language is no exception. An if statement allows a program to execute different sections of code depending on whether a certain condition is true or false. In this article, we will explore the basics of if statements in C and how to use them effectively in your code.
    

Syntax of the If Statement


The syntax of an if statement in C is as follows:

if sybtax in clanguage

The condition is a Boolean expression that evaluates to either true or false. If the condition is true, the code inside the curly braces will be executed. If the condition is false, the code inside the curly braces will be skipped and the program will move on to the next statement.

Let's look at an example to help illustrate this concept:


if wxample program in c languge

In this example, the condition is x > 3, which is true because x is equal to 5. Therefore, the program will execute the code inside the curly braces and print "x is greater than 3" to the console.

Else Statement


What if we want to execute a different section of code when the condition is false? This is where the else statement comes in. The syntax of an if-else statement in C is as follows:

if else statment syntax

Let's modify our previous example to include an else statement:

if else program in c langauge

In this example, the condition is x > 3, which is false because x is equal to 1. Therefore, the program will skip the code inside the first set of curly braces and execute the code inside the else statement. The output will be "x is less than or equal to 3".
 

Else-If Statement


Sometimes we may want to test multiple conditions and execute different code blocks based on which condition is true. This is where the else-if statement comes in. The syntax of an if-else-if statement in C is as follows:

if else program

Let's look at an example:



In this example, the first condition (x > 10) is false because x is equal to 10. The second condition (x < 10) is also false because x is not less than 10. Therefore, the program will execute the code inside the else statement and output "x is equal to 10".

Conclusion

If statements are a powerful tool in programming, allowing us to execute different sections of code based on different conditions. By using the if statement, along with the else and else-if statements, we can create complex programs that make decisions based on user input, system status, or other factors. It is important to master the basics of if statements in C

watch Video 


All C  Topics : -

  1. C Language Intro

  2. Structure of C Language 

  3. Variable & data Types 

  4. Printf and Scanf Function

  5. Operator in c Language 

  6. If Statement in c Language 

  7. If else and Nasted if in C

  8. Switch Case In C 

  9. While loop in C Language 

  10. Exit Control Loop (Do While)

  11. For Loop in c 

  12. C Language Pratice Program 

  13. Goto Statement in c Programming 

  14. Array in c

  15. Pointer in  c Language 

  16. String in c Language 

Post a Comment (0)
Previous Post Next Post