Download presentation
Presentation is loading. Please wait.
1
Decisions Chapter 4
2
Introduction Computer Languages, too, must be able to perform different sets of actions depending on the circumstances C language has three major decision making structures: If Statement If-Else Statement Switch Statement
3
If Statement Used for Condition statement
The “if statement” is used to execute or ignore a set of statements after testing a condition The “ if statement” evaluates a condition If the given condition is true, the statement or set of statements following the “if statement” is executed If condition is false, the statement or set of statements following the “if statement” condition is ignored and the control transfers to the next statement
4
If Statement Syntax Single Statement if (condition) statement 1;
Multiple Statements { Statement 1; Statement 2; Statement 3; Statement m; } Statement n;
5
If Statement Program void main (void) { char ch; ch = getche( );
if ( ch = = ‘ y ‘ ) printf ( “ \n You typed y.” ); } Output: y You typed y.
6
If Statement Operations (Flow Chart)
Test Condition FALSE TRUE Body of if Statement Statement immediately after if statement
7
If-Else Statement The “ if-else statement” is used for making two-ways decisions. Either one of two blocks of statements is executed after evaluating a condition Syntax – 1 if (condition) statement 1; else statement 2; Syntax – 2 : if (condition) { Statement 1; Statement 2; First Block Statement m; } Statement 2; Second Block Statement n;
8
If-Else Statement Operations (Flow Chart)
Test Condition TRUE FALSE Block 1 Block 2 Statement immediately after if else statement
9
If-Else Statement Program
void main (void) { char ch; ch = getche( ); if ( ch = = ‘ y ‘ ) printf ( “ \n You typed y. “ ); else printf ( “ \n You did not typed y. “ ); } Output: a You did not typed y. y You typed y.
10
Nested If-Else Statement Operations (Flow Chart)
Test Condition TRUE Block FALSE Test Condition TRUE Block FALSE Statement immediately after if else statement
11
Nested Statements void main(void) { int temp ; clrscr ( ) ; printf ( “ Type today’s temperature. “ ) ; scanf ( “ %d “, &temp ) ; if ( temp < 80 ) if ( temp > 60 ) printf ( “ Nice Day.“ ) ;} } else printf ( “ Sure is Hot! “ ) ;
12
Switch Statement The “switch statement” is used as substitute of nested if-else” statements. It is used when multiple choices are given and one choice is to be selected. switch (expression) { case const1: statements; break; case const2: case const3: case const4: default: }
13
printf ( “ = %f “, num1 * num2 ) ; break ; case ‘ / ‘ :
void main (void) { float num1 = 1.0 , num2 = 1.0 ; char op ; clrscr ( ) ; while ( ! ( num1 = = 0.0 && num2 = = 0.0 ) ) printf ( “ Type number , operator , number : \n “ ) ; scanf ( “ %f %c %f “, &num1, &op, &num2 ) ; switch ( op ) case ‘ + ‘ : printf ( “ = %f “, num1 + num2 ) ; break ; case ‘ - ‘ : printf ( “ = %f “, num1 - num2 ) ; case ‘ * ‘ : printf ( “ = %f “, num1 * num2 ) ; break ; case ‘ / ‘ : printf ( “ = %f “, num1 / num2 ) ; default : printf ( “ Unknown Operator “ ) ; } printf ( “ \n \n “ ); getch ( );
14
Switch Statement Operations (Flow Chart)
Test Condition TRUE Block FALSE Test Condition TRUE Block FALSE Statement immediately after switch statement
15
Nested if else Statement Switch Statement
It becomes complicated for multiple selections It uses an independent expression for each case The test condition can be given in special range of values. If given condition match then the statements under it will be executed. It is easy to understand for multiple selections It uses a single expression for all cases. But each case must have constant value of integer type or character type Only a single expression is given in the switch statement which returns a single value. The test condition cannot be given in a specified range. It is the major drawback of the switch statement.
16
Logical Operators The logical operators are used to combine relational expressions or relational conditions. The expression containing logical operators is called logical expression or logical condition. It is also called Compound Condition or Compound Expression There are 3 types of Logical Operators or Boolean Operators && AND operator || OR operator ! NOT operator
17
Logical Operator Program
void main (void) { int x ; clrscr ( ) ; printf ( “ Press the number 5 or 6 : “ ) ; scanf ( “ %d “, &x ) ; if ( x = = 5 | | x = = 6 ) printf ( “You have typed the right number. “ ) ; else printf ( “ Sorry wrong number. “ ) ; } Output: 2 Sorry wrong number. 5 Yes You have typed the right number.
18
The Conditional Operator
The conditional operator is used as an alternative of a simple if-else statement. (condition)? Expression 1 : Expression 2 For example: result = (a>b)?a:b If(a>b) result = a; Else result = b;
19
Lab Work Write a program to input four integers. Find out the largest value and then print it on the screen by using if statement. Write a program to input single character and print a message “it is a vowel” if it is a vowel otherwise print message “it is a consonant’. Use if-else structure and OR (||) operator only. Write above program by using switch statement Hint: vowel characters (A, E,I, O, U)
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.