C programming answers
C programming answers
### 2. Operators in C:
```c
#include <stdio.h>
int main() {
// Arithmetic operators
result = a + b;
// Conditional operator
int c = 7;
c++;
// Relational operators
if (a != b) {
// code
return 0;
```
- Right shift (`>>`): Shifts the bits of the left operand to the right by the number of positions specified
by the right operand.
- Left shift (`<<`): Shifts the bits of the left operand to the left by the number of positions specified by
the right operand.
```c
#include <stdio.h>
int main() {
char ch = 'a';
// Convert to uppercase
// Convert to lowercase
return 0;
```
2. **Compiled:** The source code is translated into machine code or intermediate code by a
compiler, and then the compiled code is executed.
#### I. If statement:
```c
if (condition) {
// code
```
#### II. If else statement:
```c
if (condition) {
// code
} else {
// code
```
```c
switch (expression) {
case constant1:
// code
break;
case constant2:
// code
break;
default:
// code
```
```c
if (condition1) {
if (condition2) {
// code
```
### 8. C program with conditional operators:
```c
#include <stdio.h>
int main() {
// Conditional operator
result = (x > y) ? x : y;
return 0;
```
The `if` statement is called one-way branching because it allows the program to take one of two
paths based on whether the condition is true or false. It provides a single alternative for the
execution of code.
The `if-else` statement is called two-way branching because it provides two possible paths for the
program to follow. If the condition is true, the code inside the `if` block is executed; otherwise, the
code inside the `else` block is executed.
Yes, the conditional operator (`? :`) can be used as a compact form of the if-else statement. However,
it's important to note that it is not a direct replacement for more complex if-else structures.
### 12. What is a loop?
```c
// code
```
```c
while (condition) {
// code
```
```c
do {
// code
} while (condition);
```
```c
#include <stdio.h>
int main() {
// i. For loop
// code
int j = 0;
while (j < 5) {
// code
j++;
int k = 0;
do {
// code
k++;
return 0;
```
```c
#include <stdio.h>
int main() {
printf("Your Name\n");
return 0;
```
The `break` keyword is used to exit from a loop or switch statement prematurely. When
encountered, it terminates the nearest enclosing loop or switch statement and transfers control to
the statement immediately following the terminated loop or switch.
```c
#include <stdio.h>
int main() {
if (i == 5) {
break;
printf("%d\n", i);
return 0;
```
### 18. Use of the `continue` keyword:
The `continue` keyword is used to skip the rest of the code inside a loop for the current iteration and
jump to the next iteration of the loop.
```c
#include <stdio.h>
int main() {
if (i == 2) {
continue;
printf("%d\n", i);
return 0;
```
The `continue` keyword is important for skipping certain iterations of a loop based on a condition,
allowing more control over the flow of the loop. It helps in avoiding the execution of unnecessary
code for specific cases, improving efficiency.