EKT150 INTRODUCTION TO COMPUTER PROGRAMMING

Slides:



Advertisements
Similar presentations
Decisions If statements in C.
Advertisements

More on Algorithms and Problem Solving
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
BBS514 Structured Programming (Yapısal Programlama)1 Selective Structures.
 Control structures  Algorithm & flowchart  If statements  While statements.
Chapter 3 - Structured Program Development
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Week 3 – Selection Structures UniMAP SemPGT C PROGRAMMING1.
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Structural Program Development: If, If-Else Outline.
UniMAP Sem II-09/10EKT120: Computer Programming1 Week 3 – Selection Structures.
Structured Program Development Outline 2.1Introduction 2.2Algorithms 2.3Pseudo code 2.4Control Structures 2.5The If Selection Structure 2.6The If/Else.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
Chapter 6: Control Structures Computer Programming Skills Second Term Department of Computer Science Foundation Year Program Umm Alqura.
C Lecture Notes 1 Structured Program Development.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Decision making statements. Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
Chapter#3 Part1 Structured Program Development in C++
Week 4 Program Control Structure
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
Control Statements: Part1  if, if…else, switch 1.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
BY ILTAF MEHDI(MCS,MCSE, CCNA) 1. INSTRUCTOR: ILTAF MEHDI (MCS, MCSE, CCNA, Web Developer) BY ILTAF MEHDI(MCS,MCSE, CCNA) 2 Programming Fundamentals Chapter.
Decision making If.. else statement.
CSE1301 Sem Selection Lecture 25 Lecture 9: Selection.
‘C’ Programming Khalid Jamal.
The if…else Selection Statement
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
- Standard C Statements
Decisions Chapter 4.
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Week 3 C Program Structures (Selection Structures)
Week 3 – Selection Structures
Chapter 2.1 Control Structures (Selection)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Week 4 – Repetition Structures / Loops
Condition Statements.
DKT121: Fundamental of Computer Programming
Chapter 6: Conditional Statements and Loops
SELECTION STATEMENTS (1)
Lecture 2: Logical Problems with Choices
How to develop a program?
CS1100 Computational Engineering
Structured Program
Chapter 4 - Program Control
1) C program development 2) Selection structure
Chapter 3 - Structured Program Development
3 Control Statements:.
Decision making If statement.
Chapter 3 - Structured Program Development
Summary Two basic concepts: variables and assignments Basic types:
Week 3 – Program Control Structure
Control Structures Lecture 6.
Structured Program Development in C++
Dale Roberts, Lecturer IUPUI
Selection Control Structure
REPETITION Why Repetition?
Switch Case Structures
Control Structures.
Structural Program Development: If, If-Else
Presentation transcript:

EKT150 INTRODUCTION TO COMPUTER PROGRAMMING Control Structure: Selection Part I Dr. Nur Hafizah Ghazali adilahhanin@unimap.edu.my

Outline Intro to control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement

Learning Objectives Apply concepts for developing algorithms, using variables and structuring a C program Explain what is meant by a control structures Explain the three basic types of control structures

Control Structure All programs can be written in terms of three control structures (like building blocks) Sequence Structure Selection Structure Repetition Structure

1. Sequence Structure Sequence Structure ‘Built-in’ to C Unless otherwise directed, one statement after the next is executed Is a series of steps executed sequentially by default Pseudo Code Flow Chart Begin Input A and B Calculate A + B Print result of SUM End Begin Read input A,B Calculate SUM=A + B Print SUM End

2. Selection Structure Used to choose among alternative courses of action Used to control the flow of a program Also called as decision or branches Branches are conditions or choices used to enable selection of program flow C has three types: If selection statement (one-way selection) If…else selection statement (two-way selection) Switch selection statement (multiple selection)

One Way Selection : if if structures is a single-entry/single-exit structure The primary tool of selection structure Used when we want a single or a group of statements to be executed if the condition is true. If the condition is false, the statement is skipped and program continues by executing the first statement after the if selection structure Condition is express as a statement and two possible outcomes are indicate by true and false

if syntax – Part 1 Exercise: Draw flowchart for the syntax above. if (test expression) { statement/s to be executed if test expression is true; } Exercise: Draw flowchart for the syntax above.

Flow Chart : if Selection Test expression True Statement (s) False

if syntax – Part 2 if (this logical expression is true) { statement 1; } next_statement; NO SEMI-COLON! The expression in parentheses can be any expression that results in a value of true or false If the expression is true, Statement 1 is executed, after which the program continues with next_statement If the expression is false, Statement 1 is skipped and execution continues immediately with next_statement

if statement : Basic Understanding Example : Is it raining? If the answer yes, take umbrella Then walk to work Exercise: Draw flowchart for the above statement

If statement : Flow Chart Expression Statement Next Statement True False Is it raining False True Take umbrella Walk to work

If statement : Example 1 Exercise: Draw flowchart for the above code #include <stdio.h> int main() { int num; printf("Enter a number to check.\n"); scanf("%d",&num); if (num<10)/* checking whether number is less than 0 or not. */ { /*If test condition is true, statement below will be executed, otherwise it will not be executed */ printf("Number = %d\n",num); } printf("if statement in C programming is easy."); return 0; Exercise: Draw flowchart for the above code Predict and display the output

If statement : Example 2 Exercise: Draw flowchart for the above code int main () { int num; printf (“Enter a number between -10 and 10:\n”); scanf (“%d”, &num); if (num > 0) printf (“%d is a positive number\n”, num); return 0; } Exercise: Draw flowchart for the above code Predict and display the output

If statement : Example 3 Exercise: Draw flowchart for the above code #include <stdio.h> int main (void) { int number = 0; printf (“\nEnter an integer between 1 and 10\n”); scanf (“%d”, &number); if (number > 3) printf (“%d is greater than 3\n”, number); if (number < 6) printf (“%d is less than 6\n”, number); return 0; } Exercise: Draw flowchart for the above code Predict and display the output

