Search This Blog

Wednesday 15 December 2021

UNIT 02. BASICS OF PROGRAMMING IN C++ - Chapter From Text Book - Computer Science For Class X

GO TO INDEX

Computer Science For Class X
Unit 02: Basics Of Programming In C++
Chapter From Text Book
































UNIT 04. CONTROL STRUCTURE - Chapter From Text Book - Computer Science For Class X

GO TO INDEX

Computer Science For Class X
Unit 04: Control Structure
Chapter From Text Book




























UNIT 04. CONTROL STRUCTURE - Text Book Exercise And Question Answers - Computer Science For Class X

GO TO INDEX

Computer Science For Class X
Unit 04: Control Structure
Question Answers

By Mrs. Ayesha Arif
Vice Principal
(Jauhar Progressive School)


DETAILED QUESTION –ANSWERS


Q No.1: What are control statements?
Ans: CONTROL STATEMENTS:
Control Statements are the statements that can control the flow of a program. They control the direction of program by repeating any block of code, making a choice or simply transfer the control to any specific block of program. They help the programmers to decide which part of code is executed at certain time.

TYPES OF CONTROL STATEMENTS
  1. C++ has three types of control statements:
  2. Selection/Decision Making Structure
  3. Iteration / Loops
  4. Jump

Q No.2: Write a detailed note on Selection / Decision making structures.
Ans: SELECTION /DECISION MAKING STRUCTURE:
These structures decide whether a certain part of code is executed or not. C++ has three decision making structures.
  1. ‘If ’ statement
  2. ‘if-else’ statement
  3. ‘switch’ statement

1. ‘if’ statement:
  • It is the basic decision statement.
  • The structure contains “if” keyword followed by a conditional expression in parenthesis and then its body of statement(s) is also called if block.
  • If there are more than one statement in “if” block or body we enclose all of them in braces.
  • It tests a particular condition. Whenever a condition is evaluated as true an action is performed.
  • But if condition is false it leaves the statements in “if” block and starts executing statements after “if” block.


 Syntax:

 if (test condition)
 {
 Statement(s);




Nested if statement:
  • An ‘if’ statement which is part of block of another ‘if’ statement is called nested ‘if’.
  • The inner ‘if’ statement will only be tested if the outer ‘if’ is true.

2. “If –else” statement:
  • The structure of if-else contains "if" keyword followed by a conditional expression in parenthesis and then its body of statement(s) then "else" keyword and its body of statement(s).
  • It checks the conditions.
  • When the condition is true the statement in if block performs an indicated action.
  • Otherwise the action is skipped, i.e. in case of false condition, it executes statements in 'else' block.
  • 'if-else' structure performs certain action when the condition is true and some different action when the condition is false, i.e. 'if' block statements or 'else' block statements must execute.


 Syntax:

 if (test condition)
 {
 Statement(s);
 }
 Else
 {
 Statement(s);
 }




'else-if' statement Or Nested 'else-if' statement:
  • This statement test for multiple conditions by placing 'if-else' statement inside 'if–else' statements.
  • When a condition is evaluated as true , the corresponding statements are executed and the rest of the structure is skipped. This structure is referred to as if–else-if-ladder.

3. Switch statement:
The 'switch' statement starts with "switch" keyword followed by a variable or expression in parenthesis, than a block of switch statement in braces.
A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.
The following rules apply to a switch statement:
  • The expression used in a switch statement must have an integer or character constant.
  • We can have any number of case statements within a switch. Each case is followed by the value to be compared to and then a colon.
  • The constant-expression for a case must be the same data type as the variable in the switch, and it must be a constant.
  • When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.
  • When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.
  • Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.
  • A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.


 Syntax:

 switch(expression)
 {
 case constant 1:
 statement(s)
 break;
 case constant 2:
 statement(s)
 break;
:   :   :
 default:
 statement(s)
 }




Q No.3: What do you know about Iteration /loop?
Ans: LOOP:
A loop is used for executing a block of statements repeatedly until a particular condition is satisfied.

ITERATION:
Ans: A loop repeats code until a certain condition is met. Each time the computer runs through a loop, it's referred to as an iteration. Many computer programs and programming languages use iterations to perform specific tasks, solve problems, and present solutions.

TYPES OF LOOPS IN C++:
There are three types of loops in C++
  1. for
  2. while
  3. do-while

1. "for" Loop:
  • It is used to repeat a statement or a block of statements or a sequence of statements for a specified number of times.
  • It is a pre-test loop as condition is tested at the start of loop.
  • It starts with keyword “for” followed by loop expression parenthesis.
  • Loop expression has three parts separated by semicolon.
    (i) Initialization: This is the first part which initializes loop variable. It is executed only once.
    (ii) Test Expression: This is the second part where loop variable is tested. If it is true it enters the loop otherwise transfers control to statement after loop. If it enters in loop then after executing all statements in loop block.
    (iii) Increment Or decrement: It shows decrement or increment in loop variable . It is also called counter controlled loop or definite repetition loop since the number of iterations is known before loop execution.
  • The body of the loop may have one or more statements. If there are more than one statements then they are enclosed in braces.


 Syntax:
 for(initialization; condition testing; increment/decrement)
 {
 statement(s);
 }



2. “while” loop:
  • This loop allows a programmer to specify that an action is to be repeated while condition remains true.
  • It tests the condition at the start of loop before executing the body of loop.
  • It is used when the exact number of loop repetitions before the loop execution begins are not known. Therefore it is also called indefinite repetition loop.
  • It is also a pre test loop.
  • It starts with a keyword “while” followed by a conditional expression like if statement in the parenthesis. Then comes the body of the loop which may have one or more statements in braces.
  • When loop starts, it tests a condition, if it is true it enters in the loop otherwise transfers control to statement after loop.
  • If it enters in loop then after executing all statements in loop block again condition is tested.


 Syntax:
 Initialization;
 while(condition)//condition=true
 {
 statement(s);
 Increment/decrement;
 }



3. “do while” loop:
  • This loop is similar to while loop with one major difference. It tests the condition at the end of the loop body so it is also called a post-test loop.
  • The do-while loop is executed at least once. Only then, the test expression is evaluated.
  • For second time condition is tested.
  • It starts with keyword "do" followed by a body of loop which may have one or more statement in braces. Then "while" keyword andconditional expression in parenthesis.
  • It must be terminated with semi colon.
  • When loop starts it executes statement in block once and then tests the condition, after “while” keyword. If it is true it enters the loop again otherwise transfers control to the next statement after loop. It is also indefinite repetition loop.


 Syntax:
 do
 {
 Statement 1;
 Statement 2;
 Statement 3;
 :
 }
 while (condition);   




Nested Loops:
  • We can use one or more loop inside any another ‘for’, ’while’ or ‘do while’ loop. If a loop exists in the body of another loop then it is called nested loop.
  • The inner nested loop is completely executed every time for each repetition of outer loop.

Q No. 4: Differentiate between:
a) "for", "while" and "do-while" loop structures
b) “if ”, “if-else” and “switch” statements (structures)

