Selection Feature of C: Decision making statements: It allow us to take decisions as to which code is to be executed next. Since these statements control.

Slides:



Advertisements
Similar presentations
INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.
Advertisements

Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Control Structures.
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 5 Selection Statements.
Chapter 3: Control Flow S. M. Farhad. Statements and Blocks An expression becomes a statement when it is followed by a semicolon Braces { and } are used.
Program Looping EE2372 Software Design I Dr. Gerardo Rosiles.
Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
If Statements, Try Catch and Validation. The main statement used in C# for making decisions depending on different conditions is called the If statement.
Fundamental of C programming
Chapter 13 Control Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Control Structures Conditional.
Lecture 3: Control Structures - Selection BJ Furman 10SEP2012.
Programming In C++ Spring Semester 2013 Lecture 4 Programming In C++, Lecture 3 By Umer Rana.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
1 Fall 2007ACS-1903 Chapter 3 Decision Structures The if Statement The if-else Statement The if-else-if Statement Nested if Statements Logical Operators.
Control Flow C and Data Structures Baojian Hua
1 Fall 2008ACS-1903 Chapter 3 Decision Structures The if Statement The if-else Statement The if-else-if Statement Nested if Statements Logical Operators.
true (any other value but zero) false (zero) expression Statement 2
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.
1 Arithmetic in C. 2 Type Casting: STOPPED You can change the data type of the variable in an expression by: (data_Type) Variable_Name Ex: int a = 15;
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
Conditionals & boolean operators
Simple Control Structures IF, IF-ELSE statements in C.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
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.
1 2. Program Construction in Java. 2.4 Selection (decisions)
Selection-making Decisions Selection allows you to choose between two or more possible program flow --- it lets you make decisions in your program. Examples.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
CPS120: Introduction to Computer Science Decision Making in Programs.
Control Flow Statements
CSI 3125, Preliminaries, page 1 Control Statements.
Control Statements: Part1  if, if…else, switch 1.
Control Structure  What is control Structure?  Types of Controls  Use the control structure in VBScript.  Example Summery.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
THE DECISIONS CONTROL STRUCTURE! CHAPTER 2. Transfer of control statement: o The statement of computer program are executed one after the other in the.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures Lecture 10.
CHAPTER 4 DECISIONS & LOOPS
Decisions Chapter 4.
C-Language Lecture By B.S.S.Tejesh, S.Neeraja
Programming Fundamentals
C# and the .NET Framework
The nested repetition control structures & Continue Statements
Loop Control Structure.
Lecture 2: Logical Problems with Choices
Decision making If statement.
Visual Basic – Decision Statements
Selection Statements.
SELECTIONS STATEMENTS
Conditionals.
ECE 103 Engineering Programming Chapter 12 More C Statements
Chapter 5: Selection Statement
Program Flow.
Controlling Program Flow
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
There many situations comes in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations.
Decision making and control functions
statement. Another decision statement, , creates branches for multi-
Arduino Programming: “if” statements
break & continue Statements
Presentation transcript:

Selection Feature of C: Decision making statements: It allow us to take decisions as to which code is to be executed next. Since these statements control the flow of execution, they are also known as control statements. In C these statements are: a)If statement b)Switch statement

If statement It can be used in following ways: Simple if statement If-else statement If-else if statement Nested if statement

S IMPLE IF STATEMENT SYNTAX: If (this condition is true) { statement; } Note: If there is only one statement in if block then there is no need of curly braces.

G ENERAL CONDITIONS USED IN FOR LOOP x==y x!=y x<y x>y x<=y x>=y x is equal to y x is not equal to y x is less than y x is greater than y x is less than or equal to y x is greater than or equal to y This expression Is true if

E XAMPLE : #include void main() { int i; i=5; if(i==5) { printf(Inside if \n); printf(This proves i=5); } Output: Inside if This proves i=5

IF - ELSE STATEMENT Syntax: if(condition) { no. of statements } else { no. of statements } start Condi- tion Statem- ent Execute statement NO YE S

E XAMPLE : #include void main() { int x; x=2; if(x<10) printf(x<10 is true \n); else printf(x<10 is false); printf(this is outside if and else); } OUTPUT: x<10 is true This is outside if and else

IF - ELSE IF STATEMENT ( LADDER IF ) syntax: if(condition) { no. of statements; } else if(condition) { no. of statements; } else { no. of statements; }

E XAMPLE : #include void main() { int a; a=1; if(a==4) printf(Im in 4 th yr); else if(a==3) printf(Im in 3 rd yr); else if(a==2) printf(Im in 2 nd yr); else printf(Im in 1 st yr); } OUTPUT: Im in 1 st yr

N ESTED IF STATEMENT A conditional statement inside another conditional statement is called nested conditional statement. They are used to implement multi way decision. syntax: if(condition) { if(condition) { no. of statements; } else { if(condition) no. of statements; }

E XAMPLE : #include void main() { int a; char c; a=20; c=f; if(c==f) { printf(Costumer is female \n); if(a< 16) printf(Her age is less than 16, so shell get a concession of 5%); }

else { printf(Costumer is male\n); if(a>50) printf(His age is more than 50, so hell get a concession of 5%); } OUTPUT: Costumer is female OUTPUT: Costumer is female

NOTE: If in above example, a=10 ; Then, If in above example, a=60; c=m; OUTPUT: Costumer is female Her age is less than 16, so shell get a concession of 5% OUTPUT: Costumer is male His age is more than 50, so hell get a concession of 5%

I F ………. IF.. ELSE ……….. ELSE : Syntax: if(condition) { if(condition) execute the body else execute the body } Else { execute the body }

E XAMPLE : #include void main() { int a,b; a=10; b=20; if(a!=b) { printf(a is not equal to b); if(a>b) printf(a is greater than b);

else printf(a is less than b); } else printf(a is equal to b); } OUTPUT: a is not equal to b a is less than b OUTPUT: a is not equal to b a is less than b

S WITCH S TATEMENT It can be used to select one option among multiple options. switch can replace if-else if statement(ladder if) syntax: switch(variable) { case value1: no. of statements; break; case value2: no. of statements; break; case valueN: no. of statements; break; default: no. of statements; }

There is no need to write break statement at last option. If you do not write break statement with any option the next option will be executed. default can be put any where in the switch block. Example 1: #include void main() { int t; t=2; switch(t) { case 1: printf(t is equal to 1\n); break; case 2: printf(t is equal to 2\n); break; case 3: printf(t is equal to 3\n); break; default: printf(t is not found\n); } OUTPUT: t is equal to 2

E XAMPLE 2: #include void main() { int t; t=2; switch(t) { case 1: printf(t is equal to 1\n); break; case 2: printf(t is equal to 2\n); case 3: printf(t is equal to 3\n); default: printf(t is not found\n); } OUTPUT: t is equal to 2 t is equal to 3 t is not found

E XAMPLE 3: #include void main() { int t; t=5; switch(t) { case 1: printf(t is equal to 1); break; default: printf(t is not found); case 2: printf(t is equal to 2); break; case 3: printf(t is equal to 3); } OUTPUT: t is not found t is equal to 2

?: OPERATOR This is a operator which is capable of controlling the flow of control. syntax: Conditional expression ? Expression 1: expression 2 Conditional expression Expression 1 Expression 2 : ?

Example: 1. ….. int a=5; a>5?printf(a=5):printf(a!=5); …. Output: a=5 2. ….. int a=6; a>5?printf(a=5):printf(a!=5); …. Output: a!=5