program till the condition is true and the keyword used for loop is "while".
expression here)
the condition is true :
All about programming
Decisions
Decisions are taken by us in
our daily life, so a program without decision is an empty box.
Keyword If
()and else
To add decisions in any C
program we can use the key word “If()”and “else ()”and you can also use if in an if and else in else but with
the C style
We
can also use”&&” for saying compiler “and” and for saying compiler or
we can use “or”
Structure
of if and else statement
The structure of if
statement is as follow:
if (your condition here) if you want to use “&&”
type ((condition1 here) && (and
2 here)) and if you want to use “or” replace && with “or”
{
Statement
1 here ;
}
else
{
Statement
2 here ;
}
Our first decisions program
#include <iostream.h>
main ()
{
int ho;
cout <<"how many
years old are you?" <<"\n" ;
cin >> ho;
if (ho >= 18)
{
cout <<
"you can play foot ball easily";
}
else
{
cout <<
"you can't play football";
}
}
Note:
You
can also use these programs by using operators. If you have any doubt you can
add pretences. Eg after declaring variables you can just type x=4 + y then the
x value will be 4+y’s value (but in the C’s typing style)
Our second program in C
#include <iostream.h>
main()
{
int ho;
cout <<"how many years old are you?"
<<"\n" ;
cin >> ho;
cout << "you are " << ho <<" years
old";
}
Result
This program
will ask you “how many years old are you” and then it will tell you your age.
Explanation
In this program
there are many commands which I have explained above so I will not repeat them.
B/w braces we
have typed “int ho;” this line will declare the variable “ho”.
Then after
cout we have type “<<” to times so in one cout so I m telling you that
you can print as many thing as you want on screen by putting “<<” in one
cout.
Then we type
“\n”, it will tell program to go to next line.
Then we have
typed “cin >> ho;” this will give user a chance to enter his/her data and
after pressing enter computer will store the data in the variable “ho”.
Then after
“cout << “you are”” we have typed “<< ho” without quotes mark this
mean that it will not print ho instead it will print ho’s value.
If you have any doubt about any
calculation put bracket in that place
Note:
Bracket
and comments are free mean they can be used freely.
(In C) In
calculations only parentheses are used not any other brackets
Operators
Assignment operators
(In
C) are use to assign a value to a variable and that operator is “=”. Remember that we can’t put
expressions on left hand side of any assignment operator. X=X+1=X’s old value + 1
Arithmetic operators
(In C) are used for
Arithmetic operations (Remember that
right hand side of = is not effected by the operation) and only the
following operators are use for Arithmetic operations:
Purpose
How to use the operators in C
1) + for addition X= 1 + 2 = 3
2) – for subtraction Y
= 2 – 1 = 1
3) / for dividing Z
= 4 / 2 = 2
Note:
Dividing by 0 is illegal and if division is of integers then
fractional part of that division will truncate. All unknowns are variables
4) * for multiplication J = 3 * 6 = 1 8
5) % for taking the remainder I = 5 % 4 =
1
Relational operators (In C) are used for any
kind or comparison and only following operator are use:
1) < shows less than
2) > shows greater than
3) = = shows equal to
4) != shows not equal to
5) <= shows less than or equal to
6) >= shows greater than or equal to
Not operators (In C) are used for any kind
of ignorance and they are as follow:
1)!True for
showing false (!(true
condition here))
2)!False for
showing true (!(false
condition here))
Precedence
The brackets are solve first, then *, / and % are
solve, then + and – are solve.
Note:
The
expressions are solve left to right (in C)
Keywords
There are certain
keywords like “main ()”and are a followed:
1) Main ( ) 4)
While ( )
2) If ( ) 5)
Do ( )
3) Else ( ) 6)
For( )
Note:
The style
of writing program is important because it tells program about the group of commands
Variables
Variable is the name of location in the memory. We
can use them to store our data in them.
Note:
In c to use variables we, first have to declare them.
It has the following things:
1) Name
2) Type
3) Size
4) Value
Explanation
1)
Name is the label
of location in memory.
2)
Type is the kind
of data which we can store in it.
3)
Size is the size
of the variable on memory
4)
Value is the data
which we store in it.
Type of data
Int
Is used for
declaring that variables which can store normal integer’s data.
Short
Is used for declaring that
variables which can store less integer’s data than int’s variables
use.
Long
Is used for declaring
that variables which can store more integer’s data than int’s variables
use.
Float
Is used for declaring
that variables which can store decimals.
Double
Is used for declaring
that variables which can store more decimals than float command.
Char
Is used for declaring
that variables which can store characters.
How to declare variables
For declaring variables type
“variable’s type then variable’s name
then ;”, in the middle of braces of “main” and for declaring a variable
which’s value is already assigned type “variable’s
type then variable’s name then = your value ;”,
Our
first c program
#include <iostream.h>
main()
{
Cout << “Hello everybody”:
}
Now you have to
compile it
Result
This program will say “Hello everybody”
Explanation
The first line
of our program is preprocessor which help us to get our output.
The second line is the point from which the execution
will start and this line come only and only 1 time in the whole program.
Then we have braces (one
is at beginning and second is at last) which are used to enclose our
program and we must have to type our whole program in it. (Otherwise you can’t compile you program)
Then we have typed “cout
<< “Hello everybody” ;” in b/w of
braces. The “cout” is the door from which we can send the material we want
to.
“<<” are the arrows showing that “Hello everybody” want to get out from the cout door.
In the end we typed “;” which tell the compiler that the command is ended and is
use in every end of C statement.
Note;
To run your program
use command prompt