Ans: DIFFERENCE BETWEEN FOR,WHILE AND DO WHILE STRUCTURES

DIFFERENCE BETWEEN If , if else and switch structures
S.NO. “if ” “if-else” loop “switch” loop
1. "If" statement is used to select among two alternatives. "If-else" statement is used to select among two alternatives  The "switch" statement is used to select among multiple alternatives.
2. It can have values based on constraints. It can have values based on constraints. Switch can have values based on user choice.
3. It implements Linear search. It implements Linear search. Switch implements Binary search.
4. "if" statement checks a condition using rational and other operators. if-else" statement checks a condition using rational and other operators. "switch" statement checks 'switch' variable value equal to constant that follows case statement.
5. If the condition is true the statement in if block is executed. If condition is false, it leaves the statement in "if" block and start executing statements after "if" block.  If the condition is true the statement in if block is executed and in case of false condition it executes statement in 'else' block. If the switch expression matches any value of constant the control is transferred to that case and all statements after colon are executed. If it doesn’t match any of the case constants control goes to keyword default.


Q No.5: What is Jump statement?
Ans: JUMP STATEMENT:
Jump statements change execution of program from its normal sequence. There are five jump statements used in C++.
  1. break
  2. continue
  3. return
  4. exit
  5. goto

i. BREAK:
  • It can be used in both switch and loop statements.
  • A break causes the switch or loop statements to terminate the moment it is executed.
  • When a break statement is encountered , it terminates the block and gets the control out of the switch or loop.
  • A break causes the innermost enclosing loop or switch to be exited immediately.

ii. CONTINUE:
  • It can appear in only loop (for, while ,do) statements.
  • This statement is used to skip the remaining statements of its body in loop that appear after continue.
  • It immediately transfer control to the top of the loop i.e. first statement.
  • When a continue statement is encountered , it gets the control to the next iteration of the loop.
  • A continue inside a loop nested within a switch causes the next loop iteration.

iii. GOTO:
  • This is used to jump or transfer control unconditionally from "goto" to a labelled statement in the same function.
  • A labelled statement is any identifier followed by a colon.
  • It is not advised to use "goto" statements in programs.
e.g.

