Learn C Language

Learn C Programming Language Step By Step. It’s easy to learn c programming language with real-life examples and hands-on. We are trying our best to deliver quality content.

Decision Making In C

« Previous Page                                                                                                                                Next Page »

Decision making in c is also known as a decision control instruction.

Decision control instruction requires the programmers to specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.

The general form of a decision-making structure found in most of the programming languages 

In C programming language, any non-zero and non-null values are assumed as true. In contrast, zero or null values are assumed as false.

In C programming following are types of decision making in c :

  • if 
  • if-else
  • conditional operator
  • nested if ( other than above 3)

If Statement

if a keyword in c programming which let the compiler to identify decision control instruction, immediately after if some condition is there. This condition is any valid expression in C. If the result of an expression is non-zero it is considered as true otherwise false.

Syntax

The syntax of a typical if block will look something like this 

OUTPUT

  • If you will enter the value of x greater than the value of y then the condition inside if block (x>y) will be true and if block will execute. Hence, it will print: x is greater than y (if block executed successfully)
  • And if you will enter the value of x smaller than the value of y then the condition inside if block (x>y) will be false hence, it will print nothing.

if-else statement

if-else statement is used for decision making in c language. This is similar to if but the else block is a new add-on. When the condition of if a statement is true then if block will be executed and when the condition of if block is false then else block will be executed.

It is important to mention that only one from two blocks (if block and else block) can be executed as the condition may have only two results., TRUE or FALSE.

Syntax

The syntax of a typical if-else block will look something like this

OUTPUT

  • If you will enter the value of x greater than the value of y then the condition inside if block (x>y) will be true and hence, if block will execute. And, it will print: x is greater than y (if block executed successfully)
  • And if you will enter the value of x smaller than the value of y then the condition inside if block (x>y) will be false hence, else block will execute. And, it will print: x is less than or equal to y (else block executed)

Conditional Operator

The conditional operator is an amazing and important statement used for decision making in c language. It is also known as a ternary operator which means the operator needs three operands to perform its operation. In addition, the conditional operator is represented as ( ?  :). 

Expression1 is a condition, which is first evaluated as TRUE or FALSE. If the condition is TRUE then Expression2 will be executed otherwise Expression3.

The conditional operator works similar to if-else, but you don’t have to use the keyword if and else.

OUTPUT

  • If you will enter the value of x smaller than the value of y then the condition (x>y) will be false hence, it will print: False is executed.
  • And, if you will enter the value of x greater than the value of y then the condition (x>y) will be true hence, it will print: True statement is executed.

nested if or if-else statement

When if or if-else statement is used inside other if or if-else statement then it is called as nested if or if-else statement. Because one statement is nested inside another statement.

OUTPUT

  • f you will enter the value of x greater than the value of y then the condition inside if block (x>y) will be true hence, if block will execute.
  • Now, the second condition will be checked by the compiler to validate whether x is equal to y (x == y ).
  • And if x is equal to y then, nested if block will execute and it will print: x is equal to y. Because nested if block executed.
  • Otherwise, nested else block will execute and it will print: x is greater than y. Because nested if-else block executed.
  • If you will enter the value of x less than the value of y then the condition inside if block (x>y) will be false hence, else block will execute.
  • Now, the second condition will be checked by the compiler to validate whether x is equal to y (x == y ).
  • And if x is equal to y then, nested else if block will execute and it will print: x is equal to y. Because nested else if block executed.
  • Otherwise, nested else block will execute and it will print: x is less than y. Because nested else-else block executed.

Top Book Suggestion

The two books on C Programming Language that I Personally Recommend. I really loved reading these books. These books are listed for quality content, easy steps, and affordable price. You can get it from Amazon and Flipkart. 

 

Check The Price On Amazon:

1. C Programming Beginner’s Guide

2. Head First C: A Brain-Friendly Guide

Check The Price On Flipkart:

1. Introduction to C Programming 

2. Head First C: A Brain-Friendly Guide

« Previous Page                                                                                                                                Next Page »

Buy Premium Courses At Lowest Price. Become A Certified Developer Today!

Best Books Selected by Our Experts

Buy Premium Projects At Lowest Price

Ebooks Written by Our Experts

CoursePot.Com

Buy Premium Courses At Lowest Price. Grab Golden Opportunity To Become A Certified Developer Today!

Useful Links

Pin It on Pinterest

Share This