slide06-Selection Structures

slide06-Selection Structures - ICS 103: Computer...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
ICS 103: Computer Programming in C Handout-6 Topic: Control Structures (Selection Structures) Objective: To Know about relational and logical operators of C language and their use. To know about order of execution of relational and logical operators with others operators. To Know syntax and use of if , if else , Nested if , if … else if … else , switch Selection Statements. Relational and Logical operators : The most commonly used relational operators are > for greater than >= for greater than or equal to < for less than <= for less than or equal to == for equal to != for not equal to The most commonly used logical operators are for binary AND | | for binary OR ! for unary NOT These operators are used in the expres sion  of  if statement. The expression having these operators is called Logical expression. Operator Precedence: Operator Precedence ! (not) , +, - , & (address of) ; Note  Above  are  Unary  Operators  which  have  a  single  operand.      Ist (i.e   highest ) * , /, % IInd Page 1 of 11
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
+ , - IIIrd < , <= , >=, > IVth == (equality operator), != (not equal) . Vth && (logical and operator ) . VIth || (logical OR operator ) . VIIth = (assignment operator) . VIIIth (i.e Lowest ) Examples: To check if the value of the integer variable x is more than 5 and less than 10 or equal to 15 , we write To check if the value of the float variable a is not equal to 1.5 or less than or equal to 2.5 but more than or equal to 1.5 , we write a != 1.5 | | a <= 2.5 && a >= 1.5 Output Example #include<stdio.h> int main(void) { int u=-2; printf("%d\n",!0*3+-2); printf("%d\n",1==5>=3); printf("%d\n",2-5>-4||!5&0); printf("%d\n",-13%5); printf("%d",3<11+u||-5*2!=5); return 0; } Selection Structures : Page 2 of 11
Background image of page 2
One-Way Selection Using the if-statement: Syntax : if (expression) Statement; If the expres sion  is true , then  the   statem ent  is executed ; otherwise  the   statem e nt  is ignored . For example,  let  marks be an integer variable. To check the value of marks to see if it is more  than   75 and print PASS if it is more than 75 we would write if (marks > 75) printf (“PASS\n”) ; If marks is less  than  or  equal  to  75 then the print statement is ignored and nothing is printed. If suppose that in addition to printing
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 11

slide06-Selection Structures - ICS 103: Computer...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online