Condition Statements.

Slides:



Advertisements
Similar presentations
Fundamental of C programming
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
1 Flow of Control True and False in C Conditional Execution Iteration Nested Code(Nested-ifs, Nested-loops) Jumps.
Programming Control Flow. Sequential Program S1 S2 S5 S4 S3 int main() { Statement1; Statement2; … StatementN; } Start End.
ספטמבר 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.
UNIT II Decision Making And Branching Decision Making And Looping
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
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 6: Control Structures Computer Programming Skills Second Term Department of Computer Science Foundation Year Program Umm Alqura.
1 Flow of Control True and False in C Conditional Execution Iteration Nested Code(Nested-ifs, Nested-loops) Jumps.
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.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
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;
Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.
IT CS 200: C ONDITION Lect. Napat Amphaiphan. T HE ABILITY TO CONTROL THE FLOW OF YOUR PROGRAM, LETTING IT MAKE DECISIONS ON WHAT CODE TO EXECUTE 2.
CSCI 171 Presentation 3. Operators Instructs C to perform some operation Assignment = Mathematical Relational Logical.
Control Flow Statements
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Making Decisions in c. 1.if statement Imagine that you could translate a statement such as “If it is not raining, then I will go swimming” into the C.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
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.
THE DECISIONS CONTROL STRUCTURE! CHAPTER 2. Transfer of control statement: o The statement of computer program are executed one after the other in the.
Lesson #4 Logical Operators and Selection Statements.
Lesson #4 Logical Operators and Selection Statements.
‘C’ Programming Khalid Jamal.
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Flow of Control True and False in C Conditional Execution Iteration
Decisions Chapter 4.
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Week 3 C Program Structures (Selection Structures)
Chapter 4 (Conditional Statements)
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Chapter 4 - Program Control
Week 3 – Selection Structures
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Chapter 2.2 Control Structures (Iteration)
Chapter 4: Making Decisions.
Control Structures.
Chapter 13 Control Structures
Introduction to Programming
Decision Making.
Điều kiện Chương 5.
Relational, Logical, and Equality Operators
SELECTIONS STATEMENTS
Conditionals.
CPS125 Week 5.
Week 3 – Program Control Structure
Control Structures Lecture 6.
Program Flow.
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
ECE 103 Engineering Programming Chapter 22 Selection
CprE 185: Intro to Problem Solving (using C)
CSCE 206 Lab Structured Programming in C
ICS103: Programming in C 5: Repetition and Loop Statements
Presentation transcript:

Condition Statements

Objectives Explain the Selection Construct - If Statement - If – else statement - Multi if statement - Nested if statement Switch statement

Conditional Statement Conditional statements enable us to change the flow of the program A conditional statement evaluates to either a true or a false value Example : To find whether a number is even or odd we proceed as follows : Accept a number Find the remainder by dividing the number by 2 If the remainder is zero, the number is “EVEN” Or if the remainder is not zero the number is “ODD”

Selection Constructs The if statement The switch statement C supports two types of selection statements The if statement The switch statement

The if statement-1 Syntax: If the if expression evaluates to true, the block following the if statement or statements are executed

The if statement-2 Example void main() { int x, y; char a = ‘y’; #include <stdio.h> void main() { int x, y; char a = ‘y’; x = y = 0; if (a == ‘y’) x += 5; printf(“The numbers are %d and \t%d”, x, y); } Program to display the values based on a condition Example

The if – else statement-1 Syntax:

The if – else statement-2 If the if expression evaluates to true, the block following the if statement or statements are executed If the if expression does not evaluate to true then the statements following the else expression take over control The else statement is optional. It is used only if a statement or a sequence of statements are to be executed in case the if expression evaluates to false

The if – else statement -3 Program to display whether a number is Even or Odd #include <stdio.h> void main() { int num , res ; printf(“Enter a number :”); scanf(“%d”,&num); res = num % 2; if (res == 0) printf(“Then number is Even”); else printf(“The number is Odd”); } Example

The if–else–if statement-1 Syntax:

The if–else–if statement-2 The if – else – if statement is also known as the if-else-if ladder or the if-else-if staircase The conditions are evaluated from the top downwards

The if–else–if statement-3 Program to display a message based on a value #include <stdio.h> main() { int x; x = 0; clrscr (); printf(“Enter Choice (1 - 3) : “); scanf(“%d”, &x); if (x == 1) printf (“\nChoice is 1”); else if ( x == 2) printf (“\nChoice is 2”); else if ( x == 3) printf (“\nChoice is 3”); else printf (“\nInvalid Choice “); } Example

Nested if-1 The nested if is an if statement, which is placed within another if or else In C, an else statement always refers to the nearest if statement that is within the same block as the else statement and is not already associated with an if

Nested if-2 Syntax: Note that the inner else is associated with if(exp3) According to ANSI standards, a compiler should support at least 15 levels of nesting

Nested if-3 Example #include <stdio.h> void main () { int x, y; clrscr (); printf (“Enter Choice (1 - 3) : “); scanf (“%d”, &x); if (x == 1) printf(“\nEnter value for y (1 - 5) : “); scanf (“%d”, &y); if (y <= 5) printf(“\nThe value for y is : %d”, y); else printf(“\nThe value of y exceeds 5 “); } printf (“\nChoice entered was not 1”); Example

The switch statement-1 The switch statement is a multi-way decision maker that tests the value of an expression against a list of integers or character constants When a match is found, the statements associated with that constant are executed

The switch statement-2 Syntax:

The switch statement-3 Example Program to check whether the entered lowercase character is vowel or ‘z’ or a consonant #include <stdio.h> main () { char ch; clrscr (); printf (“\nEnter a lower cased alphabet (a - z) : “); scanf(“%c”, &ch); contd……. Example

The switch statement-4 Example if (ch < ‘a’ || ch > ‘z’) printf(“\nCharacter not a lower cased alphabet”); else switch (ch) { case ‘a’ : case ‘e’ : case ‘i’ : case ‘o’ : case ‘u’ : printf(“\nCharacter is a vowel”); break; case ‘z’ : printf (“\nLast Alphabet (z) was entered”); default : printf(“\nCharacter is a consonant”); } Example