iv. RETURN:
  • It terminates the execution of a function and transfers program control to the statement just after the function call statement in the calling function (or to the operating system if we transfer control to the main function).
  • It can also return a value from the current function, if the return type is not "void".


 Syntax:
 return [expression/value];

The value of the expression clause returned to the calling function.

v. EXIT:
  • The exit() is used to terminate C++ program and closes program whenever executed.
  • It is defined under “stdlib.h” header file.


 Syntax:
 void exit (int);


Source: Special Thanks To Sir Syed Arif Ali

B. RESPOND THE FOLLOWING:

Answer the following questions:
Q.1: What is the Purpose of Default Statement in C++?
Ans: Purpose Of Default Statement:
The purpose of the default in the switch case in C++ is to provide an alternative option, in case the conditions specified to trigger the execution of code within the other options do not occur so that the program will not crash.
OR
A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.

Q.2. Can we use "while" loop in place of "for" loop, if yes, then how?
Ans: Yes, we can use while loop in place of for loop in C++. Initialization of loop can be done outside of while body, testing condition can be placed in while condition and increment or decrement usually done within the while body. Here is an example of for and while loop.


Q.3: What Is the main difference between "while" and "do while" loops?
Ans: Difference Between "While loop" and "Do While Loop":
S.NO. While Loop Do While Loop
1. It is pre-test loop because condition is tested at the start of loop. It is post-test loop because condition is tested at the end of loop which executes loop at least once.
2. It is known as entry controlled loop. It is known as exit controlled loop.
3.  If the condition is not true first time then control will never enter in a loop. Even if the condition is not true for the first time the control will enter in a loop.
4. There is no semicolon; after the condition in the syntax of the while loop. There is a semicolon (;) after the condition in the syntax of the do while loop.
5. Initialization and updating is not the part of the syntax. Initialization and updating is not the part of the syntax.

OR
Ans: Difference Between "While loop" and "Do While Loop":
S.NO. While Loop Do While Loop
1. The controlling condition is declared first in this loop. The controlling condition is declared at last in this loop.
2. If the condition is false it will not run. If the condition is false it will run once..
3. Semicolon is not used in this loop Semicolon is used at last in this loop
4. While loop is entry controlled loop. Do while loop is exit controlled loop.
5. | Such that


Q.4: Write the function of "for loop"?
Ans: Function Of for Loop:
A for loop is a repetition or iteration control structure that repeats a statement or block of statements for a specified number of times. The for-loop statement includes the initialization of the counter, the condition, and the increment. The for loop is commonly used when the number of iterations is known.

Q.5. Why we make block of statements using braces?
Ans: We make block of statements by using braces around all statements, even single statements, when they are part of a control structure, such as an if-else or for statement. This makes it easier to add statements without accidentally introducing bugs due to forgetting to add braces.

Q.6. Which data type variables can be used in "switch" statement?
Ans: Only "int" and "char" data types are used in switch statement.

Q.7: What is the purpose of Jump statements?
Ans: Jump Statements:
Jump statements change the normal execution of a program from its normal sequence, and jump over the specific part of program.
Following are the jump statements in C++
  1. Break
  2. Continue
  3. Goto
  4. Exit () oR Return

Q.8: What is the Purpose of the Following?
1) Else if
2) Switch
3) Goto
4) Exit Or Return

Ans:
1) else-if:
The else if statement is used when we have multiple conditions in the if-else structure. We can say if one condition is not met then we have an else-if condition to check other conditions.
Or
Nested if-else statements test for multiple conditions by placing if-else statements inside if-else statements. When a condition is evaluated as true, the corresponding statements are executed and rest of the structure is skipped. This structure is also referred as the else-if ladder.

2) Switch:
The switch statement in C tests the value of a variable and compares it with multiple cases. Once the case match is found, a block of statements associated with that particular case is executed.
OR
Switch statement is a control statement that allows to select only one choice among the many given choices. The expression in switch evaluates to return an integer or character value, which is then compared to the values present in different cases. It executes that block of codes which matches the case value. If there is no match, then default block is executed (if present).

3) Go to:
A go to statement jumps or transfers control unconditionally from the “go to” to a labeled statement in the same function. A labeled statement is an identifier with a colon(:).
OR
In C++ programming, the goto statement is used for altering the normal sequence of program execution by transferring control to some other parts of the program.

4) Exit:
The exit() is used to terminate a C++ program and closes the program whenever executed.
OR
return: The exit function, declared in <stdlib .h>, terminates a C++ program. The value supplied as an argument to exit is returned to the operating system as the program's return code or exit code.

