Can anyone help explain these to me Assume you are in C 1 Op

Can anyone help explain these to me? Assume you are in C++

1. Operators

a. +, -. *, /, %, =, operation with assignment, pre/post increment/decrement, and ?:,

b. &&, ||, !

2. Flow of control statements:

a. if, if-else, if-else ladder, switch, default, and break

b. dangling else

c. for, while, do-while, break, continue, and test at the top versus test at the bottom

d. null-statement

e. block-structure

Solution

1. Operators
So, operators are used to perform operations , like for example if we

+ : Addition Operator , Used to perform addition. It requires two operands Eg: 2+3 = 5
3+4 = 7.
- : Substraction Operator , Used to perform substraction. It requires two operands Eg: 3-2 = 1
10-5 = 5.
* : Multiplication Operator , Used to perform multiplication. It requires two operands Eg: 3*2 = 6
10*5 = 50.
/ : Division Operator , Used to perform division. It requires two operands Eg: 10/2 = 5
10/5 = 2.
% : Modulos Operator , Used to perform modulo. It gives us the remainder. It requires two operands Eg: 10%2 = 0 (because 2 completely divides 10 and there is no remainder).
10%3 = 1 ( 3*3 = 9 , so remainder is 1 ).
= : Assignment Operator : Used to perform assignment. a = 2 , b = 5 . Assigns the value 2 to a ,. Similiarly assigns the value of 5 to b.
++ : Increment Operator: Requires only one operand.
++a (Pre Increment) : Increments the value then assigns it to other variable.
a++ (Post Increment) : Assigns and then increment Increments the value

For example x = 5
If we do x++ or ++x , Both will increment it to value as 6

When It is used with a variable then it behaves differently

if x = 6
m = ++x; m = 7 ( Increments and then assigns, both m and x are 7 now)

if p = 3
k = p++ ; k = 3 , p will be incremented to 4

-- : Decrement Operator: Requires only one operand.
--a (Pre Decrement) : Decrements the value then assigns it to other variable.
a-- (Post Decrement) : Assigns and then Decrements the value

For example x = 5
If we do x-- or --x , Both will increment it to value as 4

When It is used with a variable then it behaves differently

if x = 6
m = --x; m = 5 ( Increments and then assigns, both m and x are 5 now)

if p = 3
k = p-- ; k = 3 , p will be incremented to 2

?: Conditional Operator: It is used in conjunction with if else
1. If a = 2, b =2;
a==b ? true : false [i.e if a==b then return the first argument which is true here , hence it returns true].

b. && : Logical AND operator: Requires two operands , Returns true if both the operand are non zero , otherwise returns false.
a = 5 , b = 2
a&&b --> return true
p =0 , q= 4
p&&q-->return false (as p is 0)
|| : Logical OR operator: Requires two operands , Returns true if either one the operand are non zero , returns false if both the operands are zero
a = 5 , b = 2
a||b --> return true
p =0 , q= 4
p||q-->return true (as q is non zero)
m =0 , n= 0
m || n-->return false (as m,n both are zero)

! : Logical NOT operator: Requires one operands , Returns true if operand are zero , returns false if the operand is true
a = 5
! a --> return false
p =0
!p->return true (as p is 0)
=========================================================================
Flow of control statements : It means how the control will flow from one statement to the other . Lets see How ?

a. if : If is just used to check the condition i.e if the condition is true a block of statemnet will be executed otherwise those statements will be skipped

if(condition){
Execute set of statements
}

2. if-else : It means if the condition in the if is satisfied then execute the if block else execute the else part.
if(condition){
Execute set of statements of If Part
}else{
If condition did not satisfied so execute the else part
}

3. if -else ladder: If we are having multiple test coinditions then we can add all into if-else aldder. Here is how
if(condition 1){
If condition 1 is satisfied so execute set of statements
}else if (condition 2){

If condition 2 is satisfied so execute set of statements
}else if (condition 3){

If condition 3 is satisfied so execute set of statements
}else{

Neither of the condition is satisfied so execute set of statements
}
4. Switch :
switch statements allow a variable to be tested based on cases. Based on the choice a particular case will be matched and if none of them matches it goes to default case and after every case there is break so that it doesnt go to next case following the control.
switch(choice){

case \'1\' :
break;
case \'2\':
break;
default:
break;

}

b. Dangling else:
If we have nested if condition and a single else. For example
if(condition)
   if(condition)
   if(condition)
else{
}


Here the else is dangling because it is diffcult to know as else belong to which if . Hence Using braces we can associate else to any if Like: Else belongs to first if
if(condition){
   if(condition)
   if(condition)
}else{
}

c. for :Its a looping constructs that repeats a group of statements until the condition is true
for(int i=1;i<=10;++i)
  printf(\"Hello World\");
Prints \"Hello World\" 10 times

c. while :Its a looping constructs that repeats a group of statements until the condition is true

i =1;
while(i<=10){
printf(\"Hello World\");
i = i +1 ;
}

Prints \"Hello World\" 10 times

c. do-while :Its a looping constructs that executes atleast once because the coondition comes in while

Prints
Good morning atleast once
do{

printf(\"Good morning\");

}while(condition):

break:
break statement is used to break the flow of execution inside a loop .

i =1;
while(i<=10){
if(i==5)
break;

i = i +1;
}
When i become 5 the loop breaks, i.e the control reached out of while.

Continue: Continue is used to skip a set of statements instead of breaking out of loop . It forces to move to next iteration.

i =1;
while(i<=10){
if(i==5){
i = i +1;
continue;
}

print(\"Good morning\");

i = i +1;
}

when i becomes 5, print statememnt wont be executed , It will skip set of stetements at the bottom.

d. Null statements : Null statements performs no operation, It consists of just semicolon
if(i==2)
;
else
printf(\"Good Morning\");

e. Block Structure : Something that define block or scope . C permits creation of blocks where we place our code and hence those blocks defines the scope of variables etc

int main()
{

printf(\"This is block 1\");

{
int a, b

printf(\"This is block 2\");
}

}


======================================================================
Thanks, I hope it clarifies. Let me know if there is any concern

Can anyone help explain these to me? Assume you are in C++ 1. Operators a. +, -. *, /, %, =, operation with assignment, pre/post increment/decrement, and ?:, b.
Can anyone help explain these to me? Assume you are in C++ 1. Operators a. +, -. *, /, %, =, operation with assignment, pre/post increment/decrement, and ?:, b.
Can anyone help explain these to me? Assume you are in C++ 1. Operators a. +, -. *, /, %, =, operation with assignment, pre/post increment/decrement, and ?:, b.
Can anyone help explain these to me? Assume you are in C++ 1. Operators a. +, -. *, /, %, =, operation with assignment, pre/post increment/decrement, and ?:, b.

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site