PPS Assignment: Control Statements & Programs (PDF)

Document Details

LovelyHoneysuckle6634

Uploaded by LovelyHoneysuckle6634

IIMT College of Engineering

Tags

C programming control statements programming assignments conditional statements

Summary

This document provides an assignment on control statements, particularly if-else, if-nested if, and switch statements in C programming. It includes example programs for employee salary calculations & calculators and then briefly explains precedence, associativity, and different types of bitwise operators like AND, OR, XOR.

Full Transcript

## Control Statement ## - **What is a control statement?** - Control statement allows users to specify the order in which instructions in a program are executed. - They enable programs to make decisions, repeat tasks, or jump to different sections of code. - The following are several e...

## Control Statement ## - **What is a control statement?** - Control statement allows users to specify the order in which instructions in a program are executed. - They enable programs to make decisions, repeat tasks, or jump to different sections of code. - The following are several examples: - if else - if nested if - switch ## if else ## - **What is an if else statement?** - An if else statement is used for checking the condition. - If the condition is true, then the statement is executed. - Otherwise, the program will go to the else statement. - **Syntax:** ``` if (condition) statement 1 else statement 2 ``` ## switch ## - **What is a switch statement?** - A switch statement is used for checking one time multiple cases. - On switch, we can not pass the condition, only passing the expression. - **How does it work?** - In switch expressions mapping with the different cases. If the case is satisfied, then the statement is executed and comeback by the break statement. - If not satisfied, then the program will go to the default. - **Syntax:** ``` switch (expression) case 1: stat 1; break; case 2: stat 2; break; ... case n: stat n; break; default: stat; ``` ## Write a program to calculate salary of an employee with conditions: ```c #include <stdio.h> #include<conio.h> void main() { int nsal, b; clrscr(); printf ("In enter basic"); 'scanf("%d" ("%d",&b); if (b>= 10000ffb220000) nsal = b+ Cbx5)/100+(b*6)/100+ (b*7) else 1100; if (b>= 20000 f fb< 40000) nsal nsal=b+(b*8) 1100+ (69) 1100+(6*10)/100; else if (b=40000) nsal = b+(bx11)/100+(bx2)/100+(6*13/10; ngal else nsal = b+(b*3)/100+ (bx3)/100+(b+3)/100; nsay printf ("In net sal=y.d", nsal); getch(); } ``` ## Write a program to create a calculator: ```c #include <stdio.h> #include<conio.h> void main() { int choice; float a, b, result; printf ("" "Simple Calculator In"); "perintf("1. AdditionIn"); printf("2. Subtraction In"); printf("3, Multiplication\n"); printf("4. Division In"); printf("5. Exit In");" scanf("%d", & result); perintf(" In enter two number"); Scanf("%d %d, fa, fb); switch (choice) { case 1: a = atb print("In addition = "/di break case 2! a=(a−b) print("In Subtraction = %.dia); break Case 3: a = (ab) break perintf("In Multiplication=%.dia) Case 4: 9 = (a+b) printf("In Division = %.d" a); break; default: printf("Invalid option") getch(); } ``` ## Precedence and Associativity - **Precedence:** The order in which operators are evaluated in an expression. Operators with higher precedence are evaluated first. - **Associativity:** The order in which operators with the same precedence are evaluated. Associativity can be left to right or right to left. - **What is an operator?** An operator in C is defined as the symbol that helps us to perform some specific mathematical, relational, bitwise conditional and logical computation or values of variables. ## Bitwise operators - **and (&)** - **or (|)** - **compliment (~)** - **xor (^)** - **left shift (<<)** - **right shift (>>)** - **Example:** - **And**: ```c 6 110 f 110 4 5 100 100 ``` - **Or**: ```c 6 110 1 5 101 111 ``` - **Complement:** ```c 6 110 010 ``` - **Xor:** ```c 9 1001 10 1010 0011 3 ``` - **Left shift:** ```c int x = 10; int y = 3; int z = x << y; ``` - **Right shift:** ```c int x = 10; int y = 3; int z = x >> y; ```

Use Quizgecko on...
Browser
Browser