C Programming
C Programming

C notes from scratch to advance | Conditional Instructions in C | #C

C notes from scratch to advance | Conditional Instructions in C | #C

C Programming
C Programming

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

C programming
C programming
  • 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:

if ( condition to be checked)

{

              Statements-if-condition-true ;

}

else

{

              statements-if-condition-false ;

}

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: 

1. && (AND) is true when both the conditions are true

“1 and 0” is evaluated as false

“0 and 0” is evaluated as false

“1 and 1” is evaluated as true

2. || (OR) is true when at least one of the conditions is true. (1 or 0 = 1)(1 or 1 = 1)

3. ! returns true if given false and false if given true.

!(3==3) evaluates to false

!(3>30) evaluates to true

As the number of conditions increases, the level of indentation increases. This reduces readability. Logical operators come to the rescue in such cases.

Else if clause / Ladder if-else  

Instead of using multiple if statements, we can also use else if along with if, thus forming an if-else if-else ladder.

if (condition)

{


// statements ;


}


else if (condition)

{ 

//statements;

}


else 

{ 

//statements;

}

 

Using if-else if-else reduces indents. The last “else” is optional. Also, there can be any number of “else if.”

Last else is executed only if all conditions fail.

Operator Precedence

Priority Operator
1st  !
2nd *,/,%
3rd +,-
4th <>,<=,>=
5th ==,!=
6th &&
7th ||
8th =

Conditional operators

A shorthand “if-else” can be written using conditional or ternary 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,

Switch(integer-expression)


{


           Case c1:


                   Code;


           Case c2: 

                   //c1,c2,c3 are constants


                   Code; 

                   //Code is any valid C code


            Case c3:


                   Code;


             default:


                    Code;


}

 

The value of integer-expression is matched against c1,c2,c3……if it matched any of these cases, that case along with all subsequent “case” and “default” statements are executed.

Quick Quiz: Write a program to find the grade of a student given his marks based on below:

90-100 A
80-90 B
70-80 C
60-70 D
<70 F

Important notes

  • We can use switch case statements even by writing in any order of our choice
  • Char values are allowed as they can be easily evaluated to an integer
  • A switch can occur within another, but in practice, this is rarely done

 


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 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *