Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fundamental of C programming

Similar presentations


Presentation on theme: "Fundamental of C programming"— Presentation transcript:

1 Fundamental of C programming
- Ompal Singh

2 Conditional Statement
IF- Statement: It is the basic form where the if statement evaluate a test condition and direct program execution depending on the result of that evaluation. Syntax: If (Expression) { Statement 1; Statement 2; } Where a statement may consist of a single statement, a compound statement or nothing as an empty statement. The Expression also referred so as test condition must be enclosed in parenthesis, which cause the expression to be evaluated first, if it evaluate to true (a non zero value), then the statement associated with it will be executed otherwise ignored and the control will pass to the next statement.

3 printf (“a is larger than b”); }
Example: if (a>=b) { printf (“a is larger than b”); } true false a>=b print “A is greater”

4 IF-ELSE Statement: An if statement may also optionally contain a second statement, the ``else clause,'' which is to be executed if the condition is not met. Syntax: If (Expression) { Statement 1; } else statement; stalement;

5 printf (“a is larger than b”); } else
Example: if (a>=b) { printf (“a is larger than b”); } else printf (“b is larger than a”); false true print “a is greater” print “b is greater” a >= b

6 if if/else Only performs an action if the condition is true
Specifies an action to be performed both when the condition is true and when it is false

7 Else if else/if structures
Test for multiple conditions by placing else/if selection structures inside else/if selection structures Once condition is met, rest of statements skipped

8 Syntax: if (test expression)
to be executed if test expression is true; else if(test expression 1) statements to be executed if test expression is true; else if (test expression 2) Statements to be execute if test expression is true; else statements to be executed if all test expressions are false.

9 Example: int main() { int age; printf( "Please enter your age" ); scanf( "%d", &age ); if ( age < 100 ) { printf ("You are pretty young!\n" ); } else if ( age == 100 ) { printf( "You are old\n" ); } else { printf( "You are really old\n" ); } return 0; }

10 Example if( grade >= 90) printf("You got an A!\n");
else if ( grade >= 80 ) printf("You got a B!\n"); else if ( grade >= 70 ) printf("You got a C!\n"); else if ( grade >= 60 ) printf("You got a D!\n"); else printf("You failed!\n");

11 Nested if...else statement (if...elseif....else Statement)
The if...else statement can be used in nested form when a serious decision are involved. Syntax: if (test expression) to be executed if test expression is true; Else if(test expression 1) statements to be executed if test expressions 1 is true; else if (test expression 2) statements to be executed if all test expressions are false.

12 Example: int main(){ int numb1, numb2; printf("Enter two integers to check"); scanf("%d %d",&numb1,&numb2); if(numb1==numb2) printf("Result: %d=%d",numb1,numb2); else if(numb1>numb2) printf("Result: %d>%d",numb1,numb2); printf("Result: %d>%d",numb2,numb1); return 0; }

13 Switch Case This is another form of the multi way decision. It is well structured, but can only be used in certain cases where; Only one variable is tested, all branches must depend on the value of that variable. The variable may be an integral type. (int, long, short or char). Each possible value of the variable can control a single branch. A final, catch all, default branch may optionally be used to trap all unspecified cases.

14 Syntax switch ( integer expression ) { case constant1 : statement(s)
break ; case constant2 : . . . default: }

15 Int n; Printf(“Enter the choice”) Scanf(“%d”,&n); switch ( n ) { case 0: printf (“Sunday\n”) ; break ; case 1: printf (“Monday\n”) ; case 2: printf (“Tuesday\n”) ; case 3: printf (“Wednesday\n”) ; case 4: printf (“Thursday\n”) ; case 5: printf (“Friday\n”) ; case 6: printf (“Saturday\n”) ; default: printf (“Error -- invalid day.\n”) ; }

16 #include<stdio.h>
#include<conio.h> void main() { int a,b,c,ch; clrscr(); printf(" Main Menu \n\n"); printf("1.Add\n"); printf("2.Subtraction\n"); printf("3.Multiply\n\n\n"); printf("Enter the choice\n\n"); scanf("%d",&ch); printf("\n\nenter the number a and b\n"); scanf("%d%d",&a,&b); switch(ch) case 1: c=a+b; printf("%d",c); break; case 2: c=a-b; printf("%d",c); break; case 3: c=a*b; default: printf("wrong choice"); } getch();

