Skip to main content

C Programming #4 (Decision Control Instruction)

Decision!
While writing program when its is executed it execute in the sequence which it is written but in serious programming sequence is according to some condition so here is the need to make decision .Which is implemented in C using :

  • The if statement
  • The if-else statement
  • The conditional operator
The if statement:
The general form of if statement looks like this:
if(this condition is true)
execute this statement;
Example:
#include<stdio.h>
int main()
{
int num;
printf("Enter a number less than 10");
scanf("%d",&num);
if(num<10)
printf("What an obedient servant you are!\n");
return 0;
}
The if-else statement:
In this form when the if statement turn to be false then the else statement is executed
Example:
/*Calculation of gross salary*/
#include<stdio.h>
int main()
{
float bs,gs,da,hra;
printf("Enter basic salary");
scanf("%f",&bs);
if(bs<1500)
{
hra=bs*10/100;
da=bs*90/100;
}
else
{
hra=500;
da=bs*98/100;
}
gs=bs+hra+da;
printf("gross salary=Rs%f\n",gs);
return 0;
}
At the start I have used /* this is used to write comments if you want to tell someone what program is this then you can take help of comments and it can also be used in between program.
Nested if-else:
/*A quick demo of nested if-else*/
#include<stdio.h>
int main()
{
int i;
printf("Enter either 1 or 2");
scanf("%d",&i);
if(i==1)
printf("You would go to heaven !\n");
else
{
if(i==2)
printf("Hello was created with you in mind\n");
else
printf("How about mother earth !\n");
}
return 0;
}

Note:You can compile all of these program and try new things if you have any question you are free to ask in comments.

Comments

Popular posts from this blog

Android O (Android Operating system)

There are many kind of changes and improvements in this new version of Android OS. Increased Ability of the mobile:- aptX bluetooth streaming :-   High quality audio codec is supported for streaming to bluetooth Headsets and Speakers Adaptive icons give devs and OEMs easy shape masks, extra effects :-  "an OEM like Samsung or HTC will provide a unifying mask, which will allow developers to integrate almost any existing app icon into the broader user interface with minimal effort, and no extra graphics work on the icon itself." Neighborhood Aware Networking (NAN) mode for WiFi :-  "You can think about NAN as a combination of WiFi Direct and Nearby. Two hypothetical phones with NAN could find each other and connect without any additional apps or configuration, allowing them to share data at high speeds." Picture-in-picture video for all devices and new windowing features :- "The gist is that when watching video on Android O, you'll be able to shr...

Artificial Intelligence

Artificial Intelligence (AI) in Engineering Artificial Intelligence (AI) is the science of using a computer (program) to help think of solutions to solve a particular problem. This is especially very useful in the field of Engineering; due to the need of constantly trying to automate every process for improved efficiency. Every engineering field has benefitted from the use of AI, some more than others. Computer Engineering Computer Engineering is the primary field in which AI is used. Many companies are preferring AI programming over traditional programming as it allows their programs to be long lasting and avoids the constant need to update. Personal Assistant applications like Google Now, Siri and cortana heavily rely on the use of AI for predicting user behavior and voice recognition. Other application include video games, Purchase prediction, Smart cars, Fraud detection, Security services etc. Instrumentation Engineering Instrumentation Engineering primar...

C Programming #6 (Loop Control Instruction)

In C Programming if we want to repeat some statements here we make the use of loops. There are three methods by which we can repeat part of program. Using for statement Using a while statement Using a do-while statement The while loop: It will repeat the program until the condition in while loop become false. Example: /*Calculation of simple interest for 3 sets of p, n and r*/ #include<stdio.h> int main() { int p,n,count; float r,si; count=1; while(count<=3) { printf("\nEnter values of p,n and r"); scanf("%d%d%f",&p,&n,&r); si=p*n*r/100; printf("Simple interest = Rs. %\nf",si); count=count+1; } return 0; } Here this program while loop has the condition to repeat the program 3 times. As in while loop at the end count is increased by 1 every time it execute and when it reaches to 3 control comes out of program and the program terminates. Tips and Traps As I mentioned in m...