Introduction to IT Systems (DES00001) Module 5- Part 2 PDF

Summary

This document is a lecture on Introduction to IT Systems Module 5- Part 2, covering programming concepts in C. It includes details on decision control (if and if-else statements) as well as switch cases and C loops.

Full Transcript

Introduction to IT Systems (DES00001) Module 5- Part 2 Brainware University 1 Decision Control □ Sometimes situation arises where some decision should be taken based on certain condition □ Ex- if age is >=18, then we can vote □ In C, the f...

Introduction to IT Systems (DES00001) Module 5- Part 2 Brainware University 1 Decision Control □ Sometimes situation arises where some decision should be taken based on certain condition □ Ex- if age is >=18, then we can vote □ In C, the following are used for condition checking ◦ if ◦ if-else ◦ switch-case Brainware University 2 if statement □ Syntax: if (condition) True expression { statements; // if condition is true statement1 } False □ Ex- if (marks >= 40) { printf(“You are passed”); statement2 } Flow chart of if statement Brainware University 3 Program using if int main() { int marks = 50; if (marks >= 40) printf(“You have passed”); } //Here, output will be ‘ You have passed’ because condition is satisfied Brainware University 4 if –else statement Syntax: Example: if (condition) if (marks >= 40) { { statements; // if printf(“ You are passed ”); condition is true } } else else { { printf(“ You are failed ”); statements; // if } condition is false } Brainware University 5 Flow chart of if….else statement True False expression statement1 statement2 Next statement Flow chart of if….else statement Brainware University 6 Program using if-else int main() { int marks = 50; //int marks = 30; if (marks >= 40) printf(“You have passed”); else printf(“You have failed”); } Brainware University 7 Nesting of if... else: Example: Program to find biggest number from three given numbers: if (expression1) # include { int main () if (expression2) { statementA1 int a, b, c, big; printf ("Enter three numbers : "); else scanf ("%d%d%d", &a,&b,&c); statementA2 if (a>b) } { if (a>c) Else big = a; { else if (expression3) big = c; statementB1 } else else { statementB2 if (b>c) } big = b; else big = c; } printf ("Biggest number is %d\n", big); return 0; } Brainware University 8 Program to find whether a year is leap year or not: # include int main () { int year; printf ("Enter year : "); scanf ("%d", &year); if (year % 100 !=0) { if (year % 4 == 0) printf ("Leap year"); else printf ("Not leap year"); } else { if (year % 400 ==0) printf ("Leap year"); else printf ("Not leap year"); Output: } Enter year: 2024 return 0; Leap year } Brainware University 9 Switch Case □ A switch statement tests the value of a variable and compares it with multiple cases □ Once the case match is found, a block of statements associated with that particular case is executed □ The value provided by the user is compared with all the cases inside the switch block until the match is found Brainware University 10 Switch Case □ If a case match is not found, then the default statement is executed □ Syntax : switch (n) { case 1: // code to be executed if n = 1 break; case 2: // code to be executed if n = 2 break; default: // code to be executed if n doesn't match any cases } Brainware University 11 Flow of Control in Switch-Case Brainware University 12 Example: Program to perform arithmetic calculations on integers: # include int main () { char oper; int a, b; printf ("Enter number, operator and another number : "); scanf ("%d %c %d", &a, &oper, &b); switch(oper) { case '+': printf ("Result = %d\n", a+b); break; case '-': printf ("Result = %d\n", a-b); break; case '*': printf ("Result = %d\n", a*b); break; case '/': printf ("Result = %d\n", a/b); break; default: printf ("Enter valid operator\n"); } return 0; } Brainware University 13 Points to be noted in Switch - Case □ The expression provided in the switch should result in a constant value, otherwise it would not be valid □ Duplicate case values are not allowed □ The default statement is optional ◦ Even if the switch case statement do not have a default statement,it would run without any problem Brainware University 14 Points to be noted in Switch - Case □ The break statement is used inside the switch to terminate a statement sequence ◦ When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement □ The break statement is optional ◦ If omitted, execution will continue on into the next case. The flow of control will fall through to subsequent cases until a break is reached Brainware University 15 Loop Generally, statements are executed one after other, but (say) if same task needed for certain numbers of times? Using loop, set of statements are executed repeatedly (iteration) Iteration - number of times the body of loop (statements defined in loop) is executed Brainware University 16 Types of Loop □ There are three types: ◦ while loop ◦ for loop ◦ do-while loop Loops Entry Controlled Exit Controlled do while for while For (initialization; condition; updation) Do While (condition) { { { } while (condition); } } Brainware University 17 while Loop □ A while loop in C program repeatedly execute statements defined in the body of loop as long as given condition is true □ Syntax: while (condition test) { //statement to be executed repeatedly //increment (++) Or decrement (--) } Brainware University 18 Flow chart of while loop False expression True Body of loop Next statement out of loop Brainware University 19 Program using while loop int main() { int i = 1; while(i 0) { int rem = n % 10; sum = (sum) + (rem * rem * rem); n = n / 10; } if (num == sum) { printf("%d is an Armstrong number \n", num); } else { printf("%d is not an Armstrong number", num); } return 0; } Output: Enter Number 371 is an Armstrong number Brainware University 23 C program to reverse digits of a number: #include int main() { int n, r, rev = 0; printf("Enter Number to be reversed : "); scanf("%d", &n); while (n != 0) { r = n % 10; rev = rev * 10 + r; n /= 10; } printf("Number After reversing digits is: %d", rev); return 0; } Output: Enter Number to be reversed : Number After reversing digits is: 321 Brainware University 23 Program to print the sum of digits of any number: # include int main () { int n, sum=0, rem; printf ("Enter a number : "); scanf (“%d”, &n); while (n>0) { rem = n%10; sum+=rem; n/=10; } printf ("Sum of digits = %d\n", sum); return 0; } Output: Enter a number: 123 Sum of digits= 6 Brainware University 24 for Loop □ A for loop do the same as while loop □ Syntax: for (initialization;condition;modification) { body of loop } □ here, modification mean increment or decrement Brainware University 25 Flow chart of for loop False condition True Body of loop Update expression Next statement out of loop Brainware University 26 Program using for loop int main() { int i ; for(i=1 ; i

Use Quizgecko on...
Browser
Browser