l3 conditional

37
Programming With C Programming With C Condition and Type Condition and Type Casting Casting

Upload: sm-akash

Post on 17-Sep-2015

232 views

Category:

Documents


1 download

DESCRIPTION

c

TRANSCRIPT

  • Programming With C

    Condition and Type Casting

  • Integer and float conversionsOperation between int and int always yields an int result. Operation between real and real always yields an real result. Operation between int and real always yields an real result.

  • Type conversion in assignmentsint i;float b;i=3.5;b=30;3 is stored in i and 30.000000 is stored in b.

  • Type CastingProblem : Sometimes it is needed to force the compiler to explicitly convert the value of an expression to a particular data type. For Example: void main(){float a;int x=6, y=4;a=x/y;printf(Value of a=%f, a);}

  • Output..Value of a=1.000000 The answer is coming 1.000000 instead of 1.500000Here x and y both are integers, so x/y yields an integer.When it is stored in float a, it converted to float result (1.000000)

  • Solution 1One solution could be converted x and y to floatSo, it will befloat x=6, y=4;.

  • Solution 2Another solution could be type castingfor example:void main(){float a;int x=6,y=4;a=(float)x/y;printf(Value of a=%f,a);}

  • OutputHere is the output of the following programValue of a=1.500000

  • What will be the output of the following programfor examplevoid main(){float a;int x=6,y=4;a=(float)(x/y);printf(Value of a=%f,a);}

  • OutputValue of a=1.000000

    Here, a= (float)x/y , the expression (float) convert the variable x from type int to type float before being used in division operation.

  • More about Type CastingAnother example of type castingvoid main(){float a=7.5;printf(value of a on type casting=%d,(int)a);printf(value of a=%f,a);}

  • OutputValue of a on type casting=7Value of a=7.5

  • Hierarchy of operations1st priority : *, / , %2nd priority : + , -3rd priority : =

  • Some notesin case of a tie between operators of same priority preference is given to the operator that occurs first.for example: z=a*b+c/d

    If there are more than one set of parenthesis. Must use pairs of parenthesis.

  • Logical operators&& : AND|| : OR! : NOT

  • Relational operators< : less than> : greater than= : greater than or equal to== : equal to!= : not equal to

  • Hierarchy of all operators1st priority : ! (logical)2nd priority : *, /, % (arithmetic)3rd priority : +, - (arithmetic)4th priority : , = (relational)5th priority : ==, != (relational)6th priority : && (logical)7th priority : || (logical)8th priority : = (assignment)

  • Decision Control StructuresDecision control instructions control or alter the flow of execution of the program. A decision control instruction can be implemented in C using:The if statementThe if-else statementThe switch-case statementThe conditional operators

  • The if StatementThe if statement is a one-way decision statement. It is written as if(condition)statement;If the condition is true, then the statement is executed.If the condition is not true then the statement is not executed; instead the program skips past it.More than one statement is also possible, then they must be enclosed within curly braces, { }.

  • The if StatementExample:

    int grade;if ( grade >= 60 ) printf( "Passed\n");

  • The if-else StatementThe if-else statement is a two-way decision statement. It is written asif(condition)statement1;elsestatement2;If the condition evaluates to true, then statement1 is executed.If the condition evaluates to false, then statement2 is executed.

  • The if-else StatementExample:

    int grade;if ( grade >= 60 ) printf( "Passed\n");else printf( "Failed\n"); true

    false

    Print Passed

  • The else-if StatementA multiple decision construct is also possible using else-if statement. It is written asif( condition1 ) statement1;else if( condition2 ) statement2;else if( condition3 )statement3;..else statementN; Once condition is met, rest of statements skipped.

  • The else-if StatementFlowchart of the else-if construct

    Falsecondition1condition2condition3 True True True False False..statement1statement2statement3statementNExit

  • The else-if StatementExample:int grade;if ( grade >= 90 ) printf( A );else if ( grade >= 80 ) printf( B );else if ( grade >= 70 ) printf( C );else if ( grade >= 60 ) printf( D );else printf( F );

  • The if Statement (03/06/14)Example:int grade;if ( grade >= 90 ) printf( A );if ( grade >= 80 ) printf( B );if ( grade >= 70 ) printf( C );if ( grade >= 60 ) printf( D );If ( grade
  • Ternary conditional operatorTernary conditional operator (?:)Takes three arguments (condition, value if true, value if false)Example:if ( grade >= 60 ) printf( "Passed\n");else printf( "Failed\n");This could be writtenprintf( "%s\n", grade >= 60 ? "Passed" : "Failed" ); Or it could have been writtengrade >= 60 ? printf( Passed\n ) : printf( Failed\n );

  • The switch-case statementThe switch-case statement is a multi-way decision statement.Unlike the multiple decision statement that can be created using if-else, the switch statement evaluates the conditional expression and tests it against numerous constant values. The branch corresponding to the value that the expression matches is taken during execution. The value of the expressions in a switch-case statement must be an ordinal type i.e. integer, char etc. Float and double are not allowed.

  • The switch-case statementSyntax:switch( expression ) { case 1:statement1;break; case 2:statement2;break;case 3:statement3;break; default : statement4;break; }

  • The switch-case statementEven if there are multiple statements to be executed in each case there is no need to enclose them within a pair of braces (unlike if, and else)The default clause is an optional clause that is matched if none of the constants in the case statements can be matched.

  • The switch-case statementFlowchart of the switch-case construct

    switch variableequals 1st caseconstantswitch variableequals 2nd caseconstantBody of 1st caseBody of 2nd caseBody of defaultExit True TrueFalseFalse

  • The switch-case statementExample:

    char grade;scanf(%c,&grade);switch( grade ) { case 'A' : printf( "Excellent" ); break; case 'B' : printf( "Good" );break;case 'C' : printf( "OK" );break; case 'D' : printf( "Mmmmm...." ); break;case 'F' : printf( "You must do better than this" ); break;default :printf( "What is your grade anyway?" ); break;}

  • The goto statementThe goto statement is used to alter the normal sequence of program execution by unconditionally transferring control to some other part of the program. It is written as goto label;Here label is an identifier used to label the target statement to which the control would be transferred. The target statement will appear as:label: statement;

  • The goto statementExample:void main(){int num=1, sum = 0;label1:if(num
  • ProblemsLeast Challenging1. An integer number is entered through the keyboard. Write a C program to find out whether it is an odd or even number.2.An integer number is entered through the keyboard. Write a C program that will determine whether it is a prime number or not.3.Three integer numbers are entered through the keyboard. Write a C program to find the largest of these three numbers.4.Write a C program using conditional operators to determine whether a year entered through the keyboard is a leap year or not.

  • ProblemsChallenging1. A five-digit number is entered through the keyboard. Write a program to obtain the reversed number and to determine whether the original and reversed numbers are equal or not.2.The PDB charges its domestic customers for consumption of electricity as follows:Consumption Unit Rate of Charge0-200Tk. 0.50 per unit201-400Tk. 100 plus Tk. 0.65 per unit excess of 200401-600Tk. 230 plus Tk. 0.80 per unit excess of 400601 and aboveTk. 390 plus Tk. 1.00 per unit excess of 600Write a program which will read the customer number and power consumed and print the amount to be paid by the customer.

  • Problems3. A company insures its drivers in the following cases:- If the driver is married.- If the driver is unmarried, male and above 30 years of age- If the driver is unmarried, female and above 25 years of ageIn all other cases the driver is not insured. If the marital status, sex and age of the driver are entered through the keyboard, write a program to determine whether the driver is to be insured or not.4.According to the Gregorian calendar, it was Monday on the date 01/01/1900. If any year is input through the keyboard, write a program to find out what is the day on 1st January of this year.

    *