Q.3: Match the column?
S.NO. A S.NO. B S.NO.
(a) if(i) relational eperator.(a) (vi)
(b) loop(ii) break(b) (v)
(c) Conditional expression(iii) Switch(c) (i)
(d) Loops and switch(iv) operator(d) (ii)
(e) do(v) iteration(e) (vii)
(f) >>(vi) else(f) (iv)
(g) case(vii) while(g) (iii)



Monday 13 December 2021

UNIT 04. CONTROL STRUCTURE - MCQs - Computer Science For Class X

GO TO INDEX

Computer Science For Class X
Unit 04: Control Structure
Multiple Choice Questions And Fill In The Blanks

By Mrs. Ayesha Arif
Vice Principal
(Jauhar Progressive School)


Multiple Choice Questions (MCQs)
Encircle the correct answer:
1. Loop within a loop is called ________ loop.
a. Inner
b. outer
c. enclosed
d. nested ✓

2. Case and _______ are also part of switch statement.
a. have
b. default ✓
c. for
d. if

3. “For” loop expression has ________ parts.
a. one
b. two
c. three ✓
d. four

4. exit( ) function is used to ___________.
a. close function
b. close loop
c. close program ✓
d. close switch

5. “continue ” statement takes control to the __________ .
a. top of loop ✓
b. end of loop
c. top of function
d. end of function

6. In “goto” statement label is followed by __________ character.
a. colon (:) ✓
b. semi colon (;)
c. single quote (‘)
d. double quote (“)

7. To send value to calling function we use ____________ statement.
a. throw
b. return ✓
c. send
d. back

8. “break” statement is used with _________ .
a. if
b. switch ✓
c. for
d. while

9. Using “else” is _________ with “if” statement.
a. prohibited
b. advised
c. compulsory
d. optional ✓

10. “if” and loop expressions use ___________ operators to test condition.
a. arithmetic
b. relational ✓
c. insertion
d. bitwise

11. C++ has _________ types of control statements.
a. one
b. two
c. three ✓
d. four

12. C++ has __________ decision making structures.
a. one
b. two
c. three ✓
d. four

13. __________ allows us to execute a statement several number of times.
a. if-else
b. loop ✓
c. switch
d. insertion

14. do while loop is similar to _________ loop.
a. if-else
b. while ✓
c. for
d. nested

15. If a loop exist in the body of another loop then it is called _____ .
a. if-else
b. while
c. for
d. nested ✓

16. A _________ statement causes the loop to skip the remaining statements of its body.
a. continue ✓
b. goto
c. return
d. nested

17. A _________ statement jumps or transfers control unconditionally from the goto to a labelled statement.
a. if-else
b. goto ✓
c. return
d. continue

18. A __________ program is the set of instructions in sequential form.
a. nested
b. computer ✓
c. control
d. return

19. ________ statement is the basic decision statement.
a. if ✓
b. break
c. if-else
d. continue

20. ________ statement change execution of program from its normal sequence.
a. if
b. jump ✓
c. for
d. goto

Fill In The Blanks:
1. A computer program is the set of instructions in sequential form.
2. Control statement are used to control the direction of program.
3. C++ has three types of control statements: Selection/Decision Making Structure, Iteration / Loops and Jump.
4. C++ has three decision making structures; 'if' statement, 'if-else statement and 'switch' statement.
5. "if" statement and "if-else" statements checks a condition.
6. "if" statement is the basic decision statement.
7. In "if" and "if-else" statement, if the condition is true the statements in if block are executes.
8. In "if" staement, if the condition is false, it leaves the statements in if block and starts executing statements after the block.
9. In case "if-else" staement is false, it executes statements in 'else block.
10. Switch statement checks different constants after case statement with switch variable.
11. If switch variable matches it executes statements after switch.
12. If switch variable does not match with any of the case constant control goes to default statement if present.
13. Loops allow us to execute a statement or a group of statements several numbers of times.
14. "for" loop execute a sequence of statements multiple times.
15. "for" loop is usually used in situations where at the start of loop we know that how many times loop body will execute.
16. Condition is tested at the start of loop.
17. "while" loop is a pre-test loop.
18. "while" loop tests the condition at start of loop and is usually used in situations where at the start of loop we do not know that how many times loop block will execute.
19. "while" loop is also called indefinite repetition loop.
20. "do while" loop is similar to "while" loop, except that it tests the condition at the end of the loop body.
21. "do while" loop is also called post-test loop.
22. "do while" loop's statements block is executed at least one time.
23. A "break" statement terminates the loop or switch statement and transfers control to the statement immediately following the loop or switch statement.
24. A "continue" statement causes the loop to skip the remaining statements of its body and immediately transfers control to the top of the loop.
25. A "goto" statement jumps or transfers control unconditionally from the "goto" to a labeled statement in the same function.
26. A "return" statement terminates the execution of a function and transfers program control to the statement just after the function call statement in the calling function.
27. The exit is used to terminate a C++ program.

