1 / 18

Programming Paradigms

Programming Paradigms. Lecturer Hamza Azeem. Lecture 5. Decision Control Structure. Decision Control Structure. A conditional statement allows us to control whether a program segment is executed or not. Two constructs if statement if if-else if-else-if switch statement.

zinnia
Download Presentation

Programming Paradigms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming Paradigms LecturerHamza Azeem

  2. Lecture 5 Decision Control Structure

  3. Decision Control Structure • A conditionalstatement allows us to control whether a program segment is executed or not. • Two constructs • if statement • if • if-else • if-else-if • switch statement

  4. The Basic if Statement • Syntax if(condition) action • if the condition is true then execute the action. • action is either a single statement or a group of statements within braces. condition false true action

  5. The Basic if Statement • Put multiple action statements within braces if (it's raining){ <take umbrella> <wear raincoat> }

  6. Example Program // Program to read number and print its absolute value int main(){ int value=0; printf("Enter integer: “); scanf(“%d”,&value); if(value < 0) value = value * -1; printf("The absolute value is %d“, value); }

  7. Relational Operators • The keyword iftells the compiler that what follows, is a decision control instruction. • The condition is specified using Relational Operators.

  8. Relational Operators

  9. Boolean/Logical Operators

  10. Example // Program to verify a specific combination int main() { int a=0,b=0; printf("Enter integer #1: “); scanf(“%d”,&a); printf(“\nEnter integer #2: “); scanf(“%d”,&b); if( (a > 2) && ( b > 4) ) printf(“Correct Combination”); }

  11. Example // Program to sort two variables from low to high int value1; int value2; int temp; printf("Enter integer #1: “); scanf(“%d”,&value1); printf("Enter integer #2: “); scanf(“%d”,&value2); if (value1 > value2) temp = value1; value1 = value2; value2 = temp; } Printf (“The values are nor sorted in order %d %d”, value1, value2);

  12. Predict the output int i=0; printf(“Enter value of i: “); scanf(“%d”, &i); if(i = 5) printf(“You entered 5”);

  13. The if-else Statement condition • Syntax if(condition) Action_AelseAction_B • if the condition is true then execute Action_Aelse execute Action_B • Example: if(value == 0) printf( "value is 0“); else printf( "value is not 0“); false true Action_A Action_B

  14. Example int value1; int value2; int larger; printf("Enter integer #1: “); scanf(“%d”,&value1); printf("Enter integer #2: “); scanf(“%d”,&value2); if(value1 > value2) larger = value1; else larger = value2; printf( "Larger of inputs is: %d“, larger);

  15. Example int value; printf("Enter integer: “); scanf(“%d”,&value); if(value%2 == 0) printf(“Even Number”); else printf(“Not Number”);

  16. Different uses of IF 1) if(condition) do this; 2) if(condition) { do this; and this; }

  17. Different uses of IF 3) if(condition) do this; else do this; 4) if(condition) { do this; and this; } else { do this; and this; }

  18. Different uses of IF 5) if(condition) do this; else { if(condition) do this; else { do this; and this; } }

More Related