fundamental of c programming - ompal singh. conditional statement if- statement: it is the basic...

Post on 01-Apr-2015

219 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Fundamental of C programming

- Ompal Singh

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.

Example: if (a>=b) { printf (“a is larger than b”); }

true

false

a>=b print “A is greater”

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;

Example: if (a>=b) { printf (“a is larger than b”); } else { printf (“b is larger than a”); }

falsetrue

print “a is greater”

print “b is greater”

a >= b

if Only performs an action if the condition is true

if/else Specifies an action to be performed both when

the condition is true and when it is false

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

Else if

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.

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; }

Exampleif( 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");

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) . . .

else

statements to be executed if all test expressions are false.

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);

else

printf("Result: %d>%d",numb2,numb1);

return 0; }

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.

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

Int n;

Printf(“Enter the choice”)

Scanf(“%d”,&n);

switch ( n )

{

case 0: printf (“Sunday\n”) ;

break ;

case 1: printf (“Monday\n”) ;

break ;

case 2: printf (“Tuesday\n”) ;

break ;

case 3: printf (“Wednesday\n”) ;

break ;

case 4: printf (“Thursday\n”) ;

break ;

case 5: printf (“Friday\n”) ;

break ;

case 6: printf (“Saturday\n”) ;

break ;

default: printf (“Error -- invalid day.\n”) ;

break ;

}

#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; printf("%d",c); break;

default: printf("wrong choice"); break; } getch();

}

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

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; }

Without loop

#include<stdio.h>

void main()

{printf("hello\n");

printf("hello\n");

printf("hello\n");

printf("hello\n");

printf("hello\n");

}

#include<stdio.h>void main(){ int i=1; while(i<=5) { printf("hello\n"); i++; } }

With loop

initialization

Condition

Increment

1. #include<stdio.h>

2. Void main()

3. {

4. int i=1;

5. while(i<=10)

6. {

7. printf(“%d”,i);

8. i++; or i=i+1;

9. } } Output:1 2 3 4 5 6 7 8 9 10

1. #include<stdio.h>

2. Void main()

3. {

4. int i=10;

5. while(i>=1)

6. {

7. printf(“%d”,i);

8. i--; or i=i-1;

9. } } Output:10 9 8 7 6 5 4 3 2 1

initialization

Condition

increment

Sum from 1 to 100 (while)

1. int i=1,sum=0;

2. while(i<=100)

3. {

4. sum=sum+i;

5. i++;

6. }

7. Printf(“%d”,sum);

i=1;sum=0;

sum=sum+i;

i<=100

i++;

Yes

No

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(); }

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);

1. #include<stdio.h>

2. Void main()

3. {

4. int i=1;

5. do

6. {

7. printf(“%d”,i);

8. i++; or i=i+1;

9. }while(i<=10); } Output:1 2 3 4 5 6 7 8 9 10

1. #include<stdio.h>

2. Void main()

3. {

4. int i=10;

5. do

6. {

7. printf(“%d”,i);

8. i--; or i=i-1;

9. }while(i>=1); } Output:10 9 8 7 6 5 4 3 2 1

initialization

Condition

increment

Sum from 1 to 100 (do-while)

1. int i=1,sum=0;

2. do{

3. sum=sum+i;

4. i++;

5. } while (i<=100);

6. Printf(“%d”,sum);

i=1;sum=0;

sum=sum+i;

i<=100

i++;

Yes

No

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.

1. #include<stdio.h>2. Void main()3. {4. int i;5. For(i=1;i<=10;i++)6. {7. printf(“%d”,i);8. } } Output:1 2 3 4 5 6 7 8

9 10

1. #include<stdio.h>2. Void main()3. {4. int i;5. For(i=10;i>=1;i--)6. {7. printf(“%d”,i);8. } } Output:10 9 8 7 6 5 4

3 2 1

Sum from 1 to 100 (for)

1. int i,sum=0;

2. for(i=1;i<=100;i++)

3. {

4. sum=sum+i;

5. }

6. Printf(“%d”,sum);

i=1;sum=0;

sum=sum+i;

i<=100

i++;

Yes

No

break Statement

• The break statement can be used in

while, do-while, and for loops to exit from the loop.

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:1 2 3 4Broke out of loop at i = 5.

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.

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: 1 2 3 4 6 7 8 9

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.

Nested Loop

1. int i,j;2. for(i=0;i<5;i++)3. {4. for(j=0;j<i;j++)5. {6. printf("*");7. }8. printf("\n");9. }

***************

top related