if statement
switch statement
Conditional operator statement
goto statement
Size: 186.38 KB
Language: en
Added: May 05, 2018
Slides: 27 pages
Slide Content
if statement switch statement Conditional operator statement goto statement Decision Making & Branching
TYPES OF IF STATEMET The if statement is implemented in different forms depending on the complexity of conditions to be tested: Simple if statement if…..else statement Nested if…..else statement Else if ladder
SIMPLE IF STATEMENT if(test-expression) { sta t emen t - bl o c k ; } Sta t emen t - x; Test expression? Entry F alse T rue
FLOW CHART OF SIMPLE IF Statemet - Block Entry T rue Test expression? False Statement-x Next Statement
W.A.P to display a number if user enters negative number If user enters positive number, that number won't be displayed int main() { int number; printf("Enter an integer: "); scanf("%d", &number); // Test expression is true if number is less than if (number < 0) { printf("You entered %d.\n", number); } printf(“Enter positive number."); return 0; } Output 1 Enter an integer: -2 You entered -2. Enter Positive number
W.A.P to check whether 2 nos are equal
IF – ELSE STATEMENT The if….else statement is an extension of the simple if statement. The general form is: if(test-expression) { True-block statement(s); } else { False-block statement(s); } Statement-x;
FLOWCHART OF IF - ELSE True-Block Statements E n try Test expression? T rue False Statement-x Next Statement False-Block Statements
W.A.P to check whether 2 nos are equal or not
W.A.P whether given num is even or odd
W.A.P to check whether person is eligible for voting or not
Program to check number is divisible by 5 and 11 int main() { int num; /* Reads number from user */ printf("Enter any number: "); scanf("%d", &num); if((num % 5 == 0) && (num % 11 == 0)) { printf("Number is divisible by 5 and 11"); } else { printf("Number is not divisible by 5 and 11"); } return 0; }
Program to find maximum of 2 nos int main() { int num1, num2; printf("Enter two numbers: "); scanf("%d%d", &num1, &num2); if(num1 > num2) { // True part means num1 > num2 printf("%d is maximum", num1); } else { // False part means num1 < num2 printf("%d is maximum", num2); } return 0; }
NESTED IF - ELSE if(test condition 1) { if(test condition 2) { stat e men t - 1; } else { statement-2; } } else { statement-3; } Statement-x;
FLOW CHART OF NESTED IF - ELSE Test condition 1? E n try T rue False Statement-3 Test condition 2? Statement-1 Statement-x Next Statement Statement-2 T rue False
W.A.P to check whether two numbers are >,<,= or != int var1, var2; printf("Input the value of var1:"); scanf("%d", &var1); printf("Input the value of var2:"); scanf("%d",&var2); if (var1 !=var2) { printf("var1 is not equal to var2"); //Below – if-else is nested inside another if block if (var1 >var2) { printf("var1 is greater than var2"); } else { printf("var2 is greater than var1"); } } else { printf("var1 is equal to var2"); }
W.A.P to find maximum of 3 nos int main() { int num1, num2, num3, maximum; printf("Enter three numbers: "); scanf("%d%d%d", &num1, &num2, &num3); if(num1 > num2) { if(num1 > num3) { maximum = num1; } else { maximum = num3; } } else { if(num2 > num3) { maximum = num2; } else { maximum = num3; } } printf("Maximum among all three numbers = %d", maximum); return 0; }
W.A.P to read three values from keyboard and print out the largest of them without using if statement. void main() { int x,y,z; clrscr(); printf("Enter Three Numbers:--\n"); scanf("%d %d %d",&x,&y,&z); ((x>y)&&(x>z))?printf("Largest is x :-- %d",x):((y>x)&&(y>z))?printf("Largest is y :-- %d",y):printf("Largest is z :-- %d",z); } Output:-- Enter Three Numbers:-- 3 4 5 Largest is z :-- 5
FLOWCHART OF ELSE-IF LADDER C o nd iti o n - 2 C o n d iti o n - n Statement-1 Statement-2 Statement-n Default-statement Statement-x T rue T rue T rue False Condition-1 Fa l se False
Program to check whether 2 nos are =,<,>,!= main(){ int var1, var2; printf("Input the value of var1:"); scanf("%d", &var1); printf("Input the value of var2:"); scanf("%d",&var2); if (var1 ==var2) { printf("var1 is equal to var2"); } else if (var1 >var2) { printf("var1 is greater than var2"); } else if (var2 > var1) { printf("var2 is greater than var1"); } else { printf("var1 is not equal to var2"); }}
Write a C program to check whether given character is a digit, lowercase alphabet or an uppercase alphabet. void main() { char ch; clr s cr(); printf("Enter the character:"); scanf("%c",&ch); if((ch>='0')&&(ch<='9')) printf("the number is %c digit",ch); else if((ch>='a')&&(ch<='z')) printf("the character is %c lowercase",ch); else if((ch>='A')&&(ch<='Z')) printf("the character is %c uppercase",ch); else printf("the character is %c other type"); getch(); } Output: Enter the character:7 the number is 7 digit Enter the character:R the character is R uppercase
SWITCH STATEMENT Switch ( expression ) // expression is any integer expression or characters { case value-1 : //values are constants or constant expressions evaluable to an integral constant block-1; break; case value-2 : default : block-2; break; //default is optional..executed when no case value matches default-block; break; } Statement-x;
Implement a Calculator using Switch Case void main() { int a, b, c, d, e, f, ch; printf("Enter the first no. : "); scanf("%d", &a); printf("Enter the second no. : "); scanf("%d", &b); printf("Enter the choice as \n 1.Add\n 2.Sub \n 3.Multiply\n 4.Divide \n"); scanf("%d", &ch); switch(ch) { case 1: c=a+b; printf("%d", c); break; case 2: d=a-b; printf("%d", d); break; case 3: e=a*b; printf("%d", e); break; case 4: f=a/b; printf("%d", f); break; default: printf("Wrong Input"); break; } getch(); }
Program to find number of days in a month using switch case int main() { int month; printf("Enter month number(1-12): "); scanf("%d", &month); switch(month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: printf("31 days"); break; case 4: case 6: case 9: case 11: printf("30 days"); break; case 2: printf("28/29 days"); break; default: printf("Invalid input! Please enter month number between 1-12"); } return 0; }
GOTO STATEMENT If you want to jump some where without checking any condition then goto is used. sta t ements; goto label1; Sta t ements; lable1 : statements; stateme n ts; label1 : statements; goto label1; Statements;
GoTo Example int main() { int age; Vote: printf("you are eligible for voting"); NoVote: printf("you are not eligible to vote"); printf("Enter your age:"); scanf("%d", &age); if(age>=18) goto Vote; else goto NoVote; return 0; } Output: Enter your age: 23 you are eligible for voting Enter your age: 15 you are not eligible to vote