17 What is a loop? A loop is a programming structure that contains an executable block of code that repeats so long as a given condition is true/not true. Good programmers include a way to satisfy the condition somewhere inside the executable block. Types of Loop While Do While For

18 While Loop/Pre-Test Loops
In pre-test loops, the executable block of code is execute after the condition test is true. the while loop is a pre-test loop. Syntax: initialization; While(condition) { statement; inc/dec; }

19 Without loop With loop #include<stdio.h> void main()
{ int i=1; while(i<=5) printf("hello\n"); i++; } #include<stdio.h> void main() { printf("hello\n"); } initialization Condition Increment

20 #include<stdio.h> Void main() { int i=10; while(i>=1)
printf(“%d”,i); i--; or i=i-1; } Output: #include<stdio.h> Void main() { int i=1; while(i<=10) printf(“%d”,i); i++; or i=i+1; } Output: initialization Condition increment

21 Sum from 1 to 100 (while) int i=1,sum=0; while(i<=100) { sum=sum+i;
} Printf(“%d”,sum);

22 Program to print a table
#include<stdio.h> #include<conio.h> void main() { int i,n,t; clrscr(); i=1; printf("Enter the number"); scanf("%d",&n); while(i<=10) t=n*i; printf("\n%d",t); i++; } getch();

23 Do while/Post-Test Loops
In post-test loops, the executable block of code is execute before the condition is test. The loop will always execute at least one during the run of a program. The do…while loop is a post-test loop. Syntax: initialization; do{ statement; inc/dec; } While(condition);

24 #include<stdio.h> Void main() { int i=1; do printf(“%d”,i);
i++; or i=i+1; }while(i<=10); } Output: #include<stdio.h> Void main() { int i=10; do printf(“%d”,i); i--; or i=i-1; }while(i>=1); } Output: initialization increment Condition

25 Sum from 1 to 100 (do-while) int i=1,sum=0; do{ sum=sum+i; i++;
Printf(“%d”,sum);

26 For Loop In C programming languages, the for construct is composed of three parts, each divided by a semicolon: Syntax for(initialization; test condition; inc/dec) { statement; } In the first section, the initialization, we initialize an index variable (which is usually being named i, j or k) to its initial value; In the second part, we test whether a variable (usually the same we have just initialized in the first part) satisfies a certain condition: if it does, we enter the loop one more time, otherwise we exit from it; In the third and last part, we update the variable — usually by incrementing or decrementing it by 1.

27 #include<stdio.h>
Void main() { int i; For(i=1;i<=10;i++) printf(“%d”,i); } Output: #include<stdio.h> Void main() { int i; For(i=10;i>=1;i--) printf(“%d”,i); } Output:

28 Sum from 1 to 100 (for) int i,sum=0; for(i=1;i<=100;i++) {
sum=sum+i; } Printf(“%d”,sum);

29 break Statement • The break statement can be used in
while, do-while, and for loops to exit from the loop.

30 Example break in a for Loop
#include <stdio.h> int main ( ) { int i ; for ( i = 1; i < 10; i = i + 1 ) if (i == 5) break ; } printf (“%d “, i) ; printf (“\nBroke out of loop at i = %d.\n”, i) ; return 0 ; OUTPUT: Broke out of loop at i = 5.

31 The continue Statement
• The continue statement can be used in while, do-while, and for loops. • It causes the remaining statements in the body of the loop to be skipped for the current iteration of the loop.

32 Example continue in a for Loop
#include <stdio.h> int main ( ) { int i ; for ( i = 1; i < 10; i = i + 1 ) if (i == 5) continue ; } printf (“%d ”, i) ; printf (“\nDone.\n”) ; return 0 ; OUTPUT:

33 Nested Loops • Loops may be nested (embedded) inside of each other.
• Actually, any control structure (sequence, selection, or repetition) may be nested inside of any other control structure. • It is common to see nested for loops.

34 Nested Loop * int i,j; ** for(i=0;i<5;i++) *** { ****
***** int i,j; for(i=0;i<5;i++) { for(j=0;j<i;j++) printf("*"); } printf("\n");


Download ppt "Fundamental of C programming"

Similar presentations


Ads by Google