Control Structure Decision Making S tatements Simple if If else Nested if else Switch Statements (conditional branching) Loops Do- while While statements For Statements Unconditional Branching break statement continue statement goto statement Given By Prof. Aparadh S.Y.
Simple if Syntax Flow Diagram if (expression is true) { action 1; } action 2; action3; Given By Prof. Aparadh S.Y.
Examples of Simple if 1. Print hello when number is 10 Given By Prof. Aparadh S.Y.
Examples of Simple if 1. Print hello when number is 10 Given By Prof. Aparadh S.Y. #include< conio.h > #include< stdio.h > void main() { int num; clrscr (); printf ("Enter value for num“); scanf (“%d”, &num); if(num==10) { printf ("hello)"; getch (); } }
If Else Syntax Flow Chart if (expression is true) { action 1; } Else { action 2; } action3; Given By Prof. Aparadh S.Y.
Examples of If Else Print entered number is positive or negative Given By Prof. Aparadh S.Y.
Examples of If Else Print entered number is positive or negative Given By Prof. Aparadh S.Y. #include< conio.h > #include< stdio.h > void main() { int num; clrscr (); printf (“Enter value for num“); Scanf (“% d”,&num ); if(num>0) { Printf ("num is positive“); } else { Printf ("num is negative“)S; } getch (); }
Examples of If Else Print entered number is even or odd Given By Prof. Aparadh S.Y.
Examples of If Else Print entered number is even or odd Given By Prof. Aparadh S.Y. #include< conio.h > #include< stdio.h > void main() { int num; clrscr (); printf (“Enter value for num“); Scanf (“% d”,&num ); if(num%2==0) { Printf ("num is even“); } else { Printf ("num is odd“)S; } getch (); }
Examples of If Else Print entered year is leap year or not Given By Prof. Aparadh S.Y.
Examples of If Else Print entered year is leap year or not Given By Prof. Aparadh S.Y. #include< conio.h > #include< stdio.h > void main() { int num; clrscr (); printf (“Enter value for year“); Scanf (“% d”,&num ); if(num%4==0) { Printf (“year is leap year“); } else { Printf (“year is not leap year“)S; } getch (); }
Nested If Else Ladder Syntax Flow Chart Given By Prof. Aparadh S.Y.
Home Work of Nested If Else Given By Prof. Aparadh S.Y. Write a program to input three numbers from user. Find maximum between given three numbers.
Home Work of Nested If Else Given By Prof. Aparadh S.Y. Write a program to input three numbers from user. Find maximum between given three numbers . # include< stdio.hh > #include< conio.h > void main() { int num1,num2,num3; clrscr (); printf ("Enter value for num1,num2 and num3“); scanf (“%d%d%d”,&num1, &num2, &num3); if(num1>num2) { if(num1>num3) { printf ("num1 is greater“); } else { printf (" num3 is greater“); } } else { if(num2>num3) { printf ("num2 is greater“); } else { printf (num3 is greater“); } } getch (); } Output: Enter value for num1 ,num2 and num3 4 2 1 Num1 is greater Output: Enter value for num1 ,num2 and num3 1 4 2 Num2 is greater Output: Enter value for num1 ,num2 and num3 1 2 3 Num3 is greater
If Else Ladder Given By Prof. Aparadh S.Y. In C,C++ if- else –if ladder helps user decide from among multiple options . Here if statements are executed from top down As soon as one of the conditions controlling the if is true , the statements associated with that if is executed, and rest of all conditions are bypassed. If none of the conditions is true then the final else statement will be executed
If Else Ladder Syntax Flow Chart if ( condition1) { statement 1 ; } else if ( condition2) { statement 2; } . . else { statement ; } Given By Prof. Aparadh S.Y.
Examples of If Else Print entered number is positive, negative or zero. Given By Prof. Aparadh S.Y.
Examples of If Else ladder Print entered number is positive, negative or zero. Given By Prof. Aparadh S.Y. # include< stdio.hh > #include< conio.h > void main() { int num ; clrscr (); Printf (“Enter num“); Scanf (“%d”, &num ; if( num >0) { Printf (“Positive“); } else if( num <0) { Printf (“Negative“); } else { Printf (“Zero“); } getch (); } Output: Enter num 6 Positive Output: Enter num -3 Negative Output: Enter num Zero
Examples of If Else ladder Write a program to calculate grade of students Given By Prof. Aparadh S.Y.
Write a program to calculate grade of students # include< iostream.h > #include< conio.h > void main() { int grade; clrscr (); Printf ("enter value grade“); Scanf (“%d”, &grade); if(grade>=70 && grade<=100) { Printf (“Distinction“); } else if(grade>=60 && grade<=69) { Printf ("First Class“); } else if(grade>=45 && grade<=59) { Printf (“Second Class“); } Given By Prof. Aparadh S.Y. else if(grade>=40 && grade<=44) { Printf ("Pass Class“); } else if(grade>=0 && grade<40) { Printf ("fail“); } else { Printf ("invalid grade“); Printf ("\ nshould enter grade inbetween 0 to 100“); } getch (); } //Distinction grade>=70 && grade<=100 //First Class grade>=60 && grade <=69 //Second Class grade>=45 && grade <=59 //Pass Class grade>=40 && grade <=44 //Fail grade>=0 && grade <45 //Invalid grade>=100 && grade <=0
Switch Statements Syntax Flow Chart switch (expression) { case 1: action 1; break; Case 2: action 2; break; } . Case n: action n; break; Default: break; } Given By Prof. Aparadh S.Y.
Perform arithmetic operations using switch case Examples Switch Case Given By Prof. Aparadh S.Y.
.Perform arithmetic operations using switch case Examples Switch Case Given By Prof. Aparadh S.Y. #include< stdio.h > #include< conio.h > void main() { int a,b,ch ; clrscr (); Printf ("Enter value for a and b“); Scanf (”% d%d”,&a,&b ); Printf ("Enter choice“); Scanf (“% d”,&ch ) switch( ch ) { case 1: scanf ("Addition is=%d”, a+b ); break; case 4: scanf (“Division is=%d”, a/b); break; default: printf ("Enter valid choice“); break; } getch (); } case 2: scanf (“Subtraction is=%d”, a-b); break; case 3: scanf (“ Multiplication is=%d”, a*b); break;
Do While Loop Syntax Flow Diagram Given By Prof. Aparadh S.Y. do { action 1; } w hile(condition is true); action 2;
Examples of Do While Loop 1. Print 1 to 10 numbers using do while loop Given By Prof. Aparadh S.Y.
Examples of Do While Loop 1. Print 1 to 10 numbers using do while loop Given By Prof. Aparadh S.Y. #include< stdio.h > #include< conio.h > Void main() { int a=1; Clrscr (); do { printf (“\ n%d”,a ); a++; } while(a<=10); getch (); } Output: 1 2 3 4 5 6 7 8 9 10
Examples of Do While Loop 1. Print 10 to 1 numbers using do while loop Given By Prof. Aparadh S.Y.
Examples of Do While Loop 1. Print 10 to 1 numbers using do while loop Given By Prof. Aparadh S.Y. #include< stdio.h > #include< conio.h > Void main() { int a=10; Clrscr (); do { printf (“\ n%d”,a ); a--; } while(a>0); getch (); } Output: 10 9 8 7 6 5 4 3 2 1
Syntax Flow Chart while (condition is true) { action 1; } action 2; While Loop
Examples of While Loop 1. Print 1 to 10 numbers using while loop Given By Prof. Aparadh S.Y. #include< stdio.h > #include< conio.h > Void main() { int a=1; Clrscr (); While(a<=10) { printf (“\ n%d”,a ); a++; } getch (); } Output: 1 2 3 4 5 6 7 8 9 10
Examples of While Loop 1. Print 10 to 1 numbers using while loop Given By Prof. Aparadh S.Y.
Examples of While Loop 1. Print 10 to 1 numbers using while loop Given By Prof. Aparadh S.Y. #include< stdio.h > #include< conio.h > Void main() { int a=10; Clrscr (); While(a>=10) { printf (“\ n%d”,a ); a--; } getch (); } Output: 10 9 8 7 6 5 4 3 2 1
Syntax Flow Chart for (initial value; condition; increment/decrement) { action 1; } action 2; For Loop
Examples of for Loop 1. Print 10 to 1 numbers using for loop Given By Prof. Aparadh S.Y. #include< stdio.h > #include< conio.h > Void main() { int a; Clrscr (); For(a=10;a>0;a--) { printf (“\ n%d”,a ); a--; } getch (); } Output: 10 9 8 7 6 5 4 3 2 1
Syntax Flow Chart for (initial value; condition; increment/decrement) { action 1 ; For((initial value; condition; increment/decrement) { body of inner loop; } action 2; } Nesting For Loop
Examples of nesting for Loop Given By Prof. Aparadh S.Y. Program to print following pattern using nested for loop * * * * * * * * * * * * * * * * * * * * * * * * # include< stdio.h > #include< conio.h > Void main() { int I,j ; Clrscr (); For(j=1;j<=4;j++) { for( i =1;i<=5;i++) { printf (“*”); } printf (“\n”); } getch (); }
Unconditional Branching It is also called as jumping Control is transfer from one point to another without checking condition goto statement break statement Continue
Break Statement The break statement in C programming has the following two usages − When a break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop. It can be used to terminate a case in the switch statement (covered in the next chapter). If you are using nested loops, the break statement will stop the execution of the innermost loop and start executing the next line of code after the block. Syntax The syntax for a break statement in C is as follows − break; Flow Diagram
Break Statement #include < stdio.h > int main { /* local variable definition */ int a = 10; /* while loop execution */ while( a < 20 ) { printf ("value of a: %d\n", a); a++; if( a > 15) { /* terminate the loop using break statement */ break; } } return 0; } When the above code is compiled and executed, it produces the following result value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15
Continue Statement The continue statement in C programming works somewhat like the break statement. Instead of forcing termination, it forces the next iteration of the loop to take place, skipping any code in between. For the for loop, continue statement causes the conditional test and increment portions of the loop to execute. For the while and do...while loops, continue statement causes the program control to pass to the conditional tests. Syntax The syntax for a continue statement in C is as follows − continue; Flow Diagram
Continue Statement #include < stdio.h > int main () { /* local variable definition */ int a = 10; /* do loop execution */ do { if( a == 15) { /* skip the iteration */ a = a + 1; continue; } printf ("value of a: %d\n", a); a++; } while( a < 20 ); return 0; } When the above code is compiled and executed, it produces the following result value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 16 value of a: 17 value of a: 18 value of a: 19
Goto Statement A goto statement in C programming provides an unconditional jump from the ' goto ' to a labeled statement in the same function. NOTE − Use of goto statement is highly discouraged in any programming language because it makes difficult to trace the control flow of a program, making the program hard to understand and hard to modify. Any program that uses a goto can be rewritten to avoid them. Syntax The syntax for a goto statement in C is as follows − goto label; .. . label: statement; Flow Diagram
Continue Statement #include < stdio.h > int main () { /* local variable definition */ int a = 10; /* do loop execution */ LOOP:do { if( a == 15) { /* skip the iteration */ a = a + 1; goto LOOP; } printf ("value of a: %d\n", a); a++; } while( a < 20 ); return 0; } When the above code is compiled and executed, it produces the following result value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 16 value of a: 17 value of a: 18 value of a: 19