Presentation is loading. Please wait.

Presentation is loading. Please wait.

Module 6 – Decision Control Statements Objectives  Understands Increment/Decrement operators, Conditional and special operators in C  Understands significance.

Similar presentations


Presentation on theme: "Module 6 – Decision Control Statements Objectives  Understands Increment/Decrement operators, Conditional and special operators in C  Understands significance."— Presentation transcript:

1 Module 6 – Decision Control Statements Objectives  Understands Increment/Decrement operators, Conditional and special operators in C  Understands significance of decision control statements  Knowing more on types of decision control statements  Working on decision control statements

2 Sample program on Increment and Decrement Operators // C Program to demonstrate the working of Increment/Decrement operators #include int main() { int a = 10, c; c = a++; //post increment printf("c= %d\n",c); c = ++a; //pre increment printf("c= %d\n",c); c = a--; // post decrement printf("c= %d\n",c); c = --a; //pre decrement printf("c= %d\n",c); getch(); return 0; } Output c = 10 c = 12 c = 10

3 Sample program on Conditional Operators // C Program to demonstrate the working of Conditional operators #include int main() { int a = 10, b; b = (a == 1) ? 20: 30; printf( "Value of b is %d\n", b ); b = (a == 10) ? 20: 30; printf( "Value of b is %d\n", b ); getch(); return 0; } Output Value of b is 30 Value of b is 20

4 Sample program on Special Operators // C Program to demonstrate the working of Special operators #include int main() { int a = 4; short b; double c; int* ptr; /* example of sizeof operator */ printf("Line 1 - Size of variable a = %d\n", sizeof(a) ); printf("Line 2 - Size of variable b = %d\n", sizeof(b) ); printf("Line 3 - Size of variable c= %d\n", sizeof(c) );

5 Sample program on Special Operators /* example of & and * operators */ ptr = &a; /* 'ptr' now contains the address of 'a'*/ printf("value of a is %d\n", a); printf("*ptr is %d.\n", *ptr); getch(); return 0; } Output Line 1 - Size of variable a = 4 Line 2 - Size of variable b = 2 Line 3 - Size of variable c= 8 value of a is 4 *ptr is 4

6 Program to swap numbers using temporary variable //Program to swap two numbers using temporary variable #include int main() { int firstNumber, secondNumber, temporaryVariable; printf("Enter first number: "); scanf("%d", &firstNumber); printf("Enter second number: "); scanf("%d",&secondNumber); // Value of firstNumber is assigned to temporaryVariable temporaryVariable = firstNumber;

7 Program to swap numbers using temporary variable // Value of secondNumber is assigned to firstNumber firstNumber = secondNumber; // Value of temporaryVariable (which contains the initial value of first Number) is assigned to second number secondNumber = temporaryVariable; printf("\nAfter swapping, firstNumber = %d\n", firstNumber); printf("After swapping, secondNumber = %d", secondNumber); getch(); return 0; } Output Enter first number: 10 Enter second number: 25 After swapping, firstNumber = 25 After swapping, secondNumber = 10

8 Course Contents  History of Programming Languages  Introduction to C  Basic C Program  Printf and Scanf  Data Types  Tokens and Keywords  Constants  Variables  Operators and Expressions  Decision Control Statements  Loop Control Statements  Case Control Statements  Type Qualifiers  Storage Class Specifier  Array  String  Pointers  Functions  Library Functions  Command Line Arguments  Variable Length Arguments

9 Decision Control Statements  Decision making is used to specify the order in which statements are executed.  There are three types in Decision Control Statements  if statements  if else statements  nested if statements

10 if Statements  The if statement evaluates the test expression/condition inside parenthesis.  If test expression/condition is evaluated to true (nonzero), statements inside the body of if is executed.  If test expression/condition is evaluated to false (0), statements inside the body of if is skipped.  Syntax: if (test Expression/condition) { // statements }

11 if Statements flow chart Now, will see a sample program on if statement

12 Sample program on if #include int main() { int number; printf("Enter an integer: "); scanf("%d", &number); // Test expression is true if number is less than 0 if (number < 0) { printf("You entered %d.\n", number); } printf("The if statement is easy."); getch(); return 0; } Output Enter an integer: 5 The if statement is easy.

13 if…. else statements  The if...else statement executes some code if the test expression is true (nonzero) and some other code if the test expression is false (0).  Syntax: if (testExpression) { // codes inside the body of if } else { // codes inside the body of else }

14 if…. else statements flow chart Now, will see a sample program on if..else statement

15 Sample program on if…else #include int main() { int number; printf("Enter an integer: "); scanf("%d",&number); // True if remainder is 0 if( number%2 == 0 ) printf("%d is an even integer.",number); else printf("%d is an odd integer.",number); return 0; } Output Enter an integer: 7 7 is an odd integer.

16 Nested if...else statement (if...elseif....else Statement)  The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities.  The nested if...else statement allows you to check for multiple test expressions and execute different codes for more than two conditions.

17 Nested if...else statement (if...elseif....else Statement) if (testExpression1) { // statements to be executed if testExpression1 is true } else if(testExpression2) { // statements to be executed if testExpression1 is false and testExpression2 is true }.. else { // statements to be executed if all test expressions are false } Syntax: Will see an example program on Nested if…else statement now…

18 Sample program on nested if #include int main() { int number1, number2; printf("Enter two integers: "); scanf("%d %d", &number1, &number2); //checks if two integers are equal. if(number1 == number2) { printf("Result: %d = %d",number1,number2); }

19 Sample program on nested if //checks if number1 is greater than number2. else if (number1 > number2) { printf("Result: %d > %d", number1, number2); } // if both test expression is false else { printf("Result: %d < %d",number1, number2); } getch(); return 0; } Output Enter two integers: 12 23 Result: 12 < 23

20 Course Contents  History of Programming Languages  Introduction to C  Basic C Program  Printf and Scanf  Data Types  Tokens and Keywords  Constants  Variables  Operators and Expressions  Decision Control Statements  Loop Control Statements  Case Control Statements  Type Qualifiers  Storage Class Specifier  Array  String  Pointers  Functions  Library Functions  Command Line Arguments  Variable Length Arguments

21 Loop Control Statements  Loops are used in programming to repeat a specific block until some end condition is met.  There are three types  For loop  While loop  Do-While loop

22 For Loop Control Statements  Syntax: for (initializationStatement; testExpression; updateStatement) { // codes }

23 How for loop works  The initialization statement is executed only once.  Then, the test expression is evaluated. If the test expression is false (0), for loop is terminated. But if the test expression is true (nonzero), codes inside the body of for loop is executed and update expression is updated. This process repeats until the test expression is false.  The for loop is commonly used when the number of iterations is known.

24 For loop flow chart Now, will see a sample program on ‘For Loop’

25 Sample program on For Loop #include int main() { int n, count, sum = 0; printf("Enter a positive integer: "); scanf("%d", &n); // for loop terminates when n is less than count for(count = 1; count <= n; ++count) { sum += count; } printf("Sum = %d", sum); getch(); return 0; } Output Enter a positive integer: 10 Sum = 55

26 Thank You


Download ppt "Module 6 – Decision Control Statements Objectives  Understands Increment/Decrement operators, Conditional and special operators in C  Understands significance."

Similar presentations


Ads by Google