CSC COMPUTER EDUCATION, M.K.B.NAGAR CHAPTER 3. CSC COMPUTER EDUCATION, M.K.B.NAGAR Session Objectives Explain If, If..else statements Understand Looping.

Slides:



Advertisements
Similar presentations
BBS514 Structured Programming (Yapısal Programlama)1 Selective Structures.
Advertisements

Control Structures Corresponds with Chapters 3 and 4.
For loops For loops are controlled by a counter variable. for( c =init_value;c
Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
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.
Programming Control Flow. Sequential Program S1 S2 S5 S4 S3 int main() { Statement1; Statement2; … StatementN; } Start End.
1 Agenda - Loops while for for & while Nested Loops do-while Misc. & Questions.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 2: Control Flow.
If () else statement, switch statement, while () loop, do…while() loop and for( ; ; ) loop 1.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
C programming: Variables, Expressions part II. Data Types of Arithmetic Expressions Relational Expressions Logical Expressions Multiple Assignments Compound.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Chapter 4 Program Control Statements
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
Reading the data from the input devices and displaying the results on the screen are the two main tasks of any program. To perform these tasks user friendly.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Control Statements Spring Semester 2013Programming and Data Structure1.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
Chapter 13 Control Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Control Structures Conditional.
Control Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Control Structures Conditional making.
Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
1 Flowchart notation and loops Implementation of loops in C –while loops –do-while loops –for loops Auxiliary Statements used inside the loops –break –continue.
Control Statements (Decision Making)
OBJECTIVES  Illustration of the Concept of Control Flow  Types of Control Flow  Knowledge about conditional and repetitive statements.  Make more.
Chapter 5: Structured Programming
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
1 ELEC 206 Chapter 3 Control Structures 5-Step Problem Solving Methodology 1. State the problem clearly. 2. Describe the input and output. 3. Work a.
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.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
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.
Asstt. Prof Mandeep Kaur Computer Dept. TOPIC: Control Statements in C language.
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.
February 7, You have seen how If statement works from last week’s activities. You have learned that If statement is a conditional statement. Today,
1 CSC103: Introduction to Computer and Programming Lecture No 9.
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
The following statements are for y = -1; if ( x ) if ( x>0 ) y = 1; else y = 0; A. y= -1 x0 B. y= 0 x0 C. y= 1 x
 Real numbers representation - Floating Point Notation  First C Program  Variables Declaration  Data Types in C ◦ char, short, int, long, float, double,
Module 6 – Decision Control Statements Objectives  Understands Increment/Decrement operators, Conditional and special operators in C  Understands significance.
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.
Control Structures (Repetition structure) Jump Statements
Control Structures Introduction
Chapter 4 – C Program Control
‘C’ Programming Khalid Jamal.
REPETITION CONTROL STRUCTURE
EKT120 COMPUTER PROGRAMMING
Decisions Chapter 4.
C Program Controls + Flow Structure
Chapter 4 - Program Control
Engineering Problem Solving with C++, Etter/Ingber
Looping.
CS1100 Computational Engineering
Chapter 4 - Program Control
Loops in C.
2.6 The if/else Selection Structure
Chapter 4 - Program Control
Department of Computer Science
Chapter 13 Control Structures
Presentation transcript:

CSC COMPUTER EDUCATION, M.K.B.NAGAR CHAPTER 3

CSC COMPUTER EDUCATION, M.K.B.NAGAR Session Objectives Explain If, If..else statements Understand Looping concepts Explain Switch..Case statement Explain Break,continue Statements Explain the Goto Statement Explain Types of Control Statements

CSC COMPUTER EDUCATION, M.K.B.NAGAR Decision making& Branching Looping Statement Jumping Statement

CSC COMPUTER EDUCATION, M.K.B.NAGAR By default the statements are executed sequentially. However in practice we have situations where we may have to change the order of executions of statements until specified conditions are met. Decision Making &Branching Statement Jumping Statement Looping Statement

CSC COMPUTER EDUCATION, M.K.B.NAGAR Syntax : if(condition) { True Block Statement(s); } Executable statement(s); Test Condition True Block Statements Executable Statements TRUE FALSE

CSC COMPUTER EDUCATION, M.K.B.NAGAR Syntax : if(condition) { True Block Statement(s); } else {False Block statement(s); } Test Condition True Block Statements False Block Statements TRUE FALSE

CSC COMPUTER EDUCATION, M.K.B.NAGAR Syntax : Initialization counter; while(condition) { Body of the Loop; Increment/Decrement counter ; } Start Initialize Test Condition TRUE Body of the loop Increment or Decrement FALSE STOP

CSC COMPUTER EDUCATION, M.K.B.NAGAR

Syntax : Initialization counter; do { Body of the Loop; Increment/Decrement counter ; } while(condition); Start Initialize Test Condition TRUE Body of the loop Increment or Decrement Stop FALSE

CSC COMPUTER EDUCATION, M.K.B.NAGAR

Syntax : For(initialization;condition;increment/decrement) { Body of the Loop; } Start Initialize Test Condition TRUE Body of the loop Increment or Decrement FALSE STOP

CSC COMPUTER EDUCATION, M.K.B.NAGAR Multiple Initialization / Increments The following loop is used for multiple initialization -

CSC COMPUTER EDUCATION, M.K.B.NAGAR The for loop will be termed as a nested for loop when it is written like this : -

CSC COMPUTER EDUCATION, M.K.B.NAGAR Write a “C” Program swapping two numbers #include void main() { int a,b,c; printf("enter two number="); scanf ("%d %d",&a,&b); b=a+b; a=b-a; b=b-a; printf("%d %d",a,b); } Decrement Operator Example #include void main() { int i; i=2; printf("\n The value of i is %d",i); printf("\n The value of i is %d",--i); printf("\n The value of i is %d",i); } Increment operator(++) Example #include void main() { int i=2; printf("\n The value of i is %d",i); printf("\n The value of i is %d",++i); printf("\n The value of i is %d",i); } Write a program to convert farenheit into celsius = (far - 32) *5/9 #include void main() { float faren,celsius; printf("\n Enter the farenheit = "); scanf(" %f", &faren); celsius = (faren -32) *5/9; printf("The Celsius is = %f",celsius); }

CSC COMPUTER EDUCATION, M.K.B.NAGAR Write a program to accept your name,address & city and print it one by one #include void main() { char name[20],add[30],city[20]; printf("\n Enter the name="); scanf("%s",&name); printf("\n Enter the Address="); scanf("%s",&add); printf("\n Enter the City="); scanf("%s",&city); printf("The Name is = %s\n",name); printf("The Address is = %s\n",add); printf("The City is = %s\n",city); } Enter the Name = CSCComputer Enter the Address= Meenambalsalai Enter the city = Chennai The Name is = CSCComputer The Address is= Meenambalsalai The city is = Chennai

CSC COMPUTER EDUCATION, M.K.B.NAGAR How to print a Special characters #include void main() { char c; clrscr(); printf("\n This is how to print a double quote : \"\n"); printf("\n This is how to print a backslash : \\\n"); c='\''; printf("\n c now contains %c\n",c); printf("\n This is how to print a percent - sign : %\n"); c='\007'; /* beep sound code is 7 */ printf("\n c now contains %d\n",c); getch(); }

CSC COMPUTER EDUCATION, M.K.B.NAGAR Write a program to print employee name, basic pay,pf,lic & calculate the net salary #include void main() { char name[20]; int basic,pf,lic,net; printf("\n Enter the name="); scanf("%s",name); printf("\n Enter Basic Bpay ="); scanf("%d",&basic); printf("\n Enter PF ="); scanf("%d",&pf); printf("\n Enter LIC ="); scanf("%d",&lic); net = basic – (pf + lic); printf("Net Salary is = %d ",net); } Write a program to find the greatest among two numbers a,b #include void main() { int a,b,c; printf("\n Enter 2 nos. ="); scanf("%d %d",&a,&b); if (a>b) { printf("A is greatest %d“,a); } else { printf(" B is greatest %d“,b); } getch(); } Enter 2 nos. = A is greatest 100

CSC COMPUTER EDUCATION, M.K.B.NAGAR Write a program to check whether the given number is positive or negative. #include void main() { int a; printf("Enter the number="); scanf("%d",&a); if (a>0) { printf( "Given number is positive %d“.a); } Elseif(a<0) { printf("Given number is negative %d“,a); } Else { printf("Given number is Zero %d“,a); } Write a program to find the greatest of Three numbers #include void main() { int a,b,c; printf("\n Enter 3 nos. ="); scanf("%d %d %d",&a,&b,&c); if ((a>b) && (a>c)) { printf("A is greatest %d“,a); } elseif (b>c) { printf(" B is greatest %d“,b); } else { printf("C is greatest %d“.c); } getch(); } Enter the number = 10 The Given Number is positive Enter 3 nos. = B is greatest

CSC COMPUTER EDUCATION, M.K.B.NAGAR Write a progrm input the given year is LEAP YEAR or Not #include void main() { int year; printf("Enter the year =\n"); scanf("%d",&year); if ((year%4)==0) { printf("\n It is a leap year"); } else { printf("\n It is not a leap year"); } getch(); } Write a progrm input the given Number id Odd or Even Number #include void main() { int no; printf("Enter the Number =\n"); scanf("%d",&no); if ((no%2)==0) { printf("\n The Number is Even Number %d“,no); } else { printf("\n The Number is Odd Number %d“,no); } getch(); } Enter the number = 12 The Number is Even Number 12 Enter the Year 2000 It is a leap year

CSC COMPUTER EDUCATION, M.K.B.NAGAR Write a program to print the FIBONACCI SERIES upto N Numbers #include void main() { int n,i,f1,f2,f3; printf("enter the number = "); scanf("%d",&n); f1=-1; f2=1; for(i=0;i<=n;i++) { f3=f1+f2; printf("%d\n",f3); f1=f2; f2=f3; } Enter the number = 5 PASCAL TRIANGLE #include void main() { int i,j; clrscr(); for(i=1;i<=5;i++) { for(j=1;j<=i;j++) { printf("%d\n",i); } printf("\n"); } Output

CSC COMPUTER EDUCATION, M.K.B.NAGAR PASCAL TRIANGLE #include void main() { int i,j; clrscr(); for(i=1;i<=5;i++) { for(j=1;j<=i;j++) { printf("%d\n",j); } printf("\n"); } Output PASCAL TRIANGLE #include void main() { int i,j,c=1; clrscr(); for(i=1;i<=5;i++) { for(j=1;j<=i;j++) { printf("%d\n",c); c++; } printf("\n"); } Output

CSC COMPUTER EDUCATION, M.K.B.NAGAR PASCAL TRIANGLE to print * #include void main() { int i,j; clrscr(); for(i=1;i<=5;i++) { for(j=1;j<=i;j++) { printf(“*"); } printf("\n"); } Output * * * * * * * * * * * PASCAL TRIANGLE #include void main() { int i,j; clrscr(); for(i=0;i<5;i++) { for(j=i;j>0;j--) { printf("%d\n",j); } printf("\n"); } Output

CSC COMPUTER EDUCATION, M.K.B.NAGAR To print the word in reverse #include #define size 10 void main() { char name[size+1]; int i=1; clrscr(); printf("\n Enter Any String"); scanf("%s",name); printf("\n The Given string is %s\n",name); for(i=0;name[i]!='\0';i++); printf("\n\n The Reversed String is"); for(i=size-1;i>=0;i--) { printf("%c",name[i]); } getch(); } Enter Any String : raja The Given String is raja The Reversed String is ajar

CSC COMPUTER EDUCATION, M.K.B.NAGAR The continue statement causes the next iteration of the enclosing loop to begin. When this statement is encountered, the remaining statements in the body of the loop are skipped and the control is passed on to the re-initialization step. continue;

CSC COMPUTER EDUCATION, M.K.B.NAGAR function The exit() is used to break out of the program. The use of this function causes immediate termination of the program and control rests in the hands of the Operating System.

CSC COMPUTER EDUCATION, M.K.B.NAGAR statement The break statement is used to terminate a case in a switch statement. When the break statement is encountered in a loop, the loop is terminated immediately and control is passed to the statement following the loop. break;

CSC COMPUTER EDUCATION, M.K.B.NAGAR label The goto statement transfers control to any other statement within the same function in a C program. They reduce program reliability and make program difficult to maintain. goto Labelname: Statements; -- Labelname: --- Statements;

CSC COMPUTER EDUCATION, M.K.B.NAGAR Syntax : switch(Expression) { case 1:Statements; break; case 2:Statements; break; case 3:Statements; break; default :Statements; break; } Default is Optional It is useful while writing menu driven programs Break statement transfers the control to the end of switch..case statement.

CSC COMPUTER EDUCATION, M.K.B.NAGAR Write a menu type program to solve arithmetic calculation using switch case stmts. #include void main() { int a,b,choice,tot; printf("\n 1. Addition"); printf("\n 2. Subtraction"); printf("\n 3. Multiplication"); printf("\n 4. Division"); printf("\n Enter the Values="); scanf("%d %d",&a,&b); printf("enter your choice="); scanf("%d",&choice); switch(choice) { case 1: tot = a+b; printf("\n The Addition of 2 nos. % d",tot); break; case 2: tot = a-b; printf("\n The Subtraction of 2 nos. % d",tot); break; case 3: tot = a*b; printf("\n The Multiplication of 2 nos. % d",tot); break; case 4: tot = a/b; printf("\n The Division = % d",tot); break; default: printf("\n invalid"); break; }

CSC COMPUTER EDUCATION, M.K.B.NAGAR Write a program to count the number of vowels and consonants in a string #include void main() { int c=0,v=0; char x; printf("enter any string \n"); do { switch(x=getchar()) { case 'a': case 'e': case 'i': case 'o': case 'u': v++; break; case '\n': break; default: c++; break; } }while (x!='\n'); printf("no. of vowels is %d \n",v); printf("no. of consonants is %d", c); getch(); } Enter Any String : welcome No.of Vowels : 3 No.of Consonants : 4

CSC COMPUTER EDUCATION, M.K.B.NAGAR Write a “C” Program to generate Armstrong No.s from 1 to 1000 #include void main() { int a,b,s,n,i; printf("\n ENter the Limit"); scanf("%d",&n); printf("\n The armstrong Numbers are"); for(i=0;i<=n;i++) { a=i; s=0; while(a>0) { b=a%10; b=b*b*b; s=s+b; a=a/10; } if(i==s) { printf("\t %d",i); } Write a program print the given number as reverse format. #include void main() { long int a,b,c=0,n; printf("\n Enter the Number"); scanf("%ld",&a); while(a>0) { n=a%10; a=a/10; c=b+c*10; } printf("\n The reversed numeral is %ld",c); } Enter the Number : 345 The reversed Numeral is 543

CSC COMPUTER EDUCATION, M.K.B.NAGAR Write a program to convert the number from binary to decimal #include void main() { int i=0,j=0,sum=0; long int n,x; printf("\n Enter Binary Number"); scanf("%ld",&n); if(n!=0) { i=n%10; if(i==0 || i==1) { while(n!= 0) { i=n%10; sum=sum+i*pow(2,j); n=n/10; j++; }}} if(sum==0) printf("\n The no is not a binary number"); else printf("\n The equivalent decimal number is %d",sum); } Enter Binary Number : 101 The Equivalent Decimal No : 5 'break' statement Example 'break' stmt is used to terminate loops or exit from a switch.it can be used within a do-while, for and switch statement #include void main() { int i; printf("The values are\n"); for(i=1;i<=100;i++) { printf("They are %d \n",i); if(i==25) { break; } Print the Numbers from 1…25

CSC COMPUTER EDUCATION, M.K.B.NAGAR Convert the character from lower to upper and vice versa. #include void main() { char x,a; Do { printf("\n Enter any character"); fflush(stdin); scanf("%c",&x); if(x>='A' && x<='Z') printf("\nLower case %c",x+('a' - 'A')); else if(x>='a' && x<='z') printf("\n Upper case is %c",x - ('a' - 'A')); else printf("\n That's not a Letter"); printf("\n Another Data[Y/N]?"); fflush(stdin); scanf("%c",&a); }while(a=='y'); }

CSC COMPUTER EDUCATION, M.K.B.NAGAR F2 Key  Save File F3  Open an existing File F5  Maximize F6  Move to next Program Alt+F9  To compile (Check Errors) Ctrl+F9  Compile and Linking (Execute a Program) Alt+F5  Display output mode Alt+F3  Close screen Quit  Alt+X (come out from Turbo “C”)

CSC COMPUTER EDUCATION, M.K.B.NAGAR Session Summary  The if statement is used to make decisions  The switch statement allows us to make a decision from a number of choices.  The break & continue statements used inside a for, while and do..while loops  The loop does does not terminate when a continue statement is encountered  The switch statement can only for test equality  Break used to terminate or exit from a switch statement  The goto statement is used to transfer the control in a loop or function from one point to any other portion in that program where it encounters a label

CSC COMPUTER EDUCATION, M.K.B.NAGAR EXERCISES 1. Write a program to find the perfect square using if..else statement? 2.Write a program to input the given number is prime or Not? 3. Write a program to perform arithmetic operations using switch statement? 4. Write a program to find the mean and standard deviation of N Numbers? 5. Write a program to find the number of five hundreds,Hundreds, Fifties,Twenties,Tens,Fives,two’s and one’s in a amount given using while loop? 6.Write a program to convert Octal number to a Decimal Number? 7.Write a program to generate N even Numbers and calculate its sum? 8.Write a program to count the number of digits in an integers using while loop? 9.Write a program to calculate the sine series?

CSC COMPUTER EDUCATION, M.K.B.NAGAR EXERCISES 10. Write a program to print the following Outputs using for while and do..while loops?