Source: Special Thanks To Sir Syed Arif Ali


UNIT 03. INPUT/OUTPUT HANDLING in C++ - Text Book Exercise And SLOs Question Answers - Computer Science For Class X

GO TO INDEX

Computer Science For Class X
Unit 03: Input / Output Handling In C++
Text Book Exercise & SLOs Question Answers

By Mrs. Ayesha Arif
Vice Principal
(Jauhar Progressive School)

SLOs Question-Answers

Q No.1: What is the basic structure of C++. Explain basic structure of C++ program.
Ans: BASIC STRUCTURE OF C++:
A C++ program is structured in a specific and particular manner. In C++, a program is divided into the following three basic structures or sections:
  1. Preprocessor Directives
  2. Main Function Header
  3. Body of Program /Function

1. PREPROCESSOR DIRECTIVES:
a. #include:
The # symbol is called preprocessor directive. #include is a specific preprocessor command that effectively copies and pastes the entire text of the file. It links the external header files / libraries which may be required in program.

b. <iostream>:
The file <iostream>, which is a standard file that should come with the C++ compiler, is short for input-output streams. This command contains code for displaying and getting an input from the user.

c. namespace std:
It is a prefix that is applied to all the names in a certain set. This instruction tells the compiler to use standard namespace. It is a collection of identifiers . It is used for variables, functions, class and objects . All these elements of the Standard library of C++ are declared within standard “std”.

2. MAIN FUNCTION HEADERS:
a. int main( ):
The starting point of all C++ programs is the int main( ) function. This function is called by the operating system when your program is executed by the computer .int main ( ) return value zero.
(int main(void) is used for execution of C++ program. void main(void) nothing to return.)

b. { :
It signifies the start of a block of code or indicates the beginning of the main function. It is also known as opening curly braces.

c. } :
This signifies the end of a block of code or indicates the ending of the main function. It is also known as closing curly braces.

3. BODY OF PROGRAM / FUNCTION:
The body of the function is enclosed between curly braces. All instructions are executed within opening "{" and closing "}" curly braces.

a. Statements:
Instructions that perform a particular task are called statements . Statement terminator (;) is used to end a statement. The symbol is called semi-colon.

b. cout :
The name cout is short for character output and displays whatever is between the << brackets.

c. << :
Symbols such as << can also behave like functions and are used with the keyword cout.
For example:
cout << "Pakistan Zindabad";
The output of the given example is that
"Pakistan Zindabad" will print on the screen.

d. The return:
The return keyword tells the program to return a value to the function int main.

e. After the return statement, execution control returns to the operating system component that launched this program.
(The return value; is the exit code of our program. By defualt, main( ) in C++ return an int data type value to the operating system.)


Q.2: Introduce the use of Preprocessor directives in C++ program.
Ans: Use of Preprocessor Directives in C++ Program:
The # symbol is called preprocessor directive. It gives instructions to the compiler to preprocess the information before actual compilation starts.
#include is a specific preprocessor command that effectively copies and pastes the entire text of the file. It is used to links the external header files / libraries which may be required in program. #define is used define constants in program.

Q No.3: What do you know about comment statement in C++?
Ans: COMMENT STATEMENTS:
A comment statement is where the programmer place a remark in the source code. The content of the comment is ignored by the compiler.These statements do not execute. Through comments, the programmers give special remarks to the statements for their convenience.
In CAT, there are two types of comment statements.
  1. Single Line Comment
  2. Multi Line Comment

1. Single Line Comment:
It is used for a single-line explanation with the help of a double slash (//) symbol. If the programmer wants to use a single line comment on more than one line, this may need to put a double slash on each line at the start. These comments are ignored by the compiler, which means comments are not executable.

For example:
 // Single line comment
 // This is my first program
 #include<iostream>
 int main( )
 {
 Statements…….;
 return 0;
 }


2. Multi Line Comment:
It is used for multiple-line explanations. Symbols (/* and */) are needed at the start and end of the statements. These comments are ignored by the compiler, which means comments are not executable.

For Example:
 /* Multi line comment
 This is my first program
 #include<iostream>
 int main( )
 Statements…...
 return 0;
 }


Q.4: Define I/O Streams or input/output handling in C++. OR Differentiate between input and output functions or I/O streams.
Ans: I/O STREAMS OR INPUT/OUTPUT HANDLING:
In C++ input and output (I/O) are performed in the form of a sequence of bytes, commonly known as streams. These I/O streams perform operation and are stored in header file. Such as <iostream>. These header files must be mentioned at the beginning of the program.

Difference Between Input And Output Functions Or I/O Streams:

INPUT FUNCTION OR STREAM:
  • If the direction of flow of bytes is from the device (for example, Keyboard) to the main memory then this process is called input.
  • Input function means to write or provide the program with some data to be used in it.

OUTPUT FUNCTION OR STREAM:
  • If the direction of flow of bytes is opposite, i.e. from main memory to device ( display screen like monitor) then this process is called output.
  • Output function means to display the output or data to the screen or write the data to a printer or a file.

Q.5: Use input and output function in a program.
Ans: For Answer see below Question No. 7.

Q.6: Use escape sequence in any C++ program.
Ans: Escape sequence in any C++ program:

 #include<iostream>
 #include<conio.h>
 using namespace std;
 int main()
 {
 cout << "\n\t Escape Sequence";
 cout << " \n\t Escape sequences are \"special non-printing\" characters. \\ They can be used with cout in the program.";
 cout << " \n\t \'Examples of escape Sequence";
 cout << " \n\t \a Alert Sound:";
 cout << " \n\t Pakistan \r ";
 cout << " \n\t if \\b place after the word in code, it will remove last letter as:\n\t \"Back is display as Back\b\" ";
 return 0;
 }



Q No.7: List all the input and output functions of I/O streams with descriptions.
Ans: I/O stream uses multiple input/output channels.

OUT PUT FUNCTIONS:
a. cout statement:
  • cout stands for "Character Output". Here 'C' menas "character" and 'Out' means "output".
  • Cout is a predefined object in C++. It is used to produce output on the standard output device which is usually the display screen or monitor.
  • The data needed to be displayed on the screen is inserted in the standard output stream (cout) using the insertion operator (<<).
  • Syntax: cout << variable or cout <<exp./string << variable

For Example:
cout << "MY FIRST PROGRAM";


b. puts():
  • It is used to print the string to the output stream.
  • The new line is automatically inserted after printing the string.
  • This is defined in <cstdio> header file. This file must be included at the beginning of the program.
  • Syntax: int puts(const char*str);

 For Example:
  #include<iostream>
  using namespace std;
  int main(void)
  {
  puts("MY FIRST PROGRAM");
  return 0;
 }


 OUTPUT:
MY FIRST PROGRAM



INPUT FUNCTIONS:
a. cin statement:
  • cin is a pre defined object that reads data from the keyboard with the extraction operator(>>).
  • It allows to accept data from standard input devices.
  • Syntax: cin >> varaible;

For Example:
 #include<iostream>
 using namespace std;
 int main(void)
 {
 int a;
 cin >> a; //cin takes input in "a" variable
 return 0;
 }




b. getch ():
  • It is a predefined function.
  • It is defined in conio.h.(Console input and output header file).
  • It stands for get character.
  • It is used to get a single character input from user (keyboard) during execution of program.
  • The entered character it is not displayed or printed on the screen.
  • It is used to hold the output screen until the user press any key from the keyboard.

For Example:
 #include<iostream>
 using namespace std;
 int main(void)
 {
 char ch = getch();
c out <<"X Class";
 cout << ch;

 return 0;
 }




c. getche ():
  • It stands for get character echo.
  • The function of "getche()" is similar to getch() function.
  • It is also predefined function in "conio.h" header file.
  • It is used to take a character from user and these characters are displayed on screen.
  • Syntax: character variable = getche();

For Example:
 #include<iostream>
 #include<conio.h>
 using namespace std;
 int main(void)
 {
 char ch0;
 int a=10,b=10;
 cout <<"\n Do you want to continue (Y/N)...";
 ch=getche();
 cout << "\n the addition is...";
 <<a+b;

 return 0;
 }

OUTPUT:

Do you want to continue (Y/N)... Y
the addition is...20



d. getchar ():
  • It is function in C++ that reads the charcter from standard input stream . It is defined in <stdio.h> header file.
  • It needs to press enter key after entering the character.

For Example:
 #include<iostream.h>
 #include<stdio.h>
 using namespace std;
 int main(void)
 {
 char ch;
 cout << "\n Use of getchar function";
 ch=getchar();
 cout << "\n getchar is" << ch;

 return 0;
 }

OUTPUT:

Use of getchar function ......a
getchar is ........a



e. gets():
  • It is a predefined function in C++ and it reads characters from stdin and stores them until a newline character found.
  • It is defined in <cstdio> header file.
  • Program shows how to apply this function in C++.
  • Syntax: gets(variable);

For Example:
 #include<iostream>
 #include<stdio.h>
 using namespace std;
 int main(void)
 {
 char ch[20];
 cout << "\n Enter the message .....";
 get(ch);
 cout << " Your message is .... . .; << ch;

 return 0;
 }

OUTPUT

Enter the message .....
Pakistan
Your message is ......
Pakistan



Q No.8: Define statement terminators.
Ans:STATEMENT TERMINATORS:
Statement terminators are semicolon (;) that indicates the end of the statement. Every statement in C++ must be terminated with semi colon (;). If the terminator is missing an error message will occur.

Q No.9: What are escape sequences?
Ans: ESCAPE SEQUENCES:
The escape sequences are special non-printing characters that can be used with the “cout” in C++. It starts with a backslash (\) and a code character.

Some commonly used escape sequences are as follows:

Q No.10: What are operators? Discuss in detail.
Ans: OPERATORS:
In programming, an operator is a symbol that operates on a value or a variable.
Operators are special symbols that perform operations on variables and values. They are used for specific purposes. They perform mathematical operations on operands.
For example: x + y.
Here
  • "x" and "y" are operand and
  • "+" is an operator used for addition.

Similarly "-" is also an operator used for subtraction.

TYPES OF OPERATORS:
Operators in C++ can be classified into 7 types:
  1. Arithmetic Operators
  2. Increment Operators
  3. Decrement Operators
  4. Relational Operators
  5. Logical Operators
  6. Assignment Operators
  7. Arithmetic assignment operators

1. Arithmetic Operators:
Arithmetic operators are used to perform arithmetic operations on variables and data. There are five different arithmetic operators. They are:
  1. Addition (+):
    It is used to perform arithmetic addition.
    Example: a + b;

  2. Subtraction (-):
    It’s used to perform arithmetic subtraction.
    Example: a - b;

  3. Multiplication (*):
    It used to perform arithmetic multiplication.
    Example: a * b;

  4. Division (/):
    It performs the arithmetic division of two numbers.
    In the division of integers, numbers show only whole number in result.
    Example: a/ b.

  5. Remainder / Modulus (%):
    It is used to find remainder of a division. It returns the remainder of an integer value. Remainder operator is also known as Modulus operator. This operator is used only with integral data types.
    Example: 5/2 = 2 and 1 is remainder. We can write in this form 5%2

All arithmetic operators except Remainder or Modulus operator can be used in integer and float data type.

2. Increment Operators:
Increment operators are used to add 1 to the value of a variable. They can be used with any type of variable and are represented by ++ (double plus ) sign. It can be applied to a single variable.
There are two ways to use increment operators:

  1. Pefix Increment operator:
    This operator is applied before variable name.
    It can be written like ++a.
    i.e. x=++a;

    In prefix increment operation value of 'a' is printed as 11 because 1 is added in 'a' before printing.

  2. Postfix Increment Operator:
    This operator is applied after variable name.
    It is written like a++.


    In Postfix increment operation value of 'a' is printen as 10 because 1 is added in 'a' after printing. So the value of 'a' will change to 11 after printing.

3. Decrement Operators:
Decrement operators are same as increment operators but they are used to subtract 1 from the value of a variable. They can be used with any variable and are represented by -- (double minus ) sign.
There are two ways to use decrement operators:
  1. Prefix Decrement operator:
    This operator is applied before variable name.
    Example --a

  2. Post fix Decrement Operator:
    This operator is applied after variable name.
    Example a—

4.Relational Operators:
A relational operator is used to test the relationship between two values (operands). All relational operators are binary operators. They are also called comparison operators.
These operators must require two operands. The result of the comparison is True (1) or False(0).
The following are some relational operators:


5. Logical Operator:
Logical Operators are used to determine two relational expression. They operators can be used in many conditional and relational expressions.
There are three logical operators that are used in C++ programming:


6. Assignment Operators:
The assignment operator (=) is used for assigning a variable to a value. This operator assigns the value of right side expression to left side variable.
Such as x=10;

7. Arithmetic Assignment Operators:
In arithmetic assignment operators , the arithmetic operator is combined with the assignment operator. The assignment operator comes to the right of an arithmetic operator. This is also called compound assignment operator.
The following are the arithmetic assignment operators:
S.NO. Words Meanings
1. += (Addition–Assignment)
 It adds the right operand to the left operand and assigns result to the left operand
 Example: a+2 means a=a+2

2. -= (Subtraction-assignment)
 It subtracts the right operand from the left operand and assigns result to the left operand.
 Example: a -= 3 means a=a-3

3. *= (Multiplication–Assignment)
 It multiplies the right operand with the left operand and assigns result to the left operand.
 Example: a *=4 means a=a*4

4. /= (Division–Assignment)
 It divides the left operand with the right operand and assigns result to the left operand.
 Example: a /=4 means a=a/4



Q No.11: Differentiate between relational and logical operators.
Ans: Difference Between Relational And Logical Operators
S.NO. Relational Operators Logical Operators
1. A relational operator is used to check the relationship between two given variables (operands). Logical Operators are used to determine two relational expression.
2. All relational operators are binary operators. They require two operands. They are used to combine one and more than one relational expression
3. The result of the comparison is True (1) or False (0). The result is True (1) or False(0).


Q No.12: Differentiate between assignment operator and equal to operator.
Ans: Difference Between Assignment Operator And Equal To Operator.
S.NO. Assignment Operator(=) Equal to operator (= =)
1.
 The assignment operator (=) is used for assigning a variable to a value.


 The equal to (= =) operator is used to check the equality of two operands values.

2.
 This operator assigns the value of right-side expression to left-side variable.
 Such as x=10

 It compares the value of left-side and right-side expression.
 Such as x=10 and y=10 than x= = y
 If condition true otherwise false.


TEXT BOOK EXERCISE
B. RESPOND THE FOLLOWING:

1. Use \a and \r both escape sequences in a program.
Ans: \a:
"a" means Alert or alarm. It cause a beep sound in the computer.
Example: cout << "\a";

\r:
Carriage return "r". It is used to position the cursor to the beginning of the cursor line.
Example: cout << "\r";

Use of \a and \r In Escape Sequences In A Program:
EXAMPLE:

 #include<iostream>
 #include<conio.h>
 using namespace std;
 int main( )
 {
 cout << " \a Alert Sound:";
 cout << " \n Pakistan \r ";
 return 0;
 }


2. How many types of comment statements are used in C++?
Ans: Ans. There are two types of comment statements used in C++.
  1. Single line comment (//)
  2. Multi line Comment (*/)
These statements do not execute.

1. Single Line Comment:
It is used for a single-line explanation with the help of a double slash (//) symbol. Double slash (//) symbols are used at the start of each single line comment. These comments are ignored by the compiler, which means comments are not executable.

For Example:
 // Single line comment
 // This is my first program
 #include<iostream>
 int main( )
 { 
 Statements…….;
 return 0;
 }


2. Multi Line Comment:
It is used for multiple-line explanations. Symbols (/* and */) are needed at the start and end of the statements. These comments are ignored by the compiler, which means comments are not executable.

For Example:
 /* Multi line comment
 This is my first program
 #include<iostream>
 int main( )
 Statements…...
 return 0;
 }


3. Differentiate between Arithmetic operators and Relational operators.
Ans: Difference Between Arithmetic Operators And Relational Operators:
S.NO. Arithmetic Operator Relational Operator
1. Arithmetic operators are used to perform mathematical operations. The relational operators are used to compare any two values.
2. All operators use integer & floating- point data type except remainder or modulas operator. All operators use integer & floating-point data type.
3. The result of arithmetic operation is a value. The result of comparison is True (1) or False (0).
4. In Arithmetic operators, five different operators are used to perform an arithmetic operation. These operators are used to perform logical operations on two given variables.
5. Five different operators are Addition, Subtraction, Multiplication, Division, Remainder /Modulus. Equal to ==, Not equal to != , greater than > , less than < , greater than equal to >= and Less than Equal to <= are the Relational operators.


4. Write a program in C++ and use all arithmetic assignment operators.
Ans: A program In C++ With All Arithmetic Assignment Operators:


OR


5. What is basic difference between Assignment operator and Equal to operator.
Ans: Difference Between Assignment Operator And Equal To Operator.
S.NO. Assignment Operator(=) Equal to operator (= =)
1.
 Assignment operator (=) is used for assigning a variable to a value.


 The equal to (= =) operator is used to check the equality of two operands values.

2.
 This operator assigns the value of right-side expression to left-side variable.
 Such as x=10

 It compares the value of left-side and right-side expression.
 Such as x=10 and y=10 than x= = y
 If condition true otherwise false.

6. What is the basic difference between \n and \t?
Ans: Difference Between \n and \t:
S.NO. \t \n
1.
 "\t" stands for Horizontal tab.

 "\n" stands for New line or line feed.
2.
 It is used to shift the cursor to a couple of spaces to the right in the same line.

 It insert a new line and cursor moves to the beginning of the next line.
3.
 Example: cout << "\t";


 Example: cout << "\n";


7. Get the output of following program.


Ans: Output: