second day c language learning


Conditional  Statement :

The if, if...else and nested if...else statement are used to make one-time decisions in C Programming, that is, to execute some code/s and ignore some code/s depending upon the test expression.
if (test expression)
{
       statement/s to be executed if test expression is true;
}

The if statement checks whether the text expression inside parenthesis ( ) is true or not. If the test expression is true, statement/s inside the body of if statement is executed but if test is false, statement/s inside body of if is ignored

#include <stdio.h>
      int main(){
      int num;
      printf("Enter a number. \n");
      scanf("%d",&num);
      if(num<0) {
            printf("Number = %d\n",num);
      }


return 0;
}

if...else statement:
The if...else statement is used if the programmer wants to execute some statement/s when the test expression is true and execute some other statement/s if the test expression is false.
if (test expression) {
     statements to be executed if test expression is true;
}
else {
     statements to be executed if test expression is false;
}

#include <stdio.h>
int main(){
      int num;
      printf("Enter a number.\n");
      scanf("%d",&num);
      if((num%2)==0)
           printf("%d is even.",num);
      else
           printf("%d is odd.",num);
      return 0;
}

H.W

Example of multiple if statements


Nested if...else statement
The nested if...else statement is used when program requires more than one test expression.
if (test expression1){
     statement;
     }
     else if(test expression2) {
          statement;
     }
     else if (test expression 3) {
         statement;
     }
         .
         .
         .
     else {
            statements;
       }

#include <stdio.h>
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 Statement

Decision making are needed when, the program encounters the situation to choose a particular statement among many statements. If a programmer has to choose one block of statement among many alternatives, nested if...else can be used but, this makes programming logic complex. 
switch (n) {
case constant1:
   code/s to be executed if n equals to constant1;
   break;
case constant2:
   code/s to be executed if n equals to constant2;
   break;
   .
   .
   .
default:
   code/s to be executed if n doesn't match to any cases;
}

# include <stdio.h>
int main() {
    char o;
    float num1,num2;
    printf("Select an operator either + or - or * or / \n");
   scanf("%c",&o);
    printf("Enter two operands: ");
4    scanf("%f%f",&num1,&num2);
    switch(o) {
        case '+':
            printf("%.1f + %.1f = %.1f",num1, num2, num1+num2);
            break;
        case '-':
            printf("%.1f - %.1f = %.1f",num1, num2, num1-num2);
            break;
        case '*':
            printf("%.1f * %.1f = %.1f",num1, num2, num1*num2);
            break;
        case '/':
            printf("%.1f / %.1f = %.1f",num1, num2, num1/num2);
            break;
        default:
            printf("Error! operator is not correct");
            break;
    }
    return 0;
}

Looping  Statement :
Loops cause program to execute the certain block of code repeatedly until test condition is false. 
There are 3 types of loops in C programming:
1.   for loop
2.   while loop

 

For Loop:

The initialization statement is executed only once at the beginning of the for loop. Then the test expression is checked by the program. If the test expression is false, for loop is terminated.
for(initialization statement; test expression; update statement) {
       code/s to be executed;
}

#include <stdio.h>
int main(){
    int i;

    for(i=0;i<7;i++)
    {
     printf("The Value of I = %d \n",i);

    }

    return 0;
}

#include <stdio.h>

int main()
{
    int n, c, k;

    printf("Enter number of rows\n");
    scanf("%d",&n);

    for ( c = 1 ; c <= n ; c++ )
    {
        for( k = 1 ; k <= c ; k++ )
            printf("*");

        printf("\n");
    }

    return 0;
}
#include <stdio.h>
int main()
{
    int i,j,rows;
    printf("Enter the number of rows: ");
    scanf("%d",&rows);
    for(i=1;i<=rows;++i)
    {
        for(j=1;j<=i;++j)
        {
           printf("%d ",j);
        }
        printf("\n");
    }
    return 0;
}


while loop

while (test expression) {
     statement/s to be executed. 
}
The while loop checks whether the  test expression is true or not. If it is true, code/s inside the body of while loop is executed,that is, code/s inside the braces { } are executed. Then again the test expression is checked whether test expression is true or not. This process continues until the test expression becomes false.

#include <stdio.h>
int main(){
    int i=1;

    while(i<20)
    {
     printf("The Value of I = %d \n",i);
     i++;

    }

    return 0;
}
#include <stdio.h>
     int main(){
     int number,factorial;
     printf("Enter a number.\n");
     scanf("%d",&number);
     factorial=1;
     while (number>0){
           factorial=factorial*number;
           --number;
}
printf("Factorial=%d",factorial);
return 0;
}

do...while loop

do...while loop is very similar to while loop. Only difference between these two loops is that, in while loops, test expression is checked at first but, in do...while loop code is executed at first then the condition is checked. So, the code are executed at least once in do...while loops.
do {
   some code/s;
}
while (test expression);


#include <stdio.h>
int main()
{
    int i=1;
   do
   {

     printf("The Value of I = %d \n",i);
     i++;

    }
    while(i<20);

    return 0;
}



 break and continue Statement

There are two statements built in C programming, break; and continue; to alter the normal flow of a program. Loops perform a set of repetitive task until text expression becomes false but it is sometimes desirable to skip some statement/s inside loop or terminate the loop immediately without checking the test expression. In such cases, break and continue statements are used. The break;statement is also used in switch statement to exit switch statement.

break Statement

In C programming, break is used in terminating the loop immediately after it is encountered. The break statement is used with conditional if statement.
break;
#include <stdio.h>
xint main(){
    int i;

    for(i=0;i<7;i++)
    {
     printf("The Value of I = %d \n",i);
 if(i==2)
break;
    }

    return 0;
}

Continue Statement

It is sometimes desirable to skip some statements inside the loop. In such cases, continue statements are used.
continue;
#include <stdio.h>
int main ()
{
   int a = 10;
   do
   {
      if( a == 17)
      {
         a = a + 1;
         continue;
      }
      printf("value of a: %d\n", a);
      a++;

   }while( a < 20 );

   return 0;
}

goto Statement

goto statement is used for altering the normal sequence of program execution by transferring control to some other part of the program.
goto label;
.............
.............
.............
label:
statement;
#include <stdio.h>
int main ()
{
   int a = 10;
   LOOP:do
   {
      if( a == 15)
      {
         a = a + 1;
         goto LOOP;
      }
      printf("value of a: %d\n", a);
      a++;
   }while( a < 20 );
   return 0;
}

No comments