If statement : Example 4 Write flow chart and a C program to determine if the student pass based on an average of 2 test marks. The pass marks is 40 and above.

Two Way Selection : if … else Specifies and action to be performed both when the condition is true and when it is false If the boolean expression (or statements) evaluates to true, then the if block of code will be executed, otherwise else block of code will be executed.

if … else Syntax Exercise: Draw flowchart for the syntax above. if (this logical expression is true) { statements(s) will be execute if the logical expression is true } else statement(s) will be execute if the logical expression is false NO SEMI-COLON! NO SEMI-COLON! Exercise: Draw flowchart for the syntax above.

If … else statement : Flow Chart Test expression True Statement(s) False Statement(s)

if … else : Basic Understanding If the rain today is worse than the rain yesterday I will take my umbrella else I will take my jacket Then I will go to work Exercise: Draw flowchart for the above statement

if … else : Basic Understanding If the rain today is worse than the rain yesterday I will take my umbrella else I will take my jacket Then I will go to work Take jacket True False Take umbrella Rain worse than yesterday Go to work

if … else statement : Example 1 #include <stdio.h> int main () { int num; printf (“Enter a number between -10 and 10;”); scanf (“%d”, &num); if (num >= 0 ) printf(“%d is a positive number\n”, num); else printf (“%d is a negative number\n”, num); return 0; } Exercise: Draw flowchart for the above code Predict and display the output

if … else statement : Example 2 #include <stdio.h> int main () { int num; printf (“Enter a number you want to check.\n”); scanf (“%d”, &num); if ((num%2) == 0) /*checking whether remainder is 0 or not*/ printf (%d is even number.”, num); else printf (“%d is odd number.”, num); return 0; } Exercise: Draw flowchart for the above code Predict and display the output

if … else statement : Example 3 #include <stdio.h> int main () { int num = 0; printf (“Enter a number you want to check.\n”); scanf (“%d”, &num); if( num < 20 ){ /* if condition is true then print the following */ printf(“%d is less than 20\n“,num ); } else{ /* if condition is false then print the following */ printf(“%d is equal to or greater than 20\n“,num ); return 0; Exercise: Draw flowchart for the above code Predict and display the output

if … else statement : Example 4 Write flow chart and a C program to determine if the student pass or failed based on an average of 2 test marks. The pass marks is 40 and above.

Multiple Selection : if … else if An if…else if control structure shifts program control, step by step, through a series of statement blocks. Is used when program requires more than one test expression. Very useful to test various conditions using single if…else statement.

if … else if Syntax Exercise: Draw flowchart for the syntax above. if (test expression1) { statements(s) to be execute if test expression1 is true; } else if (test expression2) { statement(s) to be execute if test expression1 is false and 2 is true; else if (test expression3) { statement(s) to be execute if test expression1 and 2 are false and 3 is true; … else { statements to be executed if all test expressions are false; Exercise: Draw flowchart for the syntax above.

if … else if statement : Flow Chart Test expression 1 True Statement(s) False Test expression 2 True Statement(s) False Test expression 3 True Statement(s) False Statement(s)

if … else if statement : Example 1 #include <stdio.h> int main(){ int numb1, numb2; printf("Enter two integers for comparison: "\n); scanf("%d %d", &numb1,&numb2); if(numb1==numb2) /*checking whether two integers are equal*/ printf("Result: %d = %d",numb1,numb2); else if(numb1>numb2) /*checking whether numb1 is greater than numb2*/ printf("Result: %d > %d",numb1,numb2); else printf("Result: %d > %d",numb2,numb1); return 0; } Exercise: Draw flowchart for the above code Predict and display the output

if … else if statement : Example 2 #include <stdio.h> int main () { char grade; printf(“Enter your grade:”); scanf(“%c”,&grade): if( grade == ‘A’ ) printf(“Your academic performance is excellent\n" ); else if( grade == ‘B’ ) printf(“Your academic performance is good\n" ); else if( grade == ‘C’ ) printf(“Your academic performance is satisfactory\n" ); else printf(“Your academic performance is poor\n" ); return 0; } Exercise: Draw flowchart for the above code Predict and display the output

if … else if statement : Example 3 Write flow chart and a C program to determine if the student pass or failed based on an average of 2 test marks. If the average mark is equal to and above 45, display “Passed”, if the average mark is between 40 and 45, display “Conditional Passed” and display “Failed” for marks lower than 40.

Compound (Block of ) Statement: Any block of the code is written or embedded inside the pair of the curly braces. If condition specified inside the ‘if block’ is true, then statements written inside the pair of curly braces will be executed If expression evaluates to false, all the statements between the braces following the else will be executed In either case, execution continues with next statement. if (expression) { StatementA1; StatementA2; ... } else StatementB1; StatementB2; Next_Statement;

Compound Statement: Basic Understanding If the weather is sunny, I will walk to the park, have a picnic and walk home else I will stay in, watch football and drink coke Sleep at 10 Exercise: Draw flowchart for the above statement

Compound Statement: Basic Understanding True Weather is sunny False Stay in Walk to park False Watch Football Have a picnic Drink coke Walk home Sleep

Compound Statement: Example 1 int main() { int num = 10 ; if(num > 0) printf ("\nNumber is Positive"); printf ("\nThis is The Example of Compound Statement"); } return 0;

Compound Statement: Example 2 #include<stdio.h> int main() { int iAge=18; if (iAge > 17 ) printf (“Eligible to take car driving license\n”); print (“Allow to register at driving school\n”); } else printf (“Not eligible to drive\n”); printf (“Not allows to register at driving school\n”); return 0;