Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 BY ILTAF MEHDI(MCS,MCSE, CCNA) 1

2 INSTRUCTOR: ILTAF MEHDI (MCS, MCSE, CCNA, Web Developer) BY ILTAF MEHDI(MCS,MCSE, CCNA) 2 Programming Fundamentals Chapter No: 03

3 BY ILTAF MEHDI(MCS,MCSE, CCNA) 3 Course Contents Chapter NoChapter NamePage No 1. The Turbo C Programming Environment 01 2. C Building Blocks 27 3. Decisions 99 4. Loops 67 5. Functions 135 6. Arrays and Strings 179

4 BY ILTAF MEHDI(MCS,MCSE, CCNA) 4 Chapter No 03 Detail: The if statement The if-else Statement The else-if statement The switch statement The conditional expression operator

5 Conditional Structure BY ILTAF MEHDI(MCS,MCSE, CCNA) 5 The computer can perform arithmetic calculations correctly and rapidly. Besides these capabilities the computer is also capable of quick decision-making and repetitions. A statement or set of statements that executed when a particular condition is true and ignored when the condition is false is called conditional statement. The computer tests a condition and selects one of the two alternatives actions depending on whether the condition is true or false. C/C++ has five decision making statements: if statement if-else statement The else-if statement The switch statement and The conditional expression operator.

