Microsoft PowerPoint - Chapter 3_20-21Final.pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

Chapter 3: Conditional Statements 1 Chapter outcomes By the end of this chapter, the students will know how to create decision making statements using if.. else statements know how to write decision making statements using switch.. case statement be a...

Chapter 3: Conditional Statements 1 Chapter outcomes By the end of this chapter, the students will know how to create decision making statements using if.. else statements know how to write decision making statements using switch.. case statement be able to solve complex problems using the concepts learned 2 Decision Control statement In decision control statements, a group of statements are executed when the condition is true. If condition is false, then else part statements are executed. There are 4 types of decision making control statements in C language. They are: 1. The if Statement 2. The if..else statements 3. Nested..if statements 4. switch..case statements 3 The if Statement The C programming language provides a general decision- making capability in the form of a language construct known as the if statement. Syntax: Flowchart: If (condition) { Statement/s to be executed if condition is true; } If the result of the test condition is true, then the instructions inside the IF body are executed. If the result of the condition is false, then the statements are skipped. 4 The if Statement Algorithm example: Equivalent in C Algorithm comparison #include main() Var { a,b : integer int a, b; printf(“Enter two integers:\n“); scanf(“%d %d“,&a, &b); Begin // a=5, b=5 if (a == b) 1- read(a,b) { 2- If (a=b) printf (“ %d is equal to %d“ write (a, “is equal to”, b) ,a,b); } End } 5 Output: 5 is equal to 5 If….Else In this type of statements, group of statements are executed when condition is true. If condition is false, then else part statements are executed. Syntax: Flowchart: If (condition) { Statement/s to be executed if condition is true;} else { Statement/s to be executed if condition is false;} The IF statement is executed if the statement inside the test condition evaluates to true; otherwise the statements 6 inside the ELSE block is executed. If….Else Algorithm example: Equivalent in C: Algorithm comparison #include main() Var { a,b : integer int a, b; printf(“Enter two integers:\n“); Begin scanf(“%d %d“,&a, &b); // a=5, b=20 1- read(a,b) if (a == b) 2- if (a=b) { write (a, “is equal to”, b) printf (“ %d and %d are equal“ ,a,b); else } write ( a, “and”, b, “are not equal”) else End { printf(“%d and %d are not equal“,a,b); } } 7 Output: 5 and 20 are not equal Conditonal operator As conditional operator works on three operands, so it is also known as the ternary operator. The behavior of the conditional operator is similar to the 'if-else' statement as 'if-else' statement is also a decision-making statement. Note: the instruction is used only if we have one statement in if part and one in else part. If (condition) Statement 1; else Statement 2; condition ? Statement1 : Statement 2; If codition is true , Statement 1 is evaluated and its value is returned. If condition is false, then Statement 2 is evaluated and its value becomes the value of the expression. 8 This form has the advantage of brevity and is appropriate for simple conditionals. Examples with conditional operator Example 1: To assign the maximum of a and b to z: if (a>b) z = (a>b) ? a : b; z = a; which is the same as: else Example 2: z=b; 9 Nested if The if...else statement can be used in nested form when serious decisions are involved. This can be very useful if you want to test various types of scenarios and situations. If the test condition is true, it will execute the code before else block but, if it is false, the control of the program jumps to the else block and check test condition 1 and the process continues. If all the test expression are false then, the last statement is executed. 10 The ANSI standard specifies that 15 levels of nesting may be continued. Algorithm example Algorithm Comparison Var n1,n2 : integer Begin 1-write(“enter two integers to check”) 1- read(n1,n2) 2- if (n1=n2) write (n1, “=”, n2) else if (n1>n2) write (n1, “>”, n2) else write (n1, “

Use Quizgecko on...
Browser
Browser