CC123 Programming 1 Week 4 PDF
Document Details
Uploaded by EliteFuturism
Cebu Technological University
Tags
Summary
This document contains notes on programming concepts such as selection statements (if statements and switch statements),logical operations (AND and OR), and their applications. It also includes exercises to reinforce the concepts. This material is relevant to an undergraduate computer science course at a Philippine university
Full Transcript
CC123 – Programming 1 Week 4 Information Technology Department STUDENT ACTI...
CC123 – Programming 1 Week 4 Information Technology Department STUDENT ACTIVITY SHEET Materials Computer, Smartphone, Student Activity Sheet, GSuite, What is Selection Statement? References https://youtu.be/F9LcfFlDIJs?s Intended Learning Outcomes i=w3I58hpxmsaIENJe https://sourceforge.net/proje At the end of the lesson, you should be able to: cts/orwelldevcpp/ Apply the different selection structures SDG Integration based on the program specification in SDG # 4 – Quality Education (Ensure inclusive and equitable quality education and promote life- program coding. long learning opportunities for all CLO1, CLO2 SDG # 9 – Innovation and Infrastructure (Build resilient infrastructure, promote inclusive and sustainable industrialization and foster innovation Lesson Preparation/ Review/Preview Direction: Imagine you are entering your room and you have a passkey authentication installed in your door. Your password is your birthday with the arrangement dd-mm-yy, if you will input the password of your door lock correctly “WELCOME” will prompt and if you entered incorrect password “NOT YOU” will prompt. 1 2 3 4 5 6 ____:____:____ 7 8 9 * 0 # Concept Notes Presentation Refer on the Handouts of the discussion. SELECTION STATEMENTS Selection statements execute a set of instructions only if a conditional expression is true and skip them if it is false. There are two selection statements in C. They are the following: a. if statement b. switch statement The if statement The if statement requires the use of parenthesis around the conditional expression. There is no semicolon after the parenthesis and after the end of the curly brace. This limits the range of actions subject to the conditional expression. The else part is optional. It may or may not be included in an if statement. The syntax of the if statement is: Conditional False if(conditional expression) Expression { Body of if… } True Body of else Body of if Exit The Else Statement The else keyword is optional and is used when there are two possible courses of action after an if statement. The first course of action is taken if the test is found to be true, else the second of action is taken if the test is found to be false. if(Conditional Expression) { Body of if…. } else { Body of else…. } Note: The body of the selection statements are contained in curly braces. If there is only one statement associated with the conditional expression then there is no requirement for the curly braces, but it is not a good programming practice. The if else-if Construct (multiple alternatives) if (conditional expression1) { statement1; statement n; } else if (conditional expression2) { statement1; statement n; }.. else { statement1; statementn; } Using Logical And (&&) Are used to combined two conditions. It returns true when all of the condition under consideration are true and returns false when all conditions are false. For example: if (conditional statement && conditional statement) { Body of if } else if (conditional statement && conditional statement) { Body of else if } else (conditional statement && conditional statement) { Body of else Using Logical Or (||) if(conditional expression || conditional expression) Body; else if(conditional expression || conditional expression) Body; else if(conditional expression || conditional expression) Body; else if(conditional expression || conditional expression) Body; else Body; The switch Statement One of the problems with multiple if-else-if constructs is that they become unwieldy to use and difficult to read. To avoid this problem the switch statement exists. It is, in effect, a series of if statements nested into one construct. The flowchart below illustrates how the switch statement works true Variable = constant1 First case body false true Variable = constant2 Second case body false The syntax of the switch statement is exit switch (variable) { case constant 1: { …………action; …………action; break;} case constant 2: { …………action; …………action; break; } default: { …………action; …………action; } } The Sort Circuit Operations Short Circuit in || Simply means the condition anywhere that returns true, then the rest will be not evaluated For example A= 1 B= 3 INC ; INC= (condition )||(condition) Short Circuit in && Simply means the condition anywhere that returns false, then the rest will be not evaluated For example A= 1 B= 3 INC ; INC= (condition )&&(condition) CompuSkill (Performance) Direction: Analyze the problem, Run the code and Debug. Screenshot the Output. And print. Submit it next meeting. Use the following any type of Selection Statement applicable in the problem. Exercise 1: Grade Calculator Write a program that takes a student’s score as input and prints the corresponding grade. Input 5 Scores and look for the Average and the Corresponding Category. 90-100: A+ 80-89: B 70-79: C 60-69: D Below 60: F Exercise 2: Even or Odd Write a program that takes an integer as input and determines whether it is even or odd. Exercise 3: Number Classification Write a program that takes an integer as input and classifies it as positive, negative, or zero. Exercise 4: Age Group Classification Write a program that takes a person’s age as input and classifies them into one of the following age groups: 0-12: Child 13-19: Teenager 20-64: Adult 65 and above: Senior Note: The body of the selection statements are contained in curly braces. If there is only one statement associated with the conditional expression then there is no requirement for the curly braces, but it is not a good programming practice.