Programming Concept
Programming Concept
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);
return 0;
}
OUTPUT
a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b=1
Assignment Operators
Assignment operators are used to assign values to variables.
Operator Example Same As
= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
&= x &= 3 x=x&3
|= x |= 3 x=x|3
^= x ^= 3 x=x^3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3
Example:
#include <stdio.h>
int main()
{
int a = 5, c;
c = a; // c is 5
printf("c = %d\n", c);
c += a; // c is 10 Output
printf("c = %d\n", c);
c -= a; // c is 5 c=5
printf("c = %d\n", c); c = 10
c=5
c *= a; // c is 25
printf("c = %d\n", c);
c = 25
c=5
c /= a; // c is 5 c=0
printf("c = %d\n", c);
c %= a; // c = 0
printf("c = %d\n", c);
return 0;
}
Relational Operators
Comparison operators are used to compare two values (or
variables). This is important in programming, because it helps us to
find answers and make decisions.
== Equal to x == y
!= Not equal x != y
return 0;
}
Logical Operators
Logical operators are used to determine the logic between variables or values:
! Logical not Reverse the result, returns !(x < 5 && x < 10)
false if the result is true
Example:
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;
return 0;
}
Bitwise Operators
In the arithmetic-logic unit (which is within the CPU), mathematical operations
like: addition, subtraction, multiplication and division are done in bit-level. To
perform bit-level operations in C programming, bitwise operators are used.
return 0;
}
Operator Description Precedence Associativity
Syntax:
if (condition)
{
// statements to execute if condition is true
}
Example:
#include <stdio.h>
int main() {
int number;
if (number < 0)
{
printf("You entered %d.\n", number);
}
return 0;
}
if-else statement
The if-else statement is a fundamental control flow structure that
allows for conditional execution of code. It tests a condition: if the
condition is true, one block of code is executed, and if the condition
is false, another block (or none at all) is executed.
Syntax :
if (condition)
{
// statements to execute if condition is true
}
else
{
// statements to execute if condition is false
}
Example:
// Check whether an integer is odd or even
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
return 0;
}
if...else Ladder
The if...else statement executes two different codes depending upon
whether the test expression is true or false. Sometimes, a choice ha
to be made from more than 2 possibilities.
return 0;
}
Nested if...else
It is possible to include an if...else statement inside the body of
another if...else statement.
Syntax :
if (outer condition) {
if (inner condition1) {
// Code for inner condition1
} else if (inner condition2) {
// Code for inner condition2
} else {
// Code for other cases within outer condition
}
} else if (another outer condition) {
// Code for another outer condition
} else {
// Code for cases not covered by any condition
}
Example:
#include <stdio.h>
int main() {
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
return 0;
}
switch statement in C
The switch statement is a control flow statement in C (and many other
programming languages) that allows you to choose one of several
possible code blocks to execute based on the value of an expression.
It’s often used as a more concise alternative to a series of if-else
statements when you need to compare a single value against multiple
possible values.
Syntax :
switch (expression) {
case value1:
// code will be executed if expression equals value1
break;
case value2:
// code will be executed if expression equals value2
break;
// additional cases...
default:
// code will be executed if none of the above cases
match
}
Example:
#include <stdio.h>
int main() {
char rideType = 'P'; // U for UberX, P for UberPool, L for UberLux
switch (rideType) {
case 'U':
printf("You've selected UberX. A comfortable ride for up to 4.\n");
break;
case 'P':
printf("You've selected UberPool. Share your ride and save!\n");
break;
case 'L':
printf("You've selected UberLux. Ride in style!\n");
break;
default:
printf("Invalid selection. Please choose a valid ride type.\n");
}
return 0;
}
Iteration Statements (Loops)
We use loops to execute a block of code repeatedly based on a
condition or a set of conditions.
return 0;
}
do-while loop
The do-while loop checks the condition after executing the loop
body, which ensures that the loop body is executed at least once,
even if the condition is initially false.
Syntax :
do
{
// code to be executed;
} while (condition);
Example:
#include <stdio.h>
int main() {
double number, sum = 0;
printf("Sum = %lf",sum);
return 0;
}