Lecture 4: Selection Control Structure

Slides:



Advertisements
Similar presentations
Fundamental of C programming
Advertisements

1 Lecture 8:Control Structures I (Selection) (cont.) Introduction to Computer Science Spring 2006.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
School of Computing Science CMT1000 Ed Currie © Middlesex University 1 CMT1000: Introduction to Programming Ed Currie Lecture 5B: Branch Statements - Making.
1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23.
12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 7 Decision Making : selection statements.
Principles of Programming - NI July Chapter 5: Structured Programming In this chapter you will learn about: Sequential structure Selection structure.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 5: Structured Programming.
CHAPTER 5: Decision Making and Looping CSEB113 PRINCIPLES of PROGRAMMING by Badariah Solemon 1BS (Sept 2013)
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
1 If statement and relational operators –, >=, ==, != Finding min/max of 2 numbers Finding the min of 3 numbers Forming Complex relational expressions.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Chapter 5: Structured Programming
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
Chapter 5: Structured Programming
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
Week 4 Program Control Structure
IT CS 200: C ONDITION Lect. Napat Amphaiphan. T HE ABILITY TO CONTROL THE FLOW OF YOUR PROGRAM, LETTING IT MAKE DECISIONS ON WHAT CODE TO EXECUTE 2.
COMP Loop Statements Yi Hong May 21, 2015.
1 Lecture03: Control Flow 9/24/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
C++ Programming Lecture 5 Control Structure I (Selection) – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
STRUCTURED PROGRAMMING Selection Statements. Content 2  Control structures  Types of selection statements  if single-selection statement  if..else.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
Control Statements: Part1  if, if…else, switch 1.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C 1 A.AlOsaimi King Saud University College of Applied studies and Community Service Csc 1101.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
CSE1301 Sem Selection Lecture 25 Lecture 9: Selection.
Chapter 4 – C Program Control
Chapter 5: Structured Programming
CSE 220 – C Programming Loops.
The if…else Selection Statement
Decision Making in C.
Selection (also known as Branching) Jumail Bin Taliba by
Decisions Chapter 4.
Flow of Control.
Chapter 4 - Program Control
CSC113: Computer Programming (Theory = 03, Lab = 01)
CS1010 Programming Methodology
JavaScript: Control Statements.
Lecture 2: Logical Problems with Choices
Structured Program
Chapter 4 - Program Control
3 Control Statements:.
CSC215 Lecture Flow Control.
Conditions and Boolean Expressions
CSC215 Lecture Control Flow.
Chapter 7 Conditional Statements
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Week 3 – Program Control Structure
CSC215 Lecture Control Flow.
Controlling Program Flow
Presentation transcript:

Lecture 4: Selection Control Structure

Objectives In this chapter you will learn about: Sequential structure Selection structure if if … else switch

Sequential Structure Statements are executed one by one until the end of the program is reached. A group of statements that executed sequentially which is usually grouped (bracketed) by { } is known as Compound Statement

Sequential Structure - example int main(void) { int count = 0; printf(“Count = %d\n”, count); count++; return 0; }

Selection Structure In selection structure, the program is executed based upon the given condition. Only instructions that satisfy the given condition are executed. There are 3 types of selection structure: if One alternative if…else Two alternatives nested if..else Multiple alternatives switch

Selection structure: if Syntax : if (condition) Statement; The statement is only executed if the condition is satisfied. Example: if (score >= 60) printf(“Pass!!\n”); In the example above, the word “Pass!!” will only be printed out if score is larger than or equal to 60. If not, the word “Pass!!” will not be printed out and the program will continue with the next statement. A condition is an expression that can return true or false (usually involving the use of an operator). Note that there is no semicolon (;) after the if statement. If there is one, that means the if statement and the printf() statement are 2 different statements and they will both get executed sequentially.

Selection structure: if… else Syntax : if (condition) statement1; else statement2; If the condition is satisfied, statement1 will be executed. Otherwise, statement2 will get executed. Example : if (score >= 60) printf(“Pass!!\n”); printf(“Fail!!\n”) In the above example, the word “Pass!!” will be printed if the value of score is bigger than 60 or equal to 60. Otherwise the string ‘Fail!!” will be printed out

Nested if… else statements A nested if…else statement is an if…else statement with another if…else statements inside it. Example : if (score >= 90) printf(“A\n”); else if (score >= 80) printf(“B\n”); else if (score >= 70) printf(“C\n”); else if (score >= 60) printf(“D\n”) else printf(“F\n”); The else if statement means that if the above condition is not satisfied, then try checking this condition.If any one of the condition is already satisfied, then ignore the rest of the available conditions

Plurality of Statements In the examples that we have seen so far, there is only one statement to be executed after the if statement. If we want to execute more than one statement after the condition is satisfied, we have to put curly braces { } around those statements to tell the compiler that they are a part of the if statement, making it a Compound Statement Example if (score >= 90) { printf(“You have done very well\n”); printf(“I’ll give you a present\n”); } else if (score >= 60) printf(“You have passed the course\n”); printf(“Sorry No present from for you\n”); printf(“Go and celebrate on your own\n”);

Selection structure: switch A switch statement is used to choose one choice from multiple cases and one default case. Syntax: switch (variable) { case case1: statement1; break; case case2: statement2; … default; statement; } The break statement is needed so that once a case has been executed, it will skip all the other cases and go outside the switch statement. If the break statement is omitted, the execution will be carried out to the next alternatives until the next break statement is found.

switch - example int number; printf(“Enter a positive integer number: “); scanf(“%d”,&number); switch (number) { case 1: printf(“One!!\n”); break; case 2: printf(“Two!!\n”); case 3: printf(“Three!!\n”); default: printf(“Others\n”); } This program reads a number from the user and print out the string equivalent for 1, 2 or 3. If the value being keyed in is other than 1,2 or 3, the default statement will be executed where the statement “Others” will be printed out.

Switch cont… The value for ‘case’ must be integer or character constant. Eg.1 switch (number) { case 1 : statement; break; …. Eg.2 switch (color) { case ‘R’ : break; The order of the ‘case’ statement is unimportant

SUMMARY In this lecture, you’ve learnt about selection control structures in C programming : Sequential Selection if..else nested if..else switch