EKT150 INTRODUCTION TO COMPUTER PROGRAMMING

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

BBS514 Structured Programming (Yapısal Programlama)1 Selective Structures.
true (any other value but zero) false (zero) expression Statement 2
Programming Control Flow. Sequential Program S1 S2 S5 S4 S3 int main() { Statement1; Statement2; … StatementN; } Start End.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
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.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Computer Science Department Relational Operators And Decisions.
UniMAP Sem II-09/10EKT120: Computer Programming1 Week 3 – Selection Structures.
Selection Structures (if & switch statements) (CS1123)
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.
Chapter 6: Control Structures Computer Programming Skills Second Term Department of Computer Science Foundation Year Program Umm Alqura.
Previously Repetition Structures While, Do-While, For.
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.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
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.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
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
IT CS 200: R EPEATATION Lect. Napat Amphaiphan. T HE ABILITY TO DO THE SAME TASK AGAIN BY AGAIN UNTIL THE CONDITION IS MET LOOP 2.
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 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C 1 A.AlOsaimi King Saud University College of Applied studies and Community Service Csc 1101.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
 Real numbers representation - Floating Point Notation  First C Program  Variables Declaration  Data Types in C ◦ char, short, int, long, float, double,
Module 6 – Decision Control Statements Objectives  Understands Increment/Decrement operators, Conditional and special operators in C  Understands significance.
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.
CSE1301 Sem Selection Lecture 25 Lecture 9: Selection.
The if…else Selection Statement
Decision Making in C.
Chapter 3 Control Statements
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Administrative things
- Standard C Statements
Decisions Chapter 4.
Week 3 C Program Structures (Selection Structures)
Chapter 4 (Conditional Statements)
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Chapter 4 - Program Control
Week 3 – Selection Structures
Week 4 – Repetition Structures / Loops
Condition Statements.
DKT121: Fundamental of Computer Programming
Introduction to Programming
Lecture 2: Logical Problems with Choices
Decision Making.
CS1100 Computational Engineering
Structured Program
Chapter 4 - Program Control
3 Control Statements:.
CPS125 Week 5.
Week 3 – Program Control Structure
Structured Program Development in C++
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Chapter 4 - Program Control
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Dale Roberts, Lecturer IUPUI
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

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

Outline Continues with Selection Structures Nested if Conditional Operator Switch Structures

Nested if Test for multiple cases by placing if…else statements inside if…else statement It is possible to have if within if statement When one control statement is within another, it is said to be nested

Nested if : Syntax Examples No specific syntax for nested if Depends on the program structure Must have correct syntax for each if/ if- else/ if – else if statement if (expression1) { Statement A; if (expression2) Statement B; else Statement C; } Statement D; Statement E; if (expression1) { Statement A; if (expression2) Statement B; if (expression3) Statement C; } else Statement D; Statement E;

Example of Nested if – Basic Understanding If the weather is good, I will go out in the garden, And if it’s warm, I will sit in the sun. else I will sit in the shade I will stay indoors I will then drink some lemonade Exercise: Draw flowchart for the above statement

Example of Nested if – Syntax if (expression1) /*Weather is good?*/ { Statement A; /*Yes – go out in the garden*/ if (expression2) /*Warm?*/ Statement B; /*Yes – sit in the sun*/ else Statement C; /*No – Sit in the shade*/ } Statement D; /*Weather not god – stay in*/ Statement E; /*Drink lemonade in any event*/

Example of Nested if – Flow Chart Is the weather good? YES Sit Indoors Out in the garden NO Is it warm? NO YES Sit in sun Sit in shade Drink some lemonade

Nested if – Exercise 1 Exercise: Draw flowchart for the above code #include <stdio.h> int main() { int numb1, numb2; printf("Enter two integers to check\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); printf("Result: %d > %d",numb2,numb1); } return 0; Exercise: Draw flowchart for the above code Predict and display the output

Nested if – Exercise 2 Exercise: Draw flowchart for the above code #include<stdio.h> int main() { int mark; printf(“Enter mark:\n”); scanf("%d",&mark); if ( mark <= 10 ) printf("You did not study.\n"); printf("You failed!\n"); } else { // mark is 11 and above if ( mark < 60 ) // mark is between 11 and 60 printf("You failed !\n"); printf("Study harder\n"); else { //mark is equal or above than 60 printf("You passed!\n"); return 0; Exercise: Draw flowchart for the above code Predict and display the output

