Conditional Programming Element - Lesson 4 PDF
Document Details
City College of Calamba
Tags
Summary
This document is a lesson on conditional programming in C. It explains the different types of conditional statements, such as if, if-else, and switch statements, with examples, and describes how they function in C.
Full Transcript
CONDITIONAL PROGRAMMING ELEMENT LESSON 4 OBJECIVES Understand the use of conditional statement in C programming. Distinguish the use of different conditional statements in C. CONDITIONAL PROGRAMMING IN C In a C program are executed sequentially. This happens when there is no condition aroun...
CONDITIONAL PROGRAMMING ELEMENT LESSON 4 OBJECIVES Understand the use of conditional statement in C programming. Distinguish the use of different conditional statements in C. CONDITIONAL PROGRAMMING IN C In a C program are executed sequentially. This happens when there is no condition around the statements. If you put some condition for a block of statements the flow of execution might change based on the result evaluated by the condition. This process is referred to as decision making in 'C.' The decision- making statements are also called as control statements. IN C PROGRAMMING CONDITIONAL STATEMENTS ARE POSSIBLE WITH THE HELP OF THE FOLLOWING TWO CONSTRUCTS: 1. if Statement. The syntax of the if statement in C programming is: if (test expression) { } HOW IF STATEMENT WORKS? The if statement evaluates the test expression inside the parenthesis (). If the test expression is evaluated to true, statements inside the body of if are executed. If the test expression is evaluated to false, statements inside the body of if are not executed. HOW IF STATEMENT WORKS? EXAMPLE: Write a program that will input a number and will display if it is negative. CODE: IN C PROGRAMMING CONDITIONAL STATEMENTS ARE POSSIBLE WITH THE HELP OF THE FOLLOWING TWO CONSTRUCTS: 2. if...else Statement The if statement may have an optional else block. The syntax of the if…else statement is: if (test expression) { } else { } HOW IF...ELSE STATEMENT WORKS? EXAMPLE: Write a program that will input an integer and checks whether an integer is odd or even. CODE: 3. if...else Ladder 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 if...else ladder allows you to check between multiple test expressions and execute different statements. Syntax of if...else Ladder if (test expression1) { // statement(s) } else if(test expression2) { // statement(s) } else if (test expression3) { // statement(s) }.. else { // statement(s) } WRITE A PROGRAM THAT WILL RELATE TWO INTEGERS USING =, > OR < SYMBOL. 4. Nested if...else It is possible to include an if...else statement inside the body of another if...else statement. Example: Using nested if…else statement, write a program that relates two integers using either and = similar to the if...else ladder's example. CODE: If the body of an if...else statement has only one statement, you do not need to use brackets {}. For example, this code 5. Switch Statement The switch statement allows us to execute one code block among many alternatives. You can do the same thing with the if...else..if ladder. However, the syntax of the switch statement is much easier to read and write. Syntax of switch...case switch (expression) { case constant1: // statements break; case constant2: // statements break;... default: // default statements } HOW DOES THE SWITCH STATEMENT WORK? EXAMPLE: Write a program that will create a simple calculator. CODE: