Input & Output Statement
The two keywords cin and cout in C++ are used for taking inputs from keyboard and printing outputs on standard output device.
1. Standard input stream (cin): C++ cin statement is the instance of the class istream and is used to read input from the standard input device(keyboard).
The extraction operator(>>) is used along with the object cin for reading inputs. The extraction operator extracts the data from the object cin which is entered using the keboard.
Syntax: cin>>variable;
2. Standard output stream (cout): The C++ cout statement is the instance of the ostream class. It is used to produce output on the standard output device which is usually the visual display screen. The data needed to be displayed on the screen is inserted in the standard output stream (cout) using the insertion operator(<<).
Syntax: cout << "Message";
Example
cout << "Enter your age:";
cin >> age;
Flow of Control (If-else)
If statement is a conditional branching statement which checks the condition and reflects two logical values(i.e. True or False). After then perform the work accordingly. if it is evaluate true a group of statement is executed. The simple format of an if statement is as follows:
If Statement : Syntax.
1. If(condition or expression) { Work; } 2. If(condition or expression) { Work1; } else { Work2; }
First Example of if statement
#include<iostream.h>
#include<conio.h>
int main()
{
int n;
cout<<"Enter a number ";
cin>>n;
if(n<10)
{
cout<<"Given number is single digited";
}
return 0;
}
Output
Enter number 7 Given number is single digited
Second Example of if statement
#include<iostream.h>
int main()
{
int n;
cout<<"Enter a number ";
cin>>n;
if(n<10)
{
cout<<"Given number is single digited";
}
else
{
cout<<"Given number is multiple digited";
}
return 0;
}
Output
Enter number 17 Given number is multiple digited
Difference between '/' and '%' operator in C
% is a modulous operator , It give remainder of two numbers on division as result and discard quotient and gives an integer
eg. int a=5%2=1
=> 5/2 => 5-(2*2) => 5-4 = 1, remainder is 1.
/ is a division operator , it give quotient of two numbers on division as result and gives a floating point number.
=> 5/2 => 5-(2*2) =>5-4 = 1 => 1/2= 0.5 . so quotient is 2.5
On integer type casting Result will be 2.
Print Even or Odd of given number.
#include<iostream.h>
int main()
{
int n=0;
cout<<"enter the Number-->";
cin>>n;
if(n%2==0)
{
cout<<n<<" is Even number";
}
else
{
cout<< n<<" is Odd number"<<n;
}
return 0;
}
Output
Enter number 5 5 is odd number or Enter number 6 6 is even number
Nested if...else statement (if...elseif....else Statement)
When you use if within if is called nested if. The nested if...else statement allows you to check for multiple test expressions and execute different codes for more than two conditions.
Syntax of nested if...else statement.
if (testExpression1) { work if testExpression1 is true } else if(testExpression2) { work if testExpression2 is trueis true } else if (testExpression 3) { work if testExpression3 is true } . . else { work if all test expressions are false }
Example: Nested if...else statement
#include <iostream.h>
int main()
{
int n,m;
cout<<"Enter two integers: ";
cin>>n>>m;
//checks if two integers are equal.
if(n == m)
{
cout<<"Result:"<<n<<"="<<m;
}
//checks if n is greater than m.
else if (n > m)
{
cout<<"Result:"<<n<<">"<<m;
}
// if both test expression is false
else
{
cout<<"Result:"<<n<<"<"<<m;
}
return 0;
}
Output
Enter two integers: 22 33 Result: 22 < 33
Switch Statement :
Switch statement is usee for multiple if statement. When the switch statement is executed, the expression in the switch statement is evaluated and the control is transferred directly to the group of statements whose case label value matches the value of the expression. Each group of statement ends with a break statement. The execution of break statement causes immediate exit from the switch statement. If break statement is not present, then the execution will falls trough all the statement in the selected case. If none of the case-label value matches the value of expression, then none of the group within the switch statement will be selected and the control is transferred directly to the statement that follows the switch statement c.
Syntax: Switch case
switch(expression)
{
case constant1:
statements 1;
break;
case constant2:
statements 2;
break;
................................
default:
statements n;
break;
}
}
Example: Switch case
#include <iostream.h>
int main()
{
char op;
float n1,n2;
cout<<"\nSelect Operator";
cin>>op;
cout<<"\nEnter two numbers:";
cin>>n1>>n2;
switch(op)
{
case '+':
cout<<n1<<"+"<<n2<<"="<<n1+n2;
break;
case '-':
cout<<n1<<"-"<<n2"="<<n1-n2;
break;
case '*':
cout<<n1<<"*"<<n2<<"="<<n1*n2;
break;
case '/':
cout<<n1<<"/"<<n2<<"="<<n1/n2;
break;
default:
cout<<"\nError";
break;
}
return 0;
}
}
0 Comments