Module 6 – Decision Control Statements Objectives  Understands Increment/Decrement operators, Conditional and special operators in C  Understands significance.

Slides:



Advertisements
Similar presentations
CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
Advertisements

Understanding Loops Using C Language Week 15 Mr.Omer Salih.
Chapter 5 Repetition and Loop Statements Instructor: Alkar & Demirer.
C programming an Introduction. Types There are only a few basic data types in C. char a character int an integer, in the range -32,767 to 32,767 long.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
CECS 121 EXAM 1. /* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Fundamentals of C and C++ Programming Control Structures and Functions.
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Principles of Programming - NI July Chapter 5: Structured Programming In this chapter you will learn about: Sequential structure Selection structure.
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Chapter 6: Control Structures Computer Programming Skills Second Term Department of Computer Science Foundation Year Program Umm Alqura.
For Loop Lesson 1 CS1313 Spring for Loop Lesson 1 Outline 1. for Loop Lesson 1 Outline 2.A while Loop That Counts #1 3.A while Loop That Counts.
Control Statements (Decision Making)
CECS 121 Test 1. Functions allow you to group program statements under one name C and C++ are case-sensitive so main(), MAIN(), and Main() are all different.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
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 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
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;
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
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”
CISC105 – General Computer Science Class 4 – 06/14/2006.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
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.
1 CSC103: Introduction to Computer and Programming Lecture No 9.
LOOPS IN ‘C’ PROGRAMMING. V ERY O FTEN, Y OU W ILL W ANT TO D O S OMETHING M ORE T HAN O NCE HA.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
Chapter 4 – C Program Control
Chapter 5: Structured Programming
‘C’ Programming Khalid Jamal.
REPETITION CONTROL STRUCTURE
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
- Standard C Statements
Lecture 7: Repeating a Known Number of Times
Input/output.
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
for Loop 1 Outline for Loop 1 Outline for Loop
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
Arrays, For loop While loop Do while loop
- Additional C Statements
Structured Program
Chapter 4 - Program Control
3 Control Statements:.
Repetition Control Structure
Repetition and Loop Statements
2.6 The if/else Selection Structure
More Loops Topics Counter-Controlled (Definite) Repetition
Dale Roberts, Lecturer IUPUI
More Loops Topics Counter-Controlled (Definite) Repetition
DATA TYPES There are four basic data types associated with variables:
Programming Fundamental
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

Module 6 – Decision Control Statements Objectives  Understands Increment/Decrement operators, Conditional and special operators in C  Understands significance of decision control statements  Knowing more on types of decision control statements  Working on decision control statements

Sample program on Increment and Decrement Operators // C Program to demonstrate the working of Increment/Decrement operators #include int main() { int a = 10, c; c = a++; //post increment printf("c= %d\n",c); c = ++a; //pre increment printf("c= %d\n",c); c = a--; // post decrement printf("c= %d\n",c); c = --a; //pre decrement printf("c= %d\n",c); getch(); return 0; } Output c = 10 c = 12 c = 10

Sample program on Conditional Operators // C Program to demonstrate the working of Conditional operators #include int main() { int a = 10, b; b = (a == 1) ? 20: 30; printf( "Value of b is %d\n", b ); b = (a == 10) ? 20: 30; printf( "Value of b is %d\n", b ); getch(); return 0; } Output Value of b is 30 Value of b is 20

Sample program on Special Operators // C Program to demonstrate the working of Special operators #include int main() { int a = 4; short b; double c; int* ptr; /* example of sizeof operator */ printf("Line 1 - Size of variable a = %d\n", sizeof(a) ); printf("Line 2 - Size of variable b = %d\n", sizeof(b) ); printf("Line 3 - Size of variable c= %d\n", sizeof(c) );

Sample program on Special Operators /* example of & and * operators */ ptr = &a; /* 'ptr' now contains the address of 'a'*/ printf("value of a is %d\n", a); printf("*ptr is %d.\n", *ptr); getch(); return 0; } Output Line 1 - Size of variable a = 4 Line 2 - Size of variable b = 2 Line 3 - Size of variable c= 8 value of a is 4 *ptr is 4

Program to swap numbers using temporary variable //Program to swap two numbers using temporary variable #include int main() { int firstNumber, secondNumber, temporaryVariable; printf("Enter first number: "); scanf("%d", &firstNumber); printf("Enter second number: "); scanf("%d",&secondNumber); // Value of firstNumber is assigned to temporaryVariable temporaryVariable = firstNumber;

Program to swap numbers using temporary variable // Value of secondNumber is assigned to firstNumber firstNumber = secondNumber; // Value of temporaryVariable (which contains the initial value of first Number) is assigned to second number secondNumber = temporaryVariable; printf("\nAfter swapping, firstNumber = %d\n", firstNumber); printf("After swapping, secondNumber = %d", secondNumber); getch(); return 0; } Output Enter first number: 10 Enter second number: 25 After swapping, firstNumber = 25 After swapping, secondNumber = 10

Course Contents  History of Programming Languages  Introduction to C  Basic C Program  Printf and Scanf  Data Types  Tokens and Keywords  Constants  Variables  Operators and Expressions  Decision Control Statements  Loop Control Statements  Case Control Statements  Type Qualifiers  Storage Class Specifier  Array  String  Pointers  Functions  Library Functions  Command Line Arguments  Variable Length Arguments

Decision Control Statements  Decision making is used to specify the order in which statements are executed.  There are three types in Decision Control Statements  if statements  if else statements  nested if statements

if Statements  The if statement evaluates the test expression/condition inside parenthesis.  If test expression/condition is evaluated to true (nonzero), statements inside the body of if is executed.  If test expression/condition is evaluated to false (0), statements inside the body of if is skipped.  Syntax: if (test Expression/condition) { // statements }

if Statements flow chart Now, will see a sample program on if statement

Sample program on if #include int main() { int number; printf("Enter an integer: "); scanf("%d", &number); // Test expression is true if number is less than 0 if (number < 0) { printf("You entered %d.\n", number); } printf("The if statement is easy."); getch(); return 0; } Output Enter an integer: 5 The if statement is easy.

if…. else statements  The if...else statement executes some code if the test expression is true (nonzero) and some other code if the test expression is false (0).  Syntax: if (testExpression) { // codes inside the body of if } else { // codes inside the body of else }

if…. else statements flow chart Now, will see a sample program on if..else statement

Sample program on if…else #include int main() { int number; printf("Enter an integer: "); scanf("%d",&number); // True if remainder is 0 if( number%2 == 0 ) printf("%d is an even integer.",number); else printf("%d is an odd integer.",number); return 0; } Output Enter an integer: 7 7 is an odd integer.

Nested if...else statement (if...elseif....else Statement)  The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities.  The nested if...else statement allows you to check for multiple test expressions and execute different codes for more than two conditions.

Nested if...else statement (if...elseif....else Statement) if (testExpression1) { // statements to be executed if testExpression1 is true } else if(testExpression2) { // statements to be executed if testExpression1 is false and testExpression2 is true }.. else { // statements to be executed if all test expressions are false } Syntax: Will see an example program on Nested if…else statement now…

Sample program on nested if #include int main() { int number1, number2; printf("Enter two integers: "); scanf("%d %d", &number1, &number2); //checks if two integers are equal. if(number1 == number2) { printf("Result: %d = %d",number1,number2); }

Sample program on nested if //checks if number1 is greater than number2. else if (number1 > number2) { printf("Result: %d > %d", number1, number2); } // if both test expression is false else { printf("Result: %d < %d",number1, number2); } getch(); return 0; } Output Enter two integers: Result: 12 < 23

Course Contents  History of Programming Languages  Introduction to C  Basic C Program  Printf and Scanf  Data Types  Tokens and Keywords  Constants  Variables  Operators and Expressions  Decision Control Statements  Loop Control Statements  Case Control Statements  Type Qualifiers  Storage Class Specifier  Array  String  Pointers  Functions  Library Functions  Command Line Arguments  Variable Length Arguments

Loop Control Statements  Loops are used in programming to repeat a specific block until some end condition is met.  There are three types  For loop  While loop  Do-While loop

For Loop Control Statements  Syntax: for (initializationStatement; testExpression; updateStatement) { // codes }

How for loop works  The initialization statement is executed only once.  Then, the test expression is evaluated. If the test expression is false (0), for loop is terminated. But if the test expression is true (nonzero), codes inside the body of for loop is executed and update expression is updated. This process repeats until the test expression is false.  The for loop is commonly used when the number of iterations is known.

For loop flow chart Now, will see a sample program on ‘For Loop’

Sample program on For Loop #include int main() { int n, count, sum = 0; printf("Enter a positive integer: "); scanf("%d", &n); // for loop terminates when n is less than count for(count = 1; count <= n; ++count) { sum += count; } printf("Sum = %d", sum); getch(); return 0; } Output Enter a positive integer: 10 Sum = 55

Thank You