1) Assignment statement :
An assignment "statement" is not really a statement , but is an expression. The value of the expression is the value that is assigned to the variable.
Example :
x = 19; // gives x the value 19.
The value of a variable may be changed. For example, if x has the value 19, then the assignment statement
x = x + 1;
will give x the value 20.
2) Input-- cin>> :
The input operator works on two operands, namely, the c in stream on its left and a variable on its right. The input operator, commonly known as the extraction operator (>>), is used with the standard input stream, cin . The input operator takes (extracts) the value through cin and stores it in the variable.
Example :
#include<iostream>
using namespace, std;
int main ()
{
int c;
cin>>c;
c = c-1;
return 0;
}
In this example, the statement cin>> a takes an input from the user and stores it in the variable c.
3) Output -- cout << endl << "\n" :
The "c" in cout refers to "character" and 'out' means "output", hence cout means "character output". The cout object is used along with the insertion operator (<<) in order to display a stream of characters
Example :
output : Enter your number : 22
Your number is : 22
b ) # include <iostream>
int main()
{
cout<<"hello \n";
cout<<"HomeworkLib ";
}
3) Output formatting : In output formatting format specifier is used during input and output. It is a way to tell the compiler what type of data is in a variable during taking input using scanf() or printing using printf(). Some examples are %c, %d, %f, etc.
Example :
#include<stdio.h>
int main();
{
int A=12;
printf("%d ",A)
}
Output:
12
4) Arithmetic operator : Arithmetic operators are the symbols that represent arithmetic math operations.
Examples include + (addition operator), - (subtraction operator), * (multiplication operator), and / (division operator).
Symbol |
Operation |
Example |
+ |
Addition |
x = 7 + 2 9 |
- |
Subtraction |
x = 5 - 2 3 |
* |
Multiplication |
x = 6 * 2 12 |
/ |
Division |
x = 24/8 3 |
% |
Modulus |
x = 7 % 3 1 |
5) Relational operator :
Relational operators are sometimes called comparison operators. Expressions that contain relational operators are called relational expressions.
+= | a += b | a = a+b |
-= | a -= b | a = a-b |
*= | a *= b | a = a*b |
/= | a /= b | a = a/b |
6) logical operator : An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false. Logical operators are commonly used in decision making in C programming.
Example :
Operator | Meaning of Operator | Example |
---|---|---|
&& | Logial AND. True only if all operands are true | If c = 4 and d = 2 then, expression ((c == 4) && (d > 5)) equals to 0. |
|| | Logical OR. True only if either one operand is true | If c = 4 and d = 2 then, expression ((c == 4) || (d > 5)) equals to 1. |
! | Logical NOT. True only if the operand is 0 | If c = 4 then, expression ! (c == 4) equals to 0. |
7) Mixed types expression : If operands in an expression contains both INTEGER and REAL constants or variables, this is a mixed mode arithmetic expression.
In mixed mode arithmetic expressions, INTEGER operands are always converted to REAL before carrying out any computations. As a result, the result of a mixed mode expression is of REAL type.
Example :
Mixed mode operator example
2 + 2 * 5 ** 3
==> 2 + 2 * (5**3)
==> 2 + 2 * 125
==> 2 + (2 * 125)
==> 2 + 250 = 252
8) Boolean expression : A Boolean expression is a logical statement that is either TRUE or FALSE. Boolean expressions can compare data of any type as long as both parts of the expression have the same basic data type. You can test data to see if it is equal to, greater than, or less than other data.
Example :
An if statement like
if (danger == true) { System.out.println("Run away!"); } |
if (danger == false) { System.out.println("Relax."); } |
. Conceptually, we say that it checks whether the condition``true'' or ``false''.
9) Implicit and Explicit type conversion :
Type Casting : Conversion of one data type to another data type. and it can be done in two ways.
Implicit type casting is performed by the compiler on its own when it encounters a mixed data type expression in the program. it is also known as automatic conversion as it is done by compiler without programmer’s assistance. implicit casting doesn’t require a casting operator.
Example :-
here b will contain typecast value of a, because while assigning value to b compiler typecasts the value of a into float then assigns it to b.
Explicit type casting is performed by the programmer. In this type casting programmer tells compiler to type cast one data type to another data type using type casting operator. but there is some risk of information loss is there, so one needs to be careful while doing it.
Example :-
here we explicitly converted float value of a to int while assigning it to int b. (int) is the type casting operator with the type in which you wants to convert.
10 ) Control Structure :
n a program, a control structure determines the order in which statements are executed. The following are the basic control structures in the programming languages:
Sequential
In Sequential execution each statement in the source code will be executed one by one in a sequential order. This is the default mode of execution.
Selection
The selection control structure is used for making decisions and branching statements, the following are the basic selection statements in the programming language.
• if:
The If statement checks for the condition and if the condition is satisfied it executes the statements in the “if” block, otherwise it exits the “if” block.
Syntax:
• if-else:
If-else statement is used to execute any of the two blocks of statements, if the condition satisfies it executes the statements in the “if block”, otherwise it executes the “else block”.
Syntax:
switch case:
Switch statement is used when a variable is to be checked for equality in a list of values.
Syntax:
Iteration
The iterative control structures are used for repetitively executing a block of code multiple times; the following are the basic iterative control structures.
•for:
A for loop execute the statements for a specific number of times mentioned in the condition.
Syntax:
•while:
A while loop executes statement repeatedly until the condition satisfies.
Syntax:
• do-while:
A do-while loop works similar to while loop, the only difference is it executes the statement once before checking the condition.
Syntax:
nested if :
It is always legal in C programming to nest if-else statements, which means you can use one if or else if statement inside another if or else if statement(s).
Syntax
The syntax for a nested if statement is as follows −
if( boolean_expression 1) { /* Executes when the boolean expression 1 is true */ if(boolean_expression 2) { /* Executes when the boolean expression 2 is true */ } }
Nested loops:
C programming allows to use one loop inside another loop. The following section shows a few examples to illustrate the concept.
Syntax
The syntax for a nested for loop statement in C is as follows −
for ( init; condition; increment ) { for ( init; condition; increment ) { statement(s); } statement(s); }
The syntax for a nested while loop statement in C programming language is as follows −
while(condition) { while(condition) { statement(s); } statement(s); }
The syntax for a nested do...while loop statement in C programming language is as follows −
do { statement(s); do { statement(s); }while( condition ); }while( condition );
A final note on loop nesting is that you can put any type of loop inside any other type of loop. For example, a 'for' loop can be inside a 'while' loop or vice versa
Conditional statements :
Conditional statements helps you to make decision based on certain conditions. These conditions are specified by a set of conditional statements having boolean expressions which are evaluated to a boolean value true or false. There are following types of conditional statements in C.
If statement
If-Else statement
Nested If-else statement
If-Else If ladder
Switch statement
11 ) Local variable and variable scope :
All the variables we have used thus far have been local variables. A local variable is a variable which is either a variable declared within the function or is an argument passed to a function. As you may have encountered in your programming, if we declare variables in a function then we can only use them within that function. This is a direct result of placing our declaration statements inside functions.
Consider a program that calls a swap() function.
#include <iostream.h>
void swap(int x, int y) {
int temp = x;
x = y;
y = temp;
}
int main()
{
int A, B;
cin >> A >> B;
cout << "A: " << A << "." << endl;
cout << "B: " << B << "." << endl;
swap(A, B);
cout << "A: " << A << "." << endl;
cout << "B: " << B << "." << endl;
return 0;
}
Consider variables which are in scope (DEF: portion of a program
in which a
variable is legally accessible) in the main() and swap()
functions:
Function |
Accessible variables |
main |
A, B |
swap |
temp, x, y |
If you attempted to use A was in swap(), a compilation error would result ("undeclared identifier") since it is a local variable in only main().
12 ) Sentinels :
Sentinel Controlled Loop
When we don’t know exactly know how many times loop body will be executed known as Sentinel Controlled Loop, for example - Reverse a given number, such kind of problem will be solved using sentinel controlled loop.
Consider the code :
int reverseNumber=0; int number=12345; while(number>0) { reverseNumber= (reverseNumber*10)+ number%10; number/=10; } printf("Reverse Number is: %d\n", reverseNumber);
12 ) Counters :
Counter Controlled Loop
When we know how many times loop body will be executed known as Counter Controlled Loop, for example - print natural numbers from 1 to 100, such kind of problem will be solved using counter controlled loop.
Consider the code :
int count; for( count=1; count<=100; count++) printf("%d",count);
13) priming reads :
A prime read is just a way to control repetition. Basically, a
prime read is a data input, before the loop statement, that allows
the first actual data value to be entered so it can be checked in
the loop statement.
The variable that is inputted by the user and being tested by the
expression in the loop is the prime read; the value of the prime
read is what you would normally call the sentinel value.
Example:
int NumPeople = 0;
int SumHeights = 0;
int Height;
cin >> Height ;
while (Height != 0 ) {
NumPeople++;
SumHeights = SumHeights + Height;
cin >> Height ;
}
float AvgHeight = SumHeights / NumPeople;
The first cin is a priming read .
14 ) Accumulators :
The computers, present in the early days of computer history, had accumulator based CPUs. In this type of CPU organization, the accumulator register is used implicitly for processing all instructions of a program and store the results into the accumulator.
The main points about Single Accumulator based CPU Organisation are:
Example :
Data transfer operation –
In this type of operation, the data is transferred from a source to
a destination.
For ex: LOAD X, STORE Y
14 ) Validating inputs :
Validating inputs means of ensuring bogus data doesn't get entered where they don't want it. After all, computers only do what we tell them. If you give them garbage, they are going to give you garbage right back.
We can look at validation in terms of numeric or string validation.
15) Overflow and underflow :
Overflow : Overflow is when the absolute value of the number is too high for the computer to represent it.
Example :
If the variable x is a signed byte it can have values in the range -128 to +127, then
will result in an overflow. +128 is not a valid value for x.
Underflow :
Underflow is when the absolute value of the number is too close to zero for the computer to represent it.
Example :
For floating point numbers, the range depends on their representation. If x is a single precision (32-bit IEEE) number, then
will result in an underflow. 1e-42 is not a valid value for x.
16 ) Using files for I/O :
a) opening file :
open() function is used for opening a file.
Syntax:
FILE pointer_name = fopen ("file_name", "Mode");
pointer_name can be anything of your choice.
Example :
FILE *fp;
fp = fopen("C:\\myfiles\\newfile.txt", "r");
The address of the first character is stored in pointer fp.
B) Closing file() :
FILE *fp;
fp = fopen("C:\\myfiles\\newfile.txt", "r");
The address of the first character is stored in pointer fp.
c) reading and writing : fgets & fputs :
fgets : reading strinfs to a file -
char *fgets(char *s, int rec_len, FILE *fpr)
s: Array of characters to store strings.
rec_len: Length of the input record.
fpr: Pointer to the input file.
fputs: Writing string to a file -
int fputs ( const char * s, FILE * fpw );
char *s – Array of char.
FILE *fpw – Pointer (of FILE type) to the file, which is going to
be written.
Can you please explain what does each sentence mean and give an example if it’s possible?....
Please help me code the following in: JAVA Please create the code in as basic way as possible, so I can understand it better :) Full points will be awarded, thanks in advance! Program Description Write a program that demonstrates the skills we've learned throughout this quarter. This type of project offers only a few guidelines, allowing you to invest as much time and polish as you want, as long as you meet the program requirements described below. You can...
Topics: About Java What is a compiler, and what does it do? Characteristics of the languageStrongly typed and statically typed Everything has a data type & data types must be declared Case sensitive Object oriented System.out.print() vs. System.out.println() How to use the Scanner class to obtain user input Data typesWhat are they? Know the basic types like: int, double, boolean, String, etc. Variables What is a variable? Declarations Initialization Assignment Constants Reserved words like: What is a reserved word? Examples:...