6 If Statement BY ILTAF MEHDI(MCS,MCSE, CCNA) 6 The fundamental decision making statement in C/C++ is the if statement. This statement is also called as conditional jump statement or conditional transfer of control statement, because it is mostly used to transfer the control to another statement if and only if a condition is true. If the condition is not fulfilled i.e. if the condition is false, transfer of control does not occur and the program execution moves on to the next statement following the if structure. Syntax: if(condition)or if(condition) Statement; { statement 1; statement 2; } <?php $satisfied = "very"; if ( $satisfied == "very" ) { print "We are pleased that you are happy with our service"; // register customer satisfaction in some way } ?> It is if-statement example used in PHP

7 The if Statement BY ILTAF MEHDI(MCS,MCSE, CCNA) 7 The basic if statement allows your program to execute a single statement, or a block of statements enclosed between braces, if a given condition is true. Example int a = 0, b=1; if (a < b) printf(“Hello”); Condition is true? Statement or Block of Statements Next Statement Yes No

8 C-program of a simple if statement BY ILTAF MEHDI(MCS,MCSE, CCNA) 8 #include void main(void) { clrscr(); int no; printf(“enter any number?”); scanf(“%d”, &no); if(no>10) { printf(“ the value you entered is above 10”); printf(“its ok “); } If (no<10) printf(“below 10 number is entered”); getch(); }

9 Making a decision BY ILTAF MEHDI(MCS,MCSE, CCNA) 9 C-program for the use of a conditional if statement. #include void main(void) { clrscr(); int val; printf(“Enter a value between 50 and 100”); scanf(“%d”, &val); if(val < 50) printf(“Wrong value entered, below 50”); if (val > 100) printf( “Wrong Value entered above 100”); if (val>=50 && val <=100) printf( “You have entered the correct value:%d“,val); getch(); }

10 Q: Get a value from the keyboard then find that the value is positive or negative BY ILTAF MEHDI(MCS,MCSE, CCNA) 10 #include void main(void) { clrscr(); int number; printf ( “Enter a number on your choice ”); scanf(“%d”, &number); if(number < 0) printf ( “the number you entered is negative”); if (number > 0) printf ( “the number you entered is positive”); if (number == 0) printf(“you enter the number equal to 0”); printf ( “You have entered :%d“, number); getch(); }

11 No The if-else Statement BY ILTAF MEHDI(MCS,MCSE, CCNA) 11 The basic if-else statement allows your program to execute a single statement, or a block of statements enclosed between braces, if a given condition is true, and another statement or block of statements if the condition is false. Example int a = 0, b=1; if (a < b) printf(“ a is less than b”); else printf(“ a is greater than or equal to b ”); Condition is true? Statement or Block of Statements for true Next Statement Yes Statement or Block of Statements for false if (expression) { // code to execute if the expression evaluates to true } else { // code to execute in all other cases } if (expression) { // code to execute if the expression evaluates to true } else { // code to execute in all other cases } if-else example in PHP

12 Syntax of if-else Statement BY ILTAF MEHDI(MCS,MCSE, CCNA) 12 It has two syntaxes: i. if(condition)ii. If(condition) statement; { else statement 1; statement; statement 2; } else { statement1; statement2; }

13 Create a program in C that get two values from the keyboard and then show the greatest value. BY ILTAF MEHDI(MCS,MCSE, CCNA) 13 #include void main(void) { clrscr(); int val1, val2; printf(“Enter the first value: ”); scanf(“%d”, &val1); printf( “Enter the second value: ”); scanf(“%d”, &val2); if(val1>val2) printf( “value 1 is greater than the value 2”); else printf(“value 2 is greater than value 1”); printf( “the above is the result “); getch(); }

14 Else-if Condition BY ILTAF MEHDI(MCS,MCSE, CCNA) 14 Purpose: - This condition is also called multiple-if condition. in this if condition the first condition is checked if that’s true then execute the entire statement in a specific block if false then next condition will be checked and so on. In case when all the conditions are fails then only else statement will be executed. Condition Block Statement after if-else structure Block Condition True False True

15 Syntax for else-if Example if (condition) { c statemetn1; } else if (condition) { c statement ; else if (condition) { c statement ; } else { c statement ; } BY ILTAF MEHDI(MCS,MCSE, CCNA)15 if ( expression ) { // code to execute if the expression evaluates to true } else if ( another expression ) { // code to execute if the previous expression failed // and this one evaluates to true } else { // code to execute in all other cases } if ( expression ) { // code to execute if the expression evaluates to true } else if ( another expression ) { // code to execute if the previous expression failed // and this one evaluates to true } else { // code to execute in all other cases }

16 Write a program that input a student marks then fine out the proper grade according to the Score as >=90—A, 80-89B, 70-79C, 60-69D, Below-60Fail BY ILTAF MEHDI(MCS,MCSE, CCNA) 16 #include void main(void) {clrscr(); int marks; printf(“Enter your marks”); scanf(“%d”, & marks); if(marks >= 90) printf(“ your grade is A “); else if (marks >=80) printf(“your grade is B“ ); else if (marks >=70) printf(“ your grade is C“); else if (marks >=60) printf(“ your grade is D“); else printf(“ you are Fail” ); getch(); }

17 Create a C-program that compare three values then find out these values are equal or not equal. BY ILTAF MEHDI(MCS,MCSE, CCNA) 17 #include void main(void) { clrscr(); int a,b,c; printf(“Enter first value ”); scanf(“%d”, &a); printf(“ enter second value”); scanf(“%d”, &b); printf(“ enter third value”); scanf(“%d”, &c) ; if(a ==b) { if (a==c) printf(“ all the numbers are equal ”); } else printf(“ the values are different ”); getch(); }

18 Write a c-program that get a character from the keyboard and then display whether it is vowel or not by using logical operator OR. BY ILTAF MEHDI(MCS,MCSE, CCNA) 18 #include void main(void) { clrscr(); char ch; printf(“Enter a character ”); scanf(“%c”, &ch); if(ch==‘a’ || ch==‘A’ || ch ==‘e’ || ch==‘E’ || ch==‘i’ || ch==‘I’ || ch==‘o’ || ch== ‘O’ || ch == ‘u’ || ch == ‘U’) printf(“ the character you entered is VOWEL”); else printf(“the character you entered is not a VOWEL”); getch(); }

19 Switch Statement BY ILTAF MEHDI(MCS,MCSE, CCNA) 19 The switch statement is another conditional structure. It is good alternative of nested else- if statement. It can be used easily when there are many choices available and only one should be executed. The nested if becomes very difficult in such situation. switch (expression) { case result1: // execute this if expression results in result1 break; case result2: // execute this if expression results in result2 break; default: // execute this if no break statement // has been encountered } switch (expression) { case result1: // execute this if expression results in result1 break; case result2: // execute this if expression results in result2 break; default: // execute this if no break statement // has been encountered } The break Statement: The break statement is mostly used with switch statement and loops. The break Statement is used to exit from a switch statement or a loop.

20 Working of the switch statement BY ILTAF MEHDI(MCS,MCSE, CCNA) 20 It works in the following way: switch evaluates expression and checks if it is equivalent to constant1, if it is, it executes group of statements 1 until it finds the break statement. When it finds this break statement the program jumps to the end of the switch selective structure. If expression was not equal to constant1 it will be checked against constant2. If it is equal to this, it will execute group of statements 2 until a break keyword is found, and then will jump to the end of the switch selective structure. Finally, if the value of expression did not match any of the previously specified constants (you can include as many case labels as values you want to check), the program will execute the statements included after the default: label, if it exists (since it is optional).

21 Syntax of the Switch Statement BY ILTAF MEHDI(MCS,MCSE, CCNA) 21 Enables you to select from multiple choices (called cases). Syntax switch(expression) { case val1 : stat1; stat2; …… break; case val2 : stat1; stat2; …… break; ………………. default: stat1; stat2; …… } c1 c3 c2 Statement statement Statement B B B Next Stat

22 BY ILTAF MEHDI(MCS,MCSE,CCNA) 22 #include void main (void) { clrscr(); int code; printf("enter any choice from 1 to 3:"); scanf("%d", &code); switch(code) { case 1: printf("\n **I am in Kabul**"); break; case 2: printf("\n **I am in Herat**"); break; case 3: printf("\n **I am in the Mazar Sharif**"); break; default: printf("\n **No case is correct**"); } printf("\n\n **end of the prog**"); getch(); } Write a program in C to show the use of switch statement.

23 The conditional expression operator OR The ? operator BY ILTAF MEHDI(MCS,MCSE, CCNA) 23 The ? (ternary condition) operator is a more efficient form for expressing simple if statements. It has the following form: expression 1 ? Expression 2 : expression 3 It simply states: if expression 1 then expression 2 else expression 3 For example to assign the maximum of a and b to z: z = (a>b) ? a : b; which is the same as: if (a>b) z = a; else z=b;


Download ppt "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."

Similar presentations


Ads by Google