Read input temperature Nested if – Exercise 3 Write a C program according to the following flow chart. Include one sample output of the program. Temperature<= 25 YES Begin Read input temperature temperature <= 35 NO Display “Warm” Display “Hot” End temperature <= 10 “ Cold” “Mild”

Conditional Operator ( ? : ) The conditional operator in C is also known as ternary operator – because it has three arguments. Evaluates an expression returning a value if that expression is true and different one if the expression is evaluated as false

Conditional Operator - Syntax Condition ? result1 : result2 If the condition is true, result1 is returned else result2 is returned

Conditional Operator – Basic Understanding c =( c > 0 ) ? 10 : - 10 If c is greater than 0, value of c will be 10 but, if c is less than n0, value of c will be -10

Conditional Operator – Example 1 10 == 5 ? 11 : 12 10 ! = 4 ? 4 : 3 12 > 8 ? a : b Exercise: Predict and display the output for above conditional operator

Conditional Operator – Example 2 #include <stdio.h> int main() { int a , b; a = 10; printf( "Value of b is %d\n", (a == 1) ? 20: 30 ); printf( "Value of b is %d\n", (a == 10) ? 20: 30 ); } Exercise: Predict and display the output for above conditional operator

Conditional Operator – Example 3 #include <stdio.h> int main() { char feb; int days; printf("Enter 1 if the year is leap year otherwise enter 0: "); scanf("%c",&feb); days = (feb==‘1') ? 29:28; /*If test condition (feb==‘1’) is true, days will be equal to 29. */ /*If test condition (feb==‘1') is false, days will be equal to 28. */ printf("Number of days in February = %d",days); return 0; } Exercise: Predict and display the output for above conditional operator

Switch Structures Similar to if-else if control structure Allows a variable to be tested for equality against a list of values Each value is called a case, and the variable being switched on is checked for each switch case.

Switch Structures : Syntax switch (expression) { case constant-expression 1: statement (s); break; case constant-expression 2: . default: statements; }

Switch Structures : Flow Chart Is switch expression == case constant1? True Statement for first case False Is switch expression == case constant2? True Statement for second case False Default statements

Switch Structures : Rules (1) The expression used in a switch statement must have an integral or enumerated type, or be of a class type in which the class has a single conversion function to an integral or enumerated type Can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon. The constant-expression for a case must be the same data type as the variable in the switch, and it much be constant or a literal.

Switch Structures : Rules (2) When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached. When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached Can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.

Switch Structures : Example 1 #include <stdio.h> int main () { char grade = 'B'; switch(grade) case 'A' : printf("Excellent!\n" ); break; case 'B' : case 'C' : printf("Well done\n" ); case 'D' : printf("You passed\n" ); break; case 'F' : printf("Better try again\n" ); default : printf("Invalid grade\n" ); } printf("Your grade is %c\n", grade ); return 0;

Switch Structures : Example 2 int main () { int choice; printf (“1. Create a new database\n”); printf (“2. Edit a database\n”); printf (3. Delete a database\n”); printf (“4. Merge database\n”); printf (“5. Exit system\n”); printf (“Choose an option:”); scanf (“%d”, &choice); switch (choice) { case 1: printf (“Creating….\n”); break; case 2: printf (“Editing…\n”); case 3: printf (“Deleting…\n”); break; case 4: printf (“Merging…\n”); case 5: printf (“Thank you, Bye.\n”); default:: printf (“Invalid input!\n”); } return 0;

Switch Structures : Example 3 #include <stdio.h> int main() { char o; float num1,num2; printf("Select an operator either + or - or * or / \n"); scanf("%c",&o); printf("Enter two operands: "); scanf("%f%f",&num1,&num2); switch(o) { case '+': printf("%.1f + %.1f = %.1f",num1, num2, num1+num2); break; case '-': printf("%.1f - %.1f = %.1f",num1, num2, num1-num2); break; case '*': printf("%.1f * %.1f = %.1f",num1, num2, num1*num2); break; case '/': printf("%.1f / %.1f = %.1f",num1, num2, num1/num2); break; default: /* If operator is other than +, -, * or /, error message is shown */ printf("Error! operator is not correct"); } return 0;