If () else statement, switch statement, while () loop, do…while() loop and for( ; ; ) loop 1.

Slides:



Advertisements
Similar presentations
Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
Advertisements

Fundamental of C programming
For loops For loops are controlled by a counter variable. for( c =init_value;c
Creating PHP Pages Chapter 7 PHP Decisions Making.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
Understanding Loops Using C Language Week 15 Mr.Omer Salih.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Chapter 4 Functions and Control Structures PHP Programming with MySQL.
Saturday May 02 PST 4 PM. Saturday May 02 PST 10:00 PM.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 2: Control Flow.
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.
Programming in C++ Lecture Notes 2 – Choice Statements Andreas Savva.
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II) Introduction to Computing Lecture 07: Repetition and Loop Statements (Part.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Control Structures Session 03 Mata kuliah: M0874 – Programming II Tahun: 2010.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Selection Statements in C++ If Statement in C++ Semantics: These statements have the same meaning as in the algorithmic language. 2- Two way selection:
Lecture 4 Introduction to Programming. if ( grade ==‘A’ ) cout
Introduction to Computer Algorithmics and Programming Ceng 113 Program Control Statements.
Compound Statements If you want to do more than one statement if an IF- else case, you can form a block of statements, or compound statement, by enclosing.
1 Flowchart notation and loops Implementation of loops in C –while loops –do-while loops –for loops Auxiliary Statements used inside the loops –break –continue.
Control Statements (Decision Making)
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
OBJECTIVES  Illustration of the Concept of Control Flow  Types of Control Flow  Knowledge about conditional and repetitive statements.  Make more.
Chapter 5: Structured Programming
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
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.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
Lecture 13 Transition from Fortran to C Yi Lin Feb 27, 2007.
Looping Construct or Statements. Introduction of looping constructs In looping,a sequence of statements are executed until some condition for termination.
CCSA 221 Programming in C CHAPTER 6 MAKING DECISIONS 1.
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 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.
Selection Executing Statements Selectively. Review We’ve seen that the C++ if statement permits a Statement to be executed selectively: if (Expression)
COMP Loop Statements Yi Hong May 21, 2015.
Beginning C For Engineers Fall 2005 Lecture 3: While loops, For loops, Nested loops, and Multiple Selection Section 2 – 9/14/05 Section 4 – 9/15/05 Bettina.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Lecture 7 Computer Programming -1-. Conditional Statements 1- if Statement. 2- if ….. else Statement. 3- switch.
CISC105 – General Computer Science Class 4 – 06/14/2006.
PGT C Programming1 Week 4 – Repetition Structures / Loops.
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
Engineering Computing I Chapter 3 Control Flow. Chapter 3 - Control Flow The control-flow of a language specify the order in which computations are performed.
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.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
The Repetition control structure using while loop.
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.
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Decisions Chapter 4.
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Lecturer CS & IT Department UOS MBDIN
Control Structures Lecture 7.
INC 161 , CPE 100 Computer Programming
Looping.
Programming Fundamentals Lecture #6 Program Control
Program Control Structures
Chapter 8 The Loops By: Mr. Baha Hanene.
Chapter 4 - Program Control
Loops in C.
Structured Program Development in C
EECE.2160 ECE Application Programming
Presentation transcript:

if () else statement, switch statement, while () loop, do…while() loop and for( ; ; ) loop 1

if() and if()…else statement In C language the using structure to write any condition by if() is: In C language the using structure to write any condition by if() is: Syntax: if (condition) { statements to execute; } 2 Syntax: if (condition) { statements to execute; } else { statements to execute; }

Continue: Example: Write a C program to solve this problem: enter a student number and his grade, then print classification according the following values: grade < 60 then print “ fail” 60 <= grade < 70 then print “ pass” 70 <= grade < 80 then print “ good” 80 <= grade < 90 then print “ very good” 90 <= grade <=100 then print “ excellent” 3

#include void main() { int id; float grade; printf(“enter student number: ”); scanf(“%d”, &id); printf(“enter student grade: ”); scanf(“%f”, &grade); if (grade <= 60) printf(“%d grade is faile\n”, id); if (grade >= 60) && (grade < 70) printf(“%d grade is pass\n”, id); if (grade >= 70) && (grade < 80) printf(“%d grade is good\n”,id); if (grade >= 80) && (grade < 90) printf(“%d grade is very good\n”, id); if (grade >= 90) && (grade <=100) printf(“%d grade is excellent\n”, id); } 4

#include void main() { int id; float grade; printf(“enter student number: ”); scanf(“%d”, &id); printf(“enter student grade: ”); scanf(“%f”, &grade); if (grade <= 60) printf(“%d grade is faile\n”, id); else if (grade >= 60) && (grade < 70) printf(“%d grade is pass\n”, id); else if (grade >= 70) && (grade < 80) printf(“%d grade is good\n”,id); else if (grade >= 80) && (grade < 90) printf(“%d grade is very good\n”, id); else if (grade >= 90) && (grade <=100) printf(“%d grade is excellent\n”, id); else printf(“out of the range”); } 5

Another example: write a program to print the name of a day’s week (i.e. we have 7 days in a week numbered from 0 to 6) according the following table: 0 Saturday 1 Sunday 2 Monday 3 Tuesday 4 Wednesday 5 Thursday 6 Friday 6

#include void main() { int n; printf(“ Enter the day number: ”); scanf(“%d”,&n); if (n == 0) printf(“\n Saturday”); if (n == 1) printf(“\n Sunday”); if (n == 2) printf(“\n Monday”); if (n == 3) printf(“\n Tuesday”); if (n == 4) printf(“\n Wednsday”); if (n == 5) printf(“\n Thursday”); if (n == 6) printf(“\n Friday”); } 7

Switch() statement: You can use switch() to represent if() not if () else. It is very simple to chose one thing from a set of choices have the same type. Syntax: switch( var ) { case v1: statement; break; case v2: statement; break;. default : statement; } 8

Example: solve the same last program by using switch. #include void main() { int n; printf(“ Enter the day number: ”); scanf(“%d”,&n); switch ( n ) { case 0: printf(“\n Saturday”); break; case 1: printf(“\n Sunday”); break; case 2: printf(“\n Monday”); break; case 3: printf(“\n Tuesday”); break; case 4: printf(“\n Wednsday”); break; case 5: printf(“\n Thursday”); break; case 6: printf(“\n Friday”); break; default: printf(“\n out of range ….”); } } 9

Ass. Write a program to print a menu on the output screen. But your choices are character. Using if() and switch() at each time. Due next lecture. 10

Loops: you can use loop when you have a number of iteration, or you need to continue a such process until enter special character (like Y or N) or number (like -1) to exit. There are a different kind of loop in C program like while(), do…while() and for() 11

While () loop Syntax: initialize variable; while(condition) { statements; increment or decrement variable; } 12

Example: write a program to enter id_number of students and print what you read until enter (N or n). #include void main( ) { char ch; int id; printf(“ Do you want to enter a student numder?(Y\N) ”); scanf(“%c”,&ch); while((ch !=‘N’)&&(ch !=‘n’)) { printf(“\n id number is: ”); scanf(“%d”, &id); printf(“id= %d \n”,id); printf(“ Do you want to enter a student numder?(Y\N) ”); scanf(“%c”,&ch); } printf(“\n end the program”); } 13

do … while() loop Syntax: initial_value; do { statements; increment or decrement; } while(condition); 14

Ass. Write the same last program by using do… while() Due next lecture 15

for () loop Syntax: for (initial value; condition; increment or decrement variable) { statements; } 16

Example: write a program to print this shape. * * * * * * * * * * 17

#include void main( ) { int i, j, k, n; printf(“\n enter number of lines: ”); scanf(“%d”, &n); for (i=1; i <= n; i++) { for(j=n; j > i; j--) { printf(“ ”); } for (k=1; k<= i; k++) { printf(“*”); } printf(“\n”); } 18