Forget your life decision, make your program to take the right decision: Conditional Construct in C: Part-1

Shashwat Raj
3 min readFeb 19, 2019

--

Next:

Life is full of decisions! We make a lot of decisions on a daily basis, almost before every task we decide.

What to wear?

What to eat?

What to read?

And many more….

Now, all these decisions are dependent on some conditions applied to it.

Like,

What do I want to wear? where am I going? — to school, party, office or for what purpose-like party, meeting, playing etc.

Same with the what to eat. What do I want to eat? what time of day is it? where am I eating? am I on a diet or not? And many more.

In the same way what to read? Depends on for what purpose I am reading? which book I am reading? etc.

So, you can clearly see our life is full of conditions. Knowingly or unknowingly we make decisions. Some are simple one and some really complex. When to wake? When to sleep? What to eat? What to wear? To decisions like where should I go for higher studies? which girl/boy should I marry? which stream should I choose? etc.

Reading this article is also one of your decision.

Programming languages are developed to solve our real-life problems and real-time problems. We make a lot of decisions and for solving our problem, these programming languages have decision making capability. According to a given condition, they also make decisions. C also has decision making statements or Conditional Constructs.

I am going to write a series of introductory articles on various conditional constructs available in C. This article is first and an introduction of all available conditional constructs.

There are different types of decision-making statements in C:

1. if:

‘if’ is a keyword in C-language it is used for decision-making statements. It is the most basic type of conditional statement. Here we have one condition to check, if it is correct we execute it.

Flowchart of if (only if) statement

2.if else:

‘else’ is another keyword in C language. It is a bit advanced version of if (or only if) statements. Here we have an option if the condition is not satisfied. When the condition is false it goes to the else part and executes it.

Flowchart of if else statement

3.Ternary operator:

It is one of the different operators available in C-language. It is also called conditional operator. We will read about it after we are done with if and if else statements.

4.switch:

‘Switch’ and ‘case’ are two keywords in C-language. In switch case we can only compare the equality. It is basically used in menu driven Programs. It uses different cases for comparison and a statement is attached to each case. It has a bit different functionality than that of the if or if else statement.

Flowchart of Switch case statement

We will discuss all these conditional constructs in the next article.

Next:

--

--

Shashwat Raj

Honours Computer Science Student & Enthusiast working with Python | Data Science | Machine Learning | Artificial Intelligence | Web Development