[Android General] mhrsolanki2020:
Chapter 6 (DECISION!! DECISION!! DECISION!!)

Chapter 6​

If you have a calculator which will just ADD the two numbers, then calculator will be pretty useless , wont it?

Hence,There is something in the calculator that decides whether to add or subtract or multiply the two or more numbers provided by the users. This is when we need decision control.

The C language too must be able to perform different sets of actions depending on the circumstances. Actually, this is what makes C popular and very widely usable.

C has three decision making instruction(

Reminder

:a line of code is instruction)

If Statement

If-Else Statement

Switch

The if Statement​

Like most languages, C uses the keyword(if you forgot what is keywords check out the FAQ) if to implement the decision control instruction. The syntax(look FAQ) of if statement looks like this:

if(condition)
{
[B][I]code[/I][/B];
}

where the CODE is the instructions that you want to run.

And if the CONDITION is true then the CODE is executed.

The keyword if tells the compiler that what follows the keyword if in the ( ) "parenthesis" is the condition. If this condition is true then the CODE in the pair of { } "braces" is executed. if the condition is not true then the statement is not executed, in fact the program skips it.

How to evaluate if the condition is true or false?

With the help of relational operators.(will be taught in the second part of chapter 5, when it will be updated for now just remember that the operators used for relating is known as relational operators Eg. == , !=, < , > , <= , >=.

== is to compare the right hand side with the left hand side and != means not equal and others are simple. Will be taught in detail in Chapter 5 part 2 i will link you as soon as that chapter is done by my friend.)

Lets try a program now:

//This is a coment
//A program for demonstration of [B][I]if[/I][/B]

#include<stdio.h>
main()
{
int number;
printf("Please enter a number with less than 100\n");
scanf("%d",&number);
if(number<100)
{
printf("What a good student you are!!");
}
}

NOTE:

Never put semi colon after if statement Like this

if(a<10);
printf("A is less than 10!");

As the program will think that you terminated the if statement after checking the condition , it will think of it this way

if(a<10)
{
}
printf("A is less than 10!!");

So no matter what the value of a is , it will always print A is less than 10.

A flowchart for the above program to make things clear:

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------​

Another Eg:

If your bill is above 10$ in McDonald's then you get 5% discount, if not then no discount.

(try making a flowchart your self , and then see the C program below)

#include<stdio.h>
main()
{
float billamount;
[COLOR="Green"]/*float data type because bill can be in decimal. eg 10.40$*/[/COLOR]
int discount;
[COLOR="Green"]/*discount will remain 10% only*/[/COLOR]
float totalamount;
[COLOR="Green"]/*the amount after deducting the discount from the bill we will store the value in the "totalamount" variable*/[/COLOR]
printf("Enter your bill amount in Dollars\n");
scanf("%f",&billamount);
if(billamount>=10)
{
discount=5; //If billamount will be more than or equal to 10$ then we will give discount of 5%
}
totalamount=[COLOR="RoyalBlue"]billamount[/COLOR]-[COLOR="Red"]billamount*discount/100[/COLOR];
[COLOR="Green"]/*here [COLOR="red"]billamount*discount/100[/COLOR] is to calculate the percentage and then subtract it with the real [COLOR="RoyalBlue"]billamount[/COLOR]*/[/COLOR]
printf("The total amount is : %f",totalamount);
}

----------------------------------------------------------------------------------------------------------------------------------------------------------------------

The If Else Statement

----------------------------------------------------------------------------------------------------------------------------------------------------------------------​

The if -Else statement is the real thing, the if statement was derived from it. Will explain you how and why at the end of the If-Else discussion .

The Syntax(syntax means general form )of the IF-ELSE statement is.

if(CONDITION)
{
CODE If true;
}
else
{
CODE if the CONDITION is false;
}

Lets go through it Step by step:

The CONDITION is evaluated

The CODE IF TRUE will run if the CONDITION is TRUE

if the condition is false then the CODE IF THE CONDITION IS FALSE will run.

Lets see a example

/*A program to add or subtract the numbers*/
#include<stdio.h>
main()
{
int a,b,c; /*a and b for accepting number for user and c for storing the addition or subtraction*/
char op;
printf("Hello, Please enter your operation (Addition or Subtraction only)?\n");
scanf("%d%c%d",&a,&op,&b);
if(op=='+')
{
c=a+b;
}
else
{
c=a-b;
}
printf("The answer is : %d",c);
}

Some examples tested in the program.

Found it complicated? The lets simplify:

scanf("%d%c%d",&a,&op,&b);

The %d will accept the first integer and save it in a

The %c will accept the Operator(+ or - ) and save it in op

The %d will accept the second integer and save it in b

Then the If will check if the op=='+', if it is + then it will add and store the answer in the variable c. It will skip the ELSE .

If op is not + then it will skip the if and the codes in the Else will run, meaning it will skip the codes in the if and then subtract the two integers and then store the value in variable c.

After this the printf functions prints the variable c, which is the answer.

---------------------------------------------------------------------------------------------------------------------------------------------------------

Revising Stuffs

---------------------------------------------------------------------------------------------------------------------------------------------------------​

Let me brush up your basics again:

%d is for integers

%c is for characters

%f is for floats

Defining: To tell the computer you want a variable of int/float/char type is called as defining a variable.

Eg.

char a;

int b;

float c;

Initializing: To give a value to a variable is known as initializing a variable.

Eg.

char a;

a='Z';

int b;

b=2;

float c;

c=3.000000; (decimal is not neccesary while initializing, but when you will print a float variable , you will see the decimals)

CAN ALSO BE INITIALIZED THIS WAY:

char a='Z';

int b=2;

float c=3.000000;

Note: To initialize a char type of variable, you use ' ' single inverted commas, Eg. char a = 'Z';. This is what we have used in the if condition in the above example , if(op=='+');

When we write only the variable name like, (supposing int x = 10;)

printf("%d",x);

We will get 10 as output , that is the value of the varaible.

But when we write a variable name with ampersand(&), (supposing int x is located at 25834 on the memory)

printf("%d",&x);

We will get 25834 as output, as the &(ampersand) is a operator that tells the program to related the variable's address rather than its value.

Now you know why we write

scanf("%d",&a);

Rather than:

scanf("%d",a);

---------------------------------------------------------------------------------------------------------------------------------------------------------​