Making Decisions and Loops in C/C++

Posted on at


 MAKING DECISIONS.
    due to few statments in C/C++ we take decisions which are following


 1.The if, if- else and nested if statements

           if and if-else statments use for to make decision.In c/C++ when we use the if and if-else staments the program,In program frist execute if statment if condition correct the if execute other wise go in if-else statment like
if (a< 0) 

     number = -1;

else 

    if (a == 0)     

     number = 0;  

 else     

       number = 1;
in above example first run if,if condition correct it well set number -1 other wise go in next statment and set number 0 other wise set number is 1

 2.Relational operators.
          There are few pre define relational operators which are below
< : less than

<= : less than or equal to

> : greater than

>= : greater than or equal to

== : equal to

/= : not equal to
All the relational operators must with two operands

  3.Conditional Operators   

         In conditional operators for example we take two expressions ,expression1 and expression 2. if condiotn true expression1 execute other wise expression2 execute.

  4.Multiple Choice statements (switch / case / break / default)
     The switch statements is very similer like if and if-else statments but the we use case and break instead of if and if-else like

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

   char name;
   printf("Which option will you choose:\n");

   printf("x) condition 1 \n");

   printf("y) condition 2 \n");

   scanf("%z", &myinput);
   switch (name)

{

   case 'x':

   printf("Run condition 1\n");

   break;

   case 'y':

{

  printf("Run condition 2\n");

  printf("Please Wait\n");

  break;

}

  default: printf("Invalid choice\n"); break;

}

  return 0;

}


Looping in C/C ++

       Looping in very important because in loop the program execute untell the condition not false few lops are below defined


    1.The for & nested- for statement    

       For loop execute until condition are not false its method are like
       for ( init; condition; increment )

  {   

     statement(s);

and also allow nested for loop like

for ( init; condition; increment )

 for ( init; condition; increment )

{   

statement(s);

}   

statement(s);

}
 The example are  
#include <stdio.h> 

int main ()

{    

 int a, b;      

for(a=2; a<100; a++)

{     

for(b=2; b <= (a/b); b++)     

  if(!(a%b))

break;      

if(b > (a/b))

printf("%d is prime\n", a);

   }    

return 0;

 

2.The while loop. 

       while loop before execution check the condtion if contion ture while eaecute its statment is like
while(condition)

{

   statement(s);

}

The exapmle are
#include <stdio.h>

 int main ()

{    

 int x = 5;
   while( x < 15 ) 

 {     

printf("value of x: %d\n", x);   

  x++; 

 }  

 return 0;

}

  3.The do while loop   

     do while loop also execute untell the condition not false but at least once the body while must run before check the condition.Its method like
do

 statement(s);
}

while( condition );
The example are
#include <stdio.h> 

int main ()

{      

int x = 5;
      do 

 {       

printf("value of x: %d\n", x); 

     x = x + 1; 

 }

while( x < 15 );  

 return 0;

}

4.The break, continue and exit ( ) statements
        It basically use in for,while and do while loop the break statments exit the current program before its normal finish.continue use in for loop to add other element and in while loop use to execute the loop at the top of loop and exit use to exit the program
 


   5.Preprocessor Directive.
              It change the text source and result of the new source without thes directives.It basically force the complier to cmpile some source before complition.The few are below

#include

#define

#undef

#if

#ifdef

#ifndef

#error__FILE____LINE____DATE____TIME____TIMESTAMP__pragma

# macro operator## macro operator



160