0% found this document useful (0 votes)
16 views

Decision Control Structure

This document discusses different decision control structures in C programming including if statements, if-else statements, logical operators, and conditional operators. It provides examples of different forms if statements can take including multiple conditions and nesting. It also explains the general forms of logical operators like AND, OR, and NOT and conditional operators ? and :.

Uploaded by

CHANDRA BHUSHAN
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Decision Control Structure

This document discusses different decision control structures in C programming including if statements, if-else statements, logical operators, and conditional operators. It provides examples of different forms if statements can take including multiple conditions and nesting. It also explains the general forms of logical operators like AND, OR, and NOT and conditional operators ? and :.

Uploaded by

CHANDRA BHUSHAN
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 12

Decision Control Structure

Presenter Vinay Gupta

Decisions ! Decisions !

Decision control instruction can be implemented in C using


The if statement The if-else statement The conditional operators.

General form of if statement looks like this:

if(this condition is true) execute this statement. The keyword if tells the compiler that what
follows, is a decision control instruction. The condition is specified using Relational Operators

Expression x==y
x != y x<y x>y x <= y x >= y

Is true if x is equal to y
x is not equal to y x is less than y x is greater than y x is less than or equal to y x is greater than or equal to y

Forms of if
1) if(condition) do this; 2) if(condition) { do this; and this; } 3) if(condition) do this; else do this; 4) if(condition) { do this; and this; } else { do this; and this; }

Forms of if..(contd)
5) if(condition) do this; else { if(condition) do this; else { do this; and this; } } 6) if(condition) { if(condition) do this; else { do this; and this; } } else do this;

Use of Logical Operators

C allows usage of three logical operators:


&& (read as AND) || (read as OR) ! (read as NOT)

The first two are composed of double symbols. Dont use single symbol | and &. These are bitwise operators. The first two operators, && and ||, allow two or more conditions to be combined in an if statement.

Predict the output.


main( ) { int i; printf(Enter value of i: ); scanf(%d, &i); if(i = 5) printf(You entered 5); else printf(You entered something other than 5); }

The Conditional Operator

The conditional operators ? and : are sometimes called ternary operators since they take three arguments. General Form:
expression 1 ? expression 2 : expression 3

Example
int x,y; scanf(%d,&x); y=(x > 5 ? 3 : 4)

The equivalent if statement will be if( x > 5) y = 3; else y = 4;

This statement will store 3 in y if x is greater than 5, otherwise it will store 4 in y.

Note following points


a) It is not necessary to use conditional operators only in arithmetic statements. For Example

(i == 1 ? printf(Hi) : printf(hello));

The conditional operators can be nested as shown below int big , a , b , c;

big = ( a > b ? (a > c ? 3 : 4 ) : ( b > c ? 6 : 8 ) );

The limitation of the conditional operator is that after the ? Or after the : only one C Statement can occur.

You might also like