Skip to main content

Decision Making in C



DECISION MAKING IN C

When we need to execute a problem in  accordance to the condition, in a  program, or to check any valid argument in a program, we need a favorable method to execute the program, this is where Decision Making steps in programming.

Think of a ROUNDABOUT where, on left lane is your home, on right is your friend's home ,forward is market, and back is hospital, you need to decide on which lane/road you need to move, hence you execute your passage through that lane to reach  your destination, decision making works in same way where you get to decide what to do according to desired condition.

For example in real world: if you need to go to hospital from roundabout  you need to take U-Turn and go thus you make a decision, since you decided to go to hospital, your condition is taking a U-Turn.

For example in programming: if you need to print your age when '1' is pressed and or name when '2' is pressed, the condition is to press either 1 or 2 and outcome is printing your age or name  accordingly.

Decision making is done through decision making statements; there are mainly three decision making statements in C.

* IF STATEMENTS
* IF ELSE STATEMENTS
* IF ELSE-IF ELSE STATEMENTS





THE IF STATEMENTS

The If statements are used to check whether a condition or a set of conditions is true or not. If the condition is true then only the desired actions, commands are executed, otherwise that set of code is skipped.

SYNTAX:

if(condition)
{
     Statements,commands to execute
}

EXAMPLE:
  • To find if a number is even.

We know that a number is even if it is divisible by 2 and gives 0 as remainder when divided by 2

CODE:

if(number%2==0)
{
    printf("THE NUMBER IS EVEN");
}


  • To find if number is divisible by 5 and 7
CODE:

if(number%5==0 && number%7==0)
{
   printf("DIVISIBLE BY BOTH NUMBER");
}

   THE IF-ELSE STATEMENTS

When the requirement is to check one condition or set of conditions and to execute statements according to that & to execute some other statements when if condition does not satisfy then If-Else statements are used.
Conditions are only carried out only in If statements. Else do not hold any condition,but just statements to execute within parenthesis.

SYNTAX:

if(condition)
{
     Statements,commands to execute
}
else
{
     Statements,commands to execute
}

EXAMPLE:
  • To find and print if a number is divisible by 7 or not.

CODE:

if(number%7==0)
{
    printf("THE NUMBER IS DIVISIBLE BY 7");
}
else
{
   printf("NOT DIVISIBLE BY 7");
}

  • To find and print if a number is greater than 10 or not.
CODE:

if(number>10)
{
   printf("GREATER THAN 10");
}
else
{
   printf("LESS THAN 10");
}


   THE IF-ELSE-IF-ELSE STATEMENTS

When the requirement is to check one condition or set of conditions and to execute statements according to that & to check other condition if first one is false and execute statements this method is referred to as If-Else-if-else statements are used.

There can be any number on else-if cases and only one else condition.

SYNTAX:

if(condition)
{
     .....statements
}
else if//1st
{
     ...Statements
}
:
:
:
else if //nth
    ....statements
}
else
..statements
}

EXAMPLE:
  • To find if number is greater than 10 or less than 10 or 10 itself.

CODE:

if(number<10)
{
    printf("THE NUMBER IS LESS THAN 10");
}
else if(number>10)
{
   printf("THE NUMBER IS GREATER THAN 10);
}
else
{
printf("NUMBER IS 10");
}

****

Comments

Post a Comment

Popular posts from this blog

Keywords and Comments in C

KEYWORDS IN C For better blogs do comment your opinion and share the blog What are Keywords? Keywords are the words that convey a special meaning to the compiler. This means the meaning of every particular keyword is already defined to the compiler of the Language. There are total 32 Keywords in C Language and all have their meaning fixed to the compiler. These are the keywords in C. auto double int struct break else long switch case enum register typedef char extern return union const float short unsigned continue for signed void default goto sizeof volatile do if static while The Keywords in Red are the data types they are used to define the data type of a variable. Example: int i; float f; char ch; long l; The Usage of these keywords will be covered as they arrive ...

Data Types In C: Size, Range and Usage.

DATA TYPES IN ‘c’ Data types are keywords which specify the type of data which is used in the programs of C language. Different data types have different storage capacities and different range of data. There are mainly two main categories of data types. PRE-DEFINED DATA TYPES/PRIMARY DATA TYPES INTEGER TYPE DATA TYPES Integer type data type is used to store a whole number or integer number Type Size(bytes) Range int or signed int 2 -32,768 to 32767 unsigned int 2 0 to 65535 short int or signed short int 1 -128 to 127 unsigned short int 1 0 to 255 long int or signed long int 4 -2,147,483,648 to 2,147,483,647 unsigned long int 4 0 to 4,294,967,295 FLOAT TYPE DATA TYPE Float type data types are used to store a real number. Type Size(bytes) Range Float 4 3.4E-38 to 3.4E+38 double 8 1.7E-308 to 1.7E+308 long double 10 3.4E-4932 to 1.1E+4932 CHARACTER DATA TYPE Character type data type is used to store Character type value ...

Variable: Declaration, initialization and Usage

Variable Variables are the names given to some memory location where a value is stored; the variable is corresponding to the data or value input in it. RULES FOR DEFINING A VARIABLE / VARIABLE NAME: · A Variable name should not have name of any KEYWORDS in C. · A Variable name can be alphanumeric but should start with an Alphabet. · A Variable name should not consist of any special symbol other than “_” Underscore Symbol.  · A Variable should be defined before using it. VARIABLE DECLARATION: In this step, the variables are declared, this steps always occurs before using the variables. FORMAT:   EXAMPLE: VARIABLE INITIALIZATION: Providing a Value to any variable is called initialization. INITIALIZATION AT THE TIME OF DECLARATION: The variable can be given its value at the time of declaration. USAGE OF VARIABLE: Here is the example, to show how a vari...