Chapter 6: Control Structures Computer Programming Skills 4800153-3 Second Term 1435-1436 Department of Computer Science Foundation Year Program Umm Alqura.

Slides:



Advertisements
Similar presentations
Unit 2 The if-else-if LECTURE – 14
Advertisements

If Statements & Relational Operators Programming.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
1 CSE1301 Computer Programming Lecture 10: Iteration (Part 1)
Some loop programs 1: Read in 10 integers and output their sum
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Programming Control Flow. Sequential Program S1 S2 S5 S4 S3 int main() { Statement1; Statement2; … StatementN; } Start End.
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.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Operators and Expressions
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
Chapter 4: Operators Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills /1436.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
CSE 251 Dr. Charles B. Owen Programming in C1 Flow Control and Booleans 1. Please get logged in. 2. Open a new terminal window Applications/Accessories/Terminal.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
RELATIONAL OPERATORS LOGICAL OPERATORS CONDITION Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
1 If statement and relational operators –, >=, ==, != Finding min/max of 2 numbers Finding the min of 3 numbers Forming Complex relational expressions.
Do-while loop Syntax do statement while (loop repetition condition)
A Program is nothing but the execution of sequence of one or more Instructions. On the basis of application it is essential. To Alter the flow of a program.
CHAPTER 8 CONTROL STRUCTURES Prepared by: Lec. Ghader R. Kurdi.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Chapter 5: Structured Programming
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.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
Python Conditionals chapter 5
CSCI 171 Presentation 5. The while loop Executes a block as long as the condition is true general form: while (condition) { statement 1; statement 2;
Chapter 4 Control Structures: Selection We introduced the three fundamental control structures from which all programs are developed: 1. Sequence structures.
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.
Lesson thirteen Conditional Statement "if- else" ©
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Chapter 3. Control Structure C++ Programing. Conditional structure C++ programming language provides following types of decision making statements. Click.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Silberschatz and Galvin  C Programming Language Decision making in C Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University.
 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.
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
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 in C.
Decisions Chapter 4.
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Chapter 4: Control Structures
Week 4 – Repetition Structures / Loops
Condition Statements.
Functions Dr. Ashish Sasankar. In programming, a function is a segment that groups code to perform a specific task. A C program has at least one function.
CSI 121 Structure Programming Language Lecture 10: Iteration (Part 1)
Introduction to Programming
Chapter 4 - Program Control
Conditionals.
SELECTIONS STATEMENTS
Chapter 5: Selection Statement
Control Structures Lecture 6.
CHAPTER 5: Control Flow Tools (if statement)
CSCE 206 Lab Structured Programming in C
Control Structure គោលបំណងនៃមេរៀន អ្វីជា Control structure ?
Presentation transcript:

Chapter 6: Control Structures Computer Programming Skills Second Term Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Place photo here

Objectives To understand the principles of control structures. To be able to use the if selection statement and if…else selection statement to select actions. The Outcomes: Students should be able to understand the control structures. Student should be able to Use control structures correctly. Control Structures:

Conditional Constructs There are different types of conditional constructs. If statement (if statement, if-else statement and Nested if- else structure) Switch statement.

Types of Conditional Constructs: StatementDescription if statement An if statement consists of a Boolean expression followed by one or more statements. if...else statement An if statement can be followed by an optional else statement, which executes when the Boolean expression is false. nested if-else statements You can use one if or else if statement inside another if or else if statement(s). switch statement A switch statement allows a variable to be tested for equality against a list of values.

The Basic If Structure: The syntax of an if statement in C programming language is: if(expression) { /* statement(s) will execute if the expression is true */ }

The Basic If Structure: Example

The Basic If Structure: Example 1: Write a C program to print the number entered by user only if the number entered is negative. #include int main(){ int num; printf("Enter a number to check.\n"); scanf("%d",&num); if(num<0) { /* checking whether number is less than 0 or not. */ printf("Number = %d\n",num); } /*If test condition is true, statement above will be executed, otherwise it will not be executed */ printf("The if statement in C programming is easy."); return 0; }

The Basic If Structure: Example 2: Write a C program to check if the value a is less than 20 or not. #include int main () { /* local variable definition */ int a = 10; /* check the boolean condition using if statement */ if( a < 20 ) { /* if condition is true then print the following */ printf("a is less than 20\n" ); } printf("value of a is : %d\n", a); return 0; }

The Basic If Structure: Example 3: Write a C program to sort two numbers entered by user. #include int main(void) int num1,num2; int remember_num1; printf("Enter two numbers: "); scanf{"%d %d", &num1,&num2); if(num1 > num2) { Remember_num1 = num1; num1 = num2; num2 = remember_num1; } printf("The input in sorted order: %d and %d\n.", num1,num2); return 0;}

The If-Else Structure: The syntax of an if...else statement in C programming language is: if(boolean_expression) { /* statement(s) will execute if the boolean expression is true */ } else { /* statement(s) will execute if the boolean expression is false */ }

The If-Else Structure:

Example 1: Check if a given number is Zero or Not. #include int main(){ int value; printf("Enter a number you want to check.\n"); scanf("%d",& value); if(value==0) //checking the value is 0 or not. printf("value is 0"); else printf("value is not 0"); return 0; }

The If-Else Structure: Example 2: Check if a given number is Even or Odd. #include 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.",num); else printf("%d is odd.",num); return 0; }

The If-Else Structure: Example 3: Finding the Max between two integers #include int main() { int a, b; printf("\nEnter value of a & b : "); scanf("%d %d", &a, &b); if (a > b) printf("\n a is Max"); else printf("\n b is Max "); return(0); }

The If-Else Structure: Example 4: C Program to Accept two Integers and Check if they are Equal. #include void main() { int m, n; printf("Enter the values for M and N\n"); scanf("%d %d", &m, &n); if (m == n) printf("M and N are equal\n"); else printf("M and N are not equal\n"); }

Selection : There are two major ways of accomplishing this choice: Nested if structure if-else statements "glued" together Switch statement

The syntax for Nested if structure: The nested if...else statement is used when program requires more than one test expression. if ( condition1 ) statement1 ; else if ( condition2 ) statement2 ;... else if ( condition-n ) statement-n ; else statement-e ;

The syntax for Nested if structure: Example 1:Write a C program to relate two integers entered by user using = or > or < sign. # include 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); else printf("Result: %d > %d",numb2,numb1); return 0; }

The syntax for Nested if structure: Example 2: Write a C program to find largest number among three numbers entered by user using nested if...else statement. #include int main(){ float a, b, c; printf("Enter three numbers: "); scanf("%f %f %f", &a, &b, &c); if(a>=b && a>=c) printf("Largest number = %.2f", a); else if(b>=a && b>=c) printf("Largest number = %.2f", b); else printf("Largest number = %.2f", c); return 0; }