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.

Slides:



Advertisements
Similar presentations
Fundamental of C programming
Advertisements

1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
C++ for Engineers and Scientists Third Edition
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.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
UNIT II Decision Making And Branching Decision Making And Looping
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Conditional Statement
Computer Science Department Relational Operators And Decisions.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Control Statements (Decision Making)
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Chapter 5: Structured Programming
Chapter 05 (Part III) Control Statements: Part II.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
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.
Lecture #7 CONTROL STRUCTURE & FLOW CHARTS
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
CCSA 221 Programming in C CHAPTER 6 MAKING DECISIONS 1.
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.
Week 4 Program Control Structure
Control Flow Statements
BY ILTAF MEHDI (MCS, MCSE, CCNA)1. INSTRUCTOR: ILTAF MEHDI (MCS, MCSE, CCNA, Web Developer) BY ILTAF MEHDI (MCS, MCSE, CCNA)2 Chapter No: 04 “Loops”
CPS120: Introduction to Computer Science Decision Making in Programs.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
COURSE: BY:HIRA FARMAN. TOPIC: BY:HIRA FARMAN CHAPTER # 04: LET US C & TURBO C.
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.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
THE DECISIONS CONTROL STRUCTURE! CHAPTER 2. Transfer of control statement: o The statement of computer program are executed one after the other in the.
Decision making If.. else statement.
‘C’ Programming Khalid Jamal.
Selection (also known as Branching) Jumail Bin Taliba by
Decisions Chapter 4.
Week 3 C Program Structures (Selection Structures)
EGR 2261 Unit 4 Control Structures I: Selection
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Week 3 – Selection Structures
Condition Statements.
DKT121: Fundamental of Computer Programming
JavaScript: Control Statements.
Control Structures.
Introduction to Programming
IF if (condition) { Process… }
Control Structures: Selection Statement
Chapter 7 Conditional Statements
Chapter 4: Control Structures I (Selection)
Boolean Expressions to Make Comparisons
Week 3 – Program Control Structure
Control Structures Lecture 6.
Structured Program Development in C++
PROGRAM FLOWCHART Selection Statements.
Control Structures: Selection Statement
ECE 103 Engineering Programming Chapter 22 Selection
Switch Case Structures
Presentation transcript:

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 No: 03

BY ILTAF MEHDI(MCS,MCSE, CCNA) 3 Course Contents Chapter NoChapter NamePage No 1. The Turbo C Programming Environment C Building Blocks Decisions Loops Functions Arrays and Strings 179

BY ILTAF MEHDI(MCS,MCSE, CCNA) 4 Chapter No 03 Detail: The if statement The if-else Statement The else-if statement The switch statement The conditional expression operator

Conditional Structure BY ILTAF MEHDI(MCS,MCSE, CCNA) 5 The computer can perform arithmetic calculations correctly and rapidly. Besides these capabilities the computer is also capable of quick decision-making and repetitions. A statement or set of statements that executed when a particular condition is true and ignored when the condition is false is called conditional statement. The computer tests a condition and selects one of the two alternatives actions depending on whether the condition is true or false. C/C++ has five decision making statements: if statement if-else statement The else-if statement The switch statement and The conditional expression operator.

