IDOL Institute of Distance and Online Learning ENHANCE YOUR QUALIFICATION, ADVANCE YOUR CAREER.
BCA 2 All right are reserved with CU-IDOL Computer Programming Course Code: BCA 112 Semester: First SLM Units: 3, 4 e-Lesson No.: 2 www.cuidol.in Units-3, 4(BCA112)
Operators, Expressions, and Decision & 33 Loop Control Structure OBJECTIVES INTRODUCTION • Describe different types of C operators, and In this session we are going to learn about • Solve expressions having different operators • Different types of C operators • Change the type of given data and variable • Solving expressions having different operators • Explain operator precedence & associativity • Changing the type of given data and variable • Define the concept of decision making • Operator precedence & associativity • Apply various decision making statements • The concept of decision making • Differentiate between if-else and switch statement • Various decision making statements • Explain the necessity of loop and describe various • Various looping statements • Use of break and continue statements looping statements • goto statement • Analyze the nesting of loops and demonstrate the use of break & continue statements • Elaborate goto statement www.cuidol.in Units-3, 4(BCA112) INASlTl ITriUgThEt OarFeDrIeSsTeArNvCeEd AwNitDh OCNUL-IIDNOE LLEARNING
TOPICS TO BE COVERED 43 • Operators INTRODUCTION • Type Conversion • Operator Precedence and Associativity • Decision Making Statements • Looping • Jump Statements www.cuidol.in Units-3, 4(BCA112) INASllTIrTigUhTtEaOrFe DreISsTeArvNeCdE wANithDCOUN-LIDINOELLEARNING
Operators 5 • An operator is a symbol that tells the computer to perform certain mathematical or logical manipulations. • Operators are used in programs to manipulate data and variables. • They usually form a part of the mathematical or logical expressions. • C operator can be classified into a number of categories. They include: 1. Arithmetic operators 2. Unary operator 3. Relational operators 4. Logical operators 5. Conditional operator 6. Bitwise operators 7. Increment and Decrement operators 8. Assignment Operator 9. Sizeof Operator www.cuidol.in Units-3, 4(BCA112) All right are reserved with CU-IDOL
Operators 6 Arithmetic Operators •The basic operators for performing arithmetic are: + addition 3+4 = 7 - subtraction 5-3 = 2 * multiplication 4*6 = 24 / division 10/2 = 5 % modulus 5%4 = 1 Unary Operators +21 All right are reserved with CU-IDOL •The unary operators are: -33 + Unary Plus Units-3, 4(BCA112) - Unary Minus www.cuidol.in
Operators Relational Operators 7 All right are reserved with CU-IDOL •The complete set of relational operators in C is: < less than 7<5 = 0 <= less than or equal 7<=5 = 0 > greater than 7>5 = 1 >= greater than or equal 7>=5 = 1 == equal 7==7 = 1 != not equal 7!=7 = 0 Logical Operators •The logical operators are: && AND (4>3) && (9<7) = 0 || OR (4>3) || (9<7) = 1 ! NOT !1 = 0 www.cuidol.in Units-3, 4(BCA112)
Operators Conditional Operator 8 •The Conditional operator (ternary operator) has syntax - expr1 ? expr2 : expr3 where expr1, expr2 and expr3 are expressions. (x > y) ? x : y (7>5) ? 7 : 5 Bitwise Operators •The Bitwise operators are: Operators Example Result www.cuidol.in Units-3, 4(BCA112) All right are reserved with CU-IDOL
Operators 9 Bitwise Operators www.cuidol.in Units-3, 4(BCA112) All right are reserved with CU-IDOL
Operators 10 Bitwise Operators www.cuidol.in Units-3, 4(BCA112) All right are reserved with CU-IDOL
Operators 11 Increment and Decrement Operators •The increment and decrement operators are: ++i add 1 to i - -j subtract 1 from j www.cuidol.in Units-3, 4(BCA112) All right are reserved with CU-IDOL
Operators 12 Increment and Decrement Operators www.cuidol.in Units-3, 4(BCA112) All right are reserved with CU-IDOL
Operators 13 Assignment Operator • Denoted by “=“. Used to assign some value to an identifier. www.cuidol.in Units-3, 4(BCA112) All right are reserved with CU-IDOL
Operators 14 Sizeof Operator • The sizeof operator is used with an operand, it returns the number of bytes the operand occupies. The operand may be a variable, a constant or a data type qualifier. Examples: m=sizeof(sum); n=sizeof(int); k=sizeof(256L); www.cuidol.in Units-3, 4(BCA112) All right are reserved with CU-IDOL
Type Conversion 15 •Converting one data type into another is known as type casting or, type-conversion. There are two types of conversion – • Implicit Type Conversion - This type of conversion is usually performed by the compiler when necessary without any commands by the user. Thus it is also called \"Automatic Type Conversion\". •The compiler usually performs this type of conversion when a particular expression contains more than one data type. www.cuidol.in Units-3, 4(BCA112) All right are reserved with CU-IDOL
Type Conversion 16 • Explicit type casting •There are some scenarios in which we may have to force type conversion. Here the user explicitly defines within the program the datatype of the operands in the expression. •The syntax is: (DataType) expression Example – gender_ratio = (float) No_of_males/ No_of_females = 42/12 = 3.5 www.cuidol.in Units-3, 4(BCA112) All right are reserved with CU-IDOL
Operator Precedence and Associativity 17 Operator precedence: dictates the order of evaluation of operators in an expression. Associativity: defines the order in which operators of the same precedence are evaluated in an expression. Associativity can be either from left to right or right to left. • www.cuidol.in Units-3, 4(BCA112) All right are reserved with CU-IDOL
Operator Precedence and Associativity 18 Example 1: Simplify the following expression: y = 34 + 12 / 4 - 45 www.cuidol.in Units-3, 4(BCA112) All right are reserved with CU-IDOL
Operator Precedence and 19 Associativity Example 2: Simplify the following expression: y = 15 + 6 – 6 / 2 > 7 + 2 y = 15 + 6 – 3 > 7 + 2 y = 21 – 3 > 7 + 2 y = 18 > 7 + 2 y = 18 > 9 y= 1 www.cuidol.in Units-3, 4(BCA112) All right are reserved with CU-IDOL
Decision Making Statements 20 • Decision making statements are used for making decision. These statements include – if statement if else statement if else ladder Nested if else Switch statement Conditional Operator if Statement – • Used for making decision based on some condition. www.cuidol.in Units-3, 4(BCA112) All right are reserved with CU-IDOL
Decision Making Statements 21 if else Statement – #include <stdio.h> • Used for making decision based on some condition. #include <conio.h> void main() { int num; printf(\"Enter an integer: \"); scanf(\"%d\", &num); if(num % 2 == 0) printf(\"%d is even.\", num); else printf(\"%d is odd.\", num); getch(); } www.cuidol.in Units-3, 4(BCA112) All right are reserved with CU-IDOL
Decision Making Statements #include<stdio.h> 22 #include<conio.h> if else if Ladder – void main() { • Used for multiple decision making based on some conditions. • Syntax - int a; printf(\"Enter a Number: \"); scanf(\"%d\",&a); if(a > 0) { printf(\"Given Number is Positive\"); } else if(a == 0) { printf(\"Given Number is Zero\"); } else if(a < 0) { printf(\"Given Number is Negative\"); } else { printf(\"Wrong Input\"); } getch(); } www.cuidol.in Units-3, 4(BCA112) All right are reserved with CU-IDOL
Decision Making Statements 23 Nested if else – #include<stdio.h> • It means one if else within another if else. void main() { •Syntax – if(condition) { int a, b; printf(\"Input two numbers:\"); if(condition2) { scanf(\"%d%d\", &a, &b); //Statements inside the body of nested \"if\" if (a != b) } { printf(\"\\n a is not equal to b\"); else { //Statements inside the body of nested \"else\" if ( a > b) } {printf(\"\\n a is greater than b\"); } } else { else //Statements inside the body of \"else\" {printf(\"\\n b is greater than a\"); } } } www.cuidol.in Units-3, 4(BCA112) else { printf(“\\n a is equal to b\"); } } All right are reserved with CU-IDOL
Decision Making Statements Switch Statement – Units-3, 4(BCA112) 24 • Mainly used for menu driven program. All right are reserved with CU-IDOL •Syntax – switch( expression ) { case value-1: Block-1; Break; case value-2: Block-2; Break; case value-n: Block-n; Break; default: Block-1; Break; } Statement-x; www.cuidol.in
Decision Making Statements Switch Statement Example– 25 void main() { int num; printf(\"\\n Enter any number between 1 and 3\"); scanf(\"%d\", &num); switch (num) { case 1: printf(\"Value is 1\"); break; case 3: printf(\"Value is 3\"); break; case 2: printf(\"Value is 2\"); break; default: printf(\"Wrong Input\"); break; } } www.cuidol.in Units-3, 4(BCA112) All right are reserved with CU-IDOL
Decision Making Statements 26 Conditional Operator – Equivalent program using if else Syntax – condition ? value_if_true : value_if_false Void main() { void main() { int a = 10, b = 20, c; int a = 10, b = 20, c; c = (a < b) ? a : b; if (a < b) printf(\"%d\", c); { } c = a; } else { c = b; } printf(\"%d\", c); } www.cuidol.in Units-3, 4(BCA112) All right are reserved with CU-IDOL
Looping 27 • Looping facilitates a program to execute the sequence of statements many times until the stated condition becomes false. • Loops are used to perform repetitive tasks by writing the codes only once. • C supports the following types of loops: while loop do while loop for loop www.cuidol.in Units-3, 4(BCA112) All right are reserved with CU-IDOL
Looping 28 while loop - www.cuidol.in Units-3, 4(BCA112) All right are reserved with CU-IDOL
Looping 29 do while loop - The syntax is: www.cuidol.in Units-3, 4(BCA112) All right are reserved with CU-IDOL
Looping 30 for loop - www.cuidol.in Units-3, 4(BCA112) All right are reserved with CU-IDOL
Looping 31 Example – #include<stdio.h> #include<stdio.h> #include<stdio.h> #include<conio.h> #include<conio.h> #include<conio.h> void main() void main() void main() { { { int num=1; int num=1; int number; while(num<=10) do { { for(number=1;number<=10;number++) { printf(\"%d\\n\",num); printf(\"%d\\n\",2*num); printf(\"%d\\n\",number); num++; num++; } } }while(num<=10); } } } www.cuidol.in Units-3, 4(BCA112) All right are reserved with CU-IDOL
Jump Statements 32 • Jump statements are used to skip some statements in the program. • Jump can be performed using statements – break continue goto • break statement is used to come out of the loop instantly. When a break statement is encountered inside a loop, the control directly comes out of loop and the loop gets terminated. • It is used with if statement. • Syntax – break; www.cuidol.in Units-3, 4(BCA112) All right are reserved with CU-IDOL
Jump Statements 33 Break Example – Output – val: 10 #include <stdio.h> val: 9 void main() Out of for-loop { int val; for (val =10; val>=1; val --) { printf(\"val: %d\\n\", val); if (val==9) { break; } } printf(\"Out of for-loop\"); } www.cuidol.in Units-3, 4(BCA112) All right are reserved with CU-IDOL
Jump Statements 34 • continue statement is used inside the loop. • When a continue statement is encountered inside a loop, control jumps to the beginning of the loop for next iteration, skipping the execution of statements inside the body of loop for the current iteration. • It is used with if statement. • Syntax – continue; www.cuidol.in Units-3, 4(BCA112) All right are reserved with CU-IDOL
Jump Statements 35 Continue Example – Output – 1 #include <stdio.h> 3 void main() 5 { 7 9 for (j=0; j<=9; j++) { if (j%2 == 0) { continue; } printf(\"%d\\n\", j); } } www.cuidol.in Units-3, 4(BCA112) All right are reserved with CU-IDOL
Jump Statements 36 • goto statement provides a jump from the ‘goto statement’ to a labeled statement in the same function. • The goto statement is rarely used because it makes program confusing, less readable and complex. •When goto is used, the control of the program won’t be easy to trace, hence it makes testing and debugging difficult. •Syntax – www.cuidol.in Units-3, 4(BCA112) All right are reserved with CU-IDOL
Jump Statements 37 goto Example – Output – 15 #include <stdio.h> void main() { int i, sum=0; for( i = 0; i<=10; i++){ sum = sum+i; if(i==5){ goto addition; } } addition: printf(\"%d\", sum); } www.cuidol.in Units-3, 4(BCA112) All right are reserved with CU-IDOL
Multiple Choice Questions 38 1. What is the output of this C code? int main() { int i = -9; int k = i %4; printf(\"%d\", k); } a) Compile time error b) -1 c) 1 d) None of the above 2. Which of the following is NOT a compound assignment operator? a) /= b) += c) %= d) == Answers: 1.b) 2.d) Units-3, 4(BCA112) All right are reserved with CU-IDOL www.cuidol.in
Multiple Choice Questions 39 3. What is the value of X in the sample code given below? int X; X = ( 2 + 3) * 2 + 3; a) 10 b) 13 c) 25 d) 28 4. Label in Goto statement is same like a) Case in switch statement b) Initialization in for loop c) Continuation condition in for loop d) All of them Answers: 3.b) 4.a) www.cuidol.in Units-3, 4(BCA112) All right are reserved with CU-IDOL
Multiple Choice Questions 40 5. What will be the output of the following C code? void main() { int x = 5; if (x < 1) printf(\"Hello\"); } a) Run time error b) Hello c) Varies d) None of the above 6. The continue statment cannot be used with a) for b) while c) do while d) switch Answers: 5.d) 6.d) Units-3, 4(BCA112) All right are reserved with CU-IDOL www.cuidol.in
SUMMARY 41 Let us recapitulate the important concepts discussed in this session: • An operator is a symbol that tells the computer to perform certain mathematical or logical manipulations. •C operator can be classified into a number of categories. They include: Arithmetic operators, Unary operator, Relational operators, Logical operators, Conditional operator, Bitwise operators, Increment and Decrement operators, Assignment Operator, Sizeof Operator. •Converting one data type into another is known as type casting or, type-conversion. There are two types of conversion – Implicit Type Conversion, and Explicit type casting. •Operator precedence dictates the order of evaluation of operators in an expression. •Associativity defines the order in which operators of the same precedence are evaluated in an expression. www.cuidol.in Units-3, 4(BCA112) All right are reserved with CU-IDOL
SUMMARY 42 • Decision making statements are used for making decision. These statements include – if statement, switch statement, Conditional Operator • Looping facilitates a program to execute the sequence of statements many times until the stated condition becomes false. • C supports the following types of loops: while loop, do while loop, for loop. • Jump statements are used to skip some statements in the program. Jump can be performed using statements – break, continue, goto. www.cuidol.in Units-3, 4(BCA112) All right are reserved with CU-IDOL
FREQUENTLY ASKED QUESTIONS 43 Q1. What is meant by operator? List and explain different types of C operators. Ans: An operator is a symbol that tells the computer to perform certain mathematical or logical manipulations. For Further details please refer to the subject SLM. Q2. Define type casting. Explain categories of type casting. Ans: Converting one data type into another is known as type casting or, type-conversion. There are two types of conversion – Implicit Type Conversion, and Explicit type casting. . For Further details please refer to the subject SLM. Q3. Define operator precedence and associativity. Discuss their importance in solving an expression. Ans: Operator precedence dictates the order of evaluation of operators in an expression. Associativity defines the order in which operators of the same precedence are evaluated in an expression. For Further details please refer to the subject SLM. www.cuidol.in Units-3, 4(BCA112) All right are reserved with CU-IDOL
FREQUENTLY ASKED QUESTIONS 44 Q4. Differentiate between break and continue. Ans: break statement is used to come out of the loop instantly. While when a continue statement is encountered inside a loop, control jumps to the beginning of the loop for next iteration; skipping the execution of statements inside the body of loop for the current iteration. For Further details please refer to the subject SLM. Q5. Why use of goto statement is not recommended? Ans: The goto statement is rarely used because it makes program confusing, less readable and complex. For Further details please refer to the subject SLM. www.cuidol.in Units-3, 4(BCA112) All right are reserved with CU-IDOL
REFERENCES 45 •CU-IDOL’s “Computer Programming” SLM • Balaguruswamy (2017).Programming in ANSI C. New Delhi: McGraw-Hill. • Kanetkar, Y. (2014), Programming in C ANSI standard.New Delhi: BPB Publications. • Gottfried (2005).Programming with C. New York: Tata McGraw Hill. • Harrow K., Jones J. (1996).Problem Solving with C. London: Pearson Education. • Jeri R., Hanly,KoffmanE.P. (2000).Problem Solving and Program Design in C. 3rd Ed. Boston: Addison Wesley. www.cuidol.in Units-3, 4(BCA112) All right are reserved with CU-IDOL
46 THANK YOU www.cuidol.in Units-3, 4(BCA112) All right are reserved with CU-IDOL
Search
Read the Text Version
- 1 - 46
Pages: