C notes from scratch to advance | Conditional Instructions in C | #C
C is a programming language as we have discussed earlier about languages and Programming Languages. C is one of the oldest and finest programming languages.
In previous, we have discussed about C basics , what is C , What is Programming , What is computer language , uses and all in chapter 0. And in chapter 1 we have discussed variables, nomenclature , constants , types , keywords , basic structure of C program including Instructions and Operators and we have made our first program also.
Links of previous notes :
Chapter 0 (basics) : click here
Chapter 1 (Variable,Constant and Keywords) : click here
Chapter 2 (Instructions and operators) : click here
- If it’s raining I will take umbrella.
- I will celebrate my birthday if my birthday is on Sunday.
- I will watch a movie if it’s Sunday.
- I will play Cricket if I had completed my HomeWork.
- If I will start the bike then I will meet you.
In Above all statements the work is all dependent on the conditions ( like : if this will happen then this will happen )
Explaination : the two conditions of the above
- If it’s raining I will take umbrella.
- If it’s not raining I will not take umbrella.
- I will celebrate my birthday if my birthday is on Sunday.
- I will not celebrate my birthday if my birthday is not on Sunday.
- I will watch a movie if it’s Sunday.
- I will not watch a movie if it’s not Sunday.
- I will play Cricket if I had completed my HomeWork.
- I will not play Cricket if I had not completed my HomeWork.
- If I will start the bike then I will meet you.
- If I will not start the bike then I will not meet you.
All these are decisions that depend on conditions being met.
In ‘C’ language, too, we must be able to execute instructions on a condition(s) being met.
Decision-making instructions in C
- If-else statement
- Switch statement
If-else statement
The syntax of an if-else statement in c looks like this:
Example :
int a=23; if (a>18) { printf(“you can drive\n”); }
Note that else block is not necessary but optional.
Relational Operators in C
Relational operators are used to evaluate conditions (true or false) inside the if statements. Some examples of relational operators are:
== | equals to |
>= | greater than or equal to |
> | greater than |
< | less than |
<= | less than or equal to |
!= | not equal to |
Important Note:
‘=’ is used for an assignment, whereas ‘==’ is used for an equality check.
The condition can be any valid expression. In C, a non-zero value is considered to be true.
Logical Operators
&&, ||, and ! are the three logical operators in C. These are read as “and,””or,” and “not.” They are used to provide logic to our c programs.
Use of logical operators:
Condition ? expression-if-true ; expression-if-false
Here, ‘?’ and ‘:’ are Ternary operators.
Switch case-control instruction
Switch-case is used when we have to make a choice between the number of alternatives for a given variable.
Syntax,
Thanks for reading this blog. Hope you get satisfied with the blog and definitely this blog must have valued your time and effort of reading.
Take a time to connect our other digital creations such as Instagram , Facebook and Youtube.
🧑💻🧑💻 Social Media Links of Tech DCode :
👉🏻 YouTube : https://www.youtube.com/channel/UCjJnEdeugftBwQ3yMuD4B_A
👉🏻 Instagram : https://www.instagram.com/thetechdcode/
👉🏻 Facebook Page : https://www.facebook.com/thetechdcode
👉🏻 Twitter : https://twitter.com/thetechdcode
👉🏻 Telegram Channel : https://t.me/thetechdcode
👉🏻 Tech DCode Linktree : https://linktr.ee/thetechdcode
👉🏻 My Personal Handles : https://linktr.ee/virtualshivamin
🧑💻🧑💻 Social Media Links of SHIVAM SINGH (OWNER) :
👉🏻 Instagram : https://www.instagram.com/virtualshivamin/
👉🏻 Facebook Page : https://www.facebook.com/virtualshivamin/
👉🏻 Twitter : https://twitter.com/virtualshivamin/
👉🏻 Personal Linktree : https://linktr.ee/virtualshivamin