If Statement BY ILTAF MEHDI(MCS,MCSE, CCNA) 6 The fundamental decision making statement in C/C++ is the if statement. This statement is also called as conditional jump statement or conditional transfer of control statement, because it is mostly used to transfer the control to another statement if and only if a condition is true. If the condition is not fulfilled i.e. if the condition is false, transfer of control does not occur and the program execution moves on to the next statement following the if structure. Syntax: if(condition)or if(condition) Statement; { statement 1; statement 2; } <?php $satisfied = "very"; if ( $satisfied == "very" ) { print "We are pleased that you are happy with our service"; // register customer satisfaction in some way } ?> It is if-statement example used in PHP

The if Statement BY ILTAF MEHDI(MCS,MCSE, CCNA) 7 The basic if statement allows your program to execute a single statement, or a block of statements enclosed between braces, if a given condition is true. Example int a = 0, b=1; if (a < b) printf(“Hello”); Condition is true? Statement or Block of Statements Next Statement Yes No

C-program of a simple if statement BY ILTAF MEHDI(MCS,MCSE, CCNA) 8 #include void main(void) { clrscr(); int no; printf(“enter any number?”); scanf(“%d”, &no); if(no>10) { printf(“ the value you entered is above 10”); printf(“its ok “); } If (no<10) printf(“below 10 number is entered”); getch(); }

Making a decision BY ILTAF MEHDI(MCS,MCSE, CCNA) 9 C-program for the use of a conditional if statement. #include void main(void) { clrscr(); int val; printf(“Enter a value between 50 and 100”); scanf(“%d”, &val); if(val < 50) printf(“Wrong value entered, below 50”); if (val > 100) printf( “Wrong Value entered above 100”); if (val>=50 && val <=100) printf( “You have entered the correct value:%d“,val); getch(); }

Q: Get a value from the keyboard then find that the value is positive or negative BY ILTAF MEHDI(MCS,MCSE, CCNA) 10 #include void main(void) { clrscr(); int number; printf ( “Enter a number on your choice ”); scanf(“%d”, &number); if(number < 0) printf ( “the number you entered is negative”); if (number > 0) printf ( “the number you entered is positive”); if (number == 0) printf(“you enter the number equal to 0”); printf ( “You have entered :%d“, number); getch(); }

No The if-else Statement BY ILTAF MEHDI(MCS,MCSE, CCNA) 11 The basic if-else statement allows your program to execute a single statement, or a block of statements enclosed between braces, if a given condition is true, and another statement or block of statements if the condition is false. Example int a = 0, b=1; if (a < b) printf(“ a is less than b”); else printf(“ a is greater than or equal to b ”); Condition is true? Statement or Block of Statements for true Next Statement Yes Statement or Block of Statements for false if (expression) { // code to execute if the expression evaluates to true } else { // code to execute in all other cases } if (expression) { // code to execute if the expression evaluates to true } else { // code to execute in all other cases } if-else example in PHP

Syntax of if-else Statement BY ILTAF MEHDI(MCS,MCSE, CCNA) 12 It has two syntaxes: i. if(condition)ii. If(condition) statement; { else statement 1; statement; statement 2; } else { statement1; statement2; }

Create a program in C that get two values from the keyboard and then show the greatest value. BY ILTAF MEHDI(MCS,MCSE, CCNA) 13 #include void main(void) { clrscr(); int val1, val2; printf(“Enter the first value: ”); scanf(“%d”, &val1); printf( “Enter the second value: ”); scanf(“%d”, &val2); if(val1>val2) printf( “value 1 is greater than the value 2”); else printf(“value 2 is greater than value 1”); printf( “the above is the result “); getch(); }

Else-if Condition BY ILTAF MEHDI(MCS,MCSE, CCNA) 14 Purpose: - This condition is also called multiple-if condition. in this if condition the first condition is checked if that’s true then execute the entire statement in a specific block if false then next condition will be checked and so on. In case when all the conditions are fails then only else statement will be executed. Condition Block Statement after if-else structure Block Condition True False True

Syntax for else-if Example if (condition) { c statemetn1; } else if (condition) { c statement ; else if (condition) { c statement ; } else { c statement ; } BY ILTAF MEHDI(MCS,MCSE, CCNA)15 if ( expression ) { // code to execute if the expression evaluates to true } else if ( another expression ) { // code to execute if the previous expression failed // and this one evaluates to true } else { // code to execute in all other cases } if ( expression ) { // code to execute if the expression evaluates to true } else if ( another expression ) { // code to execute if the previous expression failed // and this one evaluates to true } else { // code to execute in all other cases }

Write a program that input a student marks then fine out the proper grade according to the Score as >=90—A, 80-89B, 70-79C, 60-69D, Below-60Fail BY ILTAF MEHDI(MCS,MCSE, CCNA) 16 #include void main(void) {clrscr(); int marks; printf(“Enter your marks”); scanf(“%d”, & marks); if(marks >= 90) printf(“ your grade is A “); else if (marks >=80) printf(“your grade is B“ ); else if (marks >=70) printf(“ your grade is C“); else if (marks >=60) printf(“ your grade is D“); else printf(“ you are Fail” ); getch(); }

Create a C-program that compare three values then find out these values are equal or not equal. BY ILTAF MEHDI(MCS,MCSE, CCNA) 17 #include void main(void) { clrscr(); int a,b,c; printf(“Enter first value ”); scanf(“%d”, &a); printf(“ enter second value”); scanf(“%d”, &b); printf(“ enter third value”); scanf(“%d”, &c) ; if(a ==b) { if (a==c) printf(“ all the numbers are equal ”); } else printf(“ the values are different ”); getch(); }

Write a c-program that get a character from the keyboard and then display whether it is vowel or not by using logical operator OR. BY ILTAF MEHDI(MCS,MCSE, CCNA) 18 #include void main(void) { clrscr(); char ch; printf(“Enter a character ”); scanf(“%c”, &ch); if(ch==‘a’ || ch==‘A’ || ch ==‘e’ || ch==‘E’ || ch==‘i’ || ch==‘I’ || ch==‘o’ || ch== ‘O’ || ch == ‘u’ || ch == ‘U’) printf(“ the character you entered is VOWEL”); else printf(“the character you entered is not a VOWEL”); getch(); }

Switch Statement BY ILTAF MEHDI(MCS,MCSE, CCNA) 19 The switch statement is another conditional structure. It is good alternative of nested else- if statement. It can be used easily when there are many choices available and only one should be executed. The nested if becomes very difficult in such situation. switch (expression) { case result1: // execute this if expression results in result1 break; case result2: // execute this if expression results in result2 break; default: // execute this if no break statement // has been encountered } switch (expression) { case result1: // execute this if expression results in result1 break; case result2: // execute this if expression results in result2 break; default: // execute this if no break statement // has been encountered } The break Statement: The break statement is mostly used with switch statement and loops. The break Statement is used to exit from a switch statement or a loop.

Working of the switch statement BY ILTAF MEHDI(MCS,MCSE, CCNA) 20 It works in the following way: switch evaluates expression and checks if it is equivalent to constant1, if it is, it executes group of statements 1 until it finds the break statement. When it finds this break statement the program jumps to the end of the switch selective structure. If expression was not equal to constant1 it will be checked against constant2. If it is equal to this, it will execute group of statements 2 until a break keyword is found, and then will jump to the end of the switch selective structure. Finally, if the value of expression did not match any of the previously specified constants (you can include as many case labels as values you want to check), the program will execute the statements included after the default: label, if it exists (since it is optional).

Syntax of the Switch Statement BY ILTAF MEHDI(MCS,MCSE, CCNA) 21 Enables you to select from multiple choices (called cases). Syntax switch(expression) { case val1 : stat1; stat2; …… break; case val2 : stat1; stat2; …… break; ………………. default: stat1; stat2; …… } c1 c3 c2 Statement statement Statement B B B Next Stat

BY ILTAF MEHDI(MCS,MCSE,CCNA) 22 #include void main (void) { clrscr(); int code; printf("enter any choice from 1 to 3:"); scanf("%d", &code); switch(code) { case 1: printf("\n **I am in Kabul**"); break; case 2: printf("\n **I am in Herat**"); break; case 3: printf("\n **I am in the Mazar Sharif**"); break; default: printf("\n **No case is correct**"); } printf("\n\n **end of the prog**"); getch(); } Write a program in C to show the use of switch statement.

The conditional expression operator OR The ? operator BY ILTAF MEHDI(MCS,MCSE, CCNA) 23 The ? (ternary condition) operator is a more efficient form for expressing simple if statements. It has the following form: expression 1 ? Expression 2 : expression 3 It simply states: if expression 1 then expression 2 else expression 3 For example to assign the maximum of a and b to z: z = (a>b) ? a : b; which is the same as: if (a>b) z = a; else z=b;