Unit 2 The if-else-if LECTURE – 14

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Chapter 3 - Structured Program Development
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 2: Control Flow.
If () else statement, switch statement, while () loop, do…while() loop and for( ; ; ) loop 1.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
JAVA Control Statement.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Conditional Statement
Principles of Programming - NI July Chapter 5: Structured Programming In this chapter you will learn about: Sequential structure Selection structure.
Structured Program Development Outline 2.1Introduction 2.2Algorithms 2.3Pseudo code 2.4Control Structures 2.5The If Selection Structure 2.6The If/Else.
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.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
Chapter 6: Control Structures Computer Programming Skills Second Term Department of Computer Science Foundation Year Program Umm Alqura.
Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.
Control Statements (Decision Making)
C Lecture Notes 1 Structured Program Development.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
OBJECTIVES  Illustration of the Concept of Control Flow  Types of Control Flow  Knowledge about conditional and repetitive statements.  Make more.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Chapter 5: Structured Programming
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
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 5: Structured Programming
Lecture #7 CONTROL STRUCTURE & FLOW CHARTS
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.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
Repetition and Iteration ANSI-C. Repetition We need a control instruction to allows us to execute an statement or a set of statements as many times as.
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.
Asstt. Prof Mandeep Kaur Computer Dept. TOPIC: Control Statements in C language.
Control Flow Statements
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
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.
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.
Unit 3 Control Structure
Control Structures (Repetition structure) Jump Statements
Chapter 5: Structured Programming
‘C’ Programming Khalid Jamal.
ECE Application Programming
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Iteration statement while do-while
JavaScript: Control Statements.
Control Structures.
IF if (condition) { Process… }
Structured Program
Exam 1 Date: Feb. 2nd, 2015 during class time (50 minutes) Coverage
Chapter 3 - Structured Program Development
Chapter 3 – Control Structures
CSC215 Lecture Control Flow.
Chapter 3 - Structured Program Development
Structured Program Development in C
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
Department of Computer Science
CSC215 Lecture Control Flow.
REPETITION Why Repetition?
Presentation transcript:

Unit 2 The if-else-if LECTURE – 14 The if-else-if (nested if) ladder statements:-In this kind of statements numbers of logical condition are checked for executing various statements. Here if any logical condition is true the compiler executes the block followed by if condition otherwise if skips and executes else block. In if-else statements else block is executed by default after failure of condition. In order to execute the else block depending upon certain condition we can add repetitively if statements in else block. This kind of nesting will be unlimited. Syntax of if-else-if statement if(condition is true) { statements; } else if (condition) { statements; } else { statement; } Basic oc Computer & 'C' Programming

Unit 2 The if-else-if LECTURE – 14 Expression:- This Expression Is true if x==y x is equal to y x!=y x is not equal to y x<y x is less than y x>y x is greater than y x<=y x is less than or equal to y x>=y x is greater than or equal to y Basic oc Computer & 'C' Programming

Unit 2 The if-else-if LECTURE – 14 Q. WAP to find the Average of six subjects and display the results as follows Average Results >34 & <50 Third Division >49 & <60 Second Division >60 & <75 First Division >75 & <=100 Distinction If marks in any subject less than 35 Print fail. Basic oc Computer & 'C' Programming

Unit 2 The if-else-if LECTURE – 14 #include<stdio.h> void main() { int sum=0,a,b,c,d,e,f; float avg; printf(“ Enter Marks”); printf(“P C B M E H”); scanf(“%d%d%d%d%d%d”, &a,&b,&c,&d,&e,&f); sum= a+b+c+d+e+f; avg=sum/6; printf(“Total: %d Average: %f”, sum,avg); if(a<35||b<35||c<35|| d<35||e<35||f<35) { printf(“Results: fail”); exit(); } if (avg>34 && avg<50) printf(“Result: Third Division”); else if (avg>49 && avg<60) printf(“Result: Second Division”); else if (avg>60 && avg<75) printf(“Result: First Division”); else if (avg>75 && avg<=100) printf(“Result: Distinction”); } Basic oc Computer & 'C' Programming

Unit 2 The if-else-if LECTURE – 14 Programs using if-else-if :-  Q. WAP to find the smallest out of the three No. int a,b,c, smallest; printf(“Enter Three No”); scanf(“%d%d%d”, &a,&b,&c); if(a<b) { if(a<c) smallest=a; else smallest=c; } Else { if (b<c) smallest=b; smallest=c;} printf(“The Smallest of %d %d %d is %d”, a,b,c);}   Basic oc Computer & 'C' Programming

Unit 2 The if-else-if LECTURE – 14 Q. WAP to find the Largest out of the three No. int x,y,z ; printf(“Enter Three No x,y,z”); scanf(“%d%d%d”, &x,&y,&z); printf(“Largest out of three no is”); if(x>y) { if(x>z) printf(“x=%d”,x); else printf(“z=%d”,z); } Else { if (z>y) printf(“z=%d”,z); printf(“y=%d”,y);}} Basic oc Computer & 'C' Programming

Unit 2 The if-else-if LECTURE – 14 The break statement:- The keyword break allows the programmers to terminate the loop . The break skips from the loop or block in which it is defined. The control automatically goes to the first statements after the loop or block. The break can be associated with all conditional statements. Basic oc Computer & 'C' Programming

Unit 2 The if-else-if LECTURE – 14 The continue statements:- The continue statements is exactly opposite to break. The continue statements is used for continuing next iteration of the loop statements, when it occurs in the loop it does not terminate, but it skips the statements after this statement. It is useful when we want to continue the program without executing any part of the program.   Basic oc Computer & 'C' Programming

Unit 2 The if-else-if LECTURE – 14 The goto statements:- This statements does not requires any condition. This statement passes control anywhere in the program i.e control is transferred to another part of the program without testing any condition. The user has to define goto statement as follows…….. goto label; Where, the label name must start with any character. Basic oc Computer & 'C' Programming

Unit 2 The if-else-if LECTURE – 14 Q.WAP to check whether the entered year is a leap year or not. Ues goto. #include<stdio.h> void main() { int year; printf(“Enter year”); scanf(“%d”, &year); if(year%4= =0) goto leap: else goto noleap: leap: printf(“%d is a leap year”); return; noleap: printf(“%d is not a leap year”); } Basic oc Computer & 'C' Programming

Unit 2 The if-else-if LECTURE – 14 Thanks Basic oc Computer & 'C' Programming