0% found this document useful (0 votes)
36 views

Lesson 3: Selection: If, ?, Switch

This document discusses selection and switch statements in C programming. It includes: 1) An explanation of if-else selection statements using comparison operators like >, ? and : to execute one of two expressions depending on the result. 2) Examples of if statements to find the maximum of two numbers and take the absolute value. 3) An explanation of switch statements using a case to execute different code blocks depending on the value of an expression. 4) An example calculator program using switch to perform different arithmetic operations based on a character input. 5) Two problems - converting Roman numerals and solving a quadratic equation using switch/case.

Uploaded by

Jonny Cernei
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Lesson 3: Selection: If, ?, Switch

This document discusses selection and switch statements in C programming. It includes: 1) An explanation of if-else selection statements using comparison operators like >, ? and : to execute one of two expressions depending on the result. 2) Examples of if statements to find the maximum of two numbers and take the absolute value. 3) An explanation of switch statements using a case to execute different code blocks depending on the value of an expression. 4) An example calculator program using switch to perform different arithmetic operations based on a character input. 5) Two problems - converting Roman numerals and solving a quadratic equation using switch/case.

Uploaded by

Jonny Cernei
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Lesson 3

Selection: if , ?, switch
Linear
programs
Selection
If. Syntax
diagram
If – special
forms
Selection operators are ' ?’ and ' : ‘. Both used together.
Format: < expresie_1 > ? <expresie_2> :
<expresie_3>
expresie_1 is evaluated. If is no-zero, expresie_2 is executed.
Else (if is equal to 0) expresie_3 is executed.
Examples :
If operation

Expresie Valoare
x > y ? z=x : z=y z - maxim dintre x şi y
x >= 0 ? z=x : z=-x z - modulul lui x
AND (&&)
X Y X && Y
False False False
False True False
True False False NOT (!)
True True True X !X
Logical
OR (||) False True
operators X Y X || Y True False
False False False
False True False
True False False
True True True
Scrieți un program care va
#include <stdio.h>
determina dacă 3 numere
introduse pot forma lungimile
int main() laturilor unui triunghi oarecare
{
float a,b,c;

printf("Sides: ");
scanf("%f %f %f", &a, &b, &c);

if (a + b > c && a + c > b && b + c > a)


printf("Triunghi\n");
else printf("Nu este Triunghi\n");
return 0;
}
switch
switch (expresion)
{
case <expr. 1> : <commands block 1> break;
case <expr. 2> : < commands block 2> break;
switch …
case < expr. n> : < commands block n> break;
default: < commands block n+1 > break;
}
#include <stdio.h>
Example: int a;
calculator int main()
{
float a,b,c; char q;

printf("Operation:");
q = getchar();
printf("Operators: ");
scanf("%f %f", &a, &b);

switch(q)
{ case '+' : c=a+b; break;
case '-' : c=a-b; break;
case '*' : c=a*b; break;
case '/' : c=a/b; break;
}
printf("%f %c %f = %f\n", a,q,b,c);
return 0;
}
Problem 1:
Roman numbers of length two
Problem 2
A correct roman number, Calculate solutions of quadratic
Problems formed from two digits is equation using switch / case
given.
Write a program to find it’s
arabic equivalent.
Example: VI – 6, IX - 9
#include <stdio.h>
#include <math.h>
int main()
{
float a, b, c;
The solution float root1, root2, imaginary;
(problem 2) float discriminant;
printf("Enter values of a, b, c : ");
scanf("%f %f %f", &a, &b, &c);
/* Calculate discriminant */
discriminant = (b * b) - (4 * a * c);
discriminant = (b * b) - (4 * a * c);
switch(discriminant > 0)
{
case 1:
The solution root1 = (-b + sqrt(discriminant))
(problem 2) / (2 * a);
root2 = (-b - sqrt(discriminant))
/ (2 * a);
printf("Two real roots: %.2f and %.2f",
root1, root2);
break;
case 0:
switch(discriminant < 0)
{
case 1:
root1 = root2 = -b / (2 * a);
imaginary = sqrt(-discriminant) / (2 * a);
printf("Two distinct complex roots:
%.2f + i%.2f and %.2f - i%.2f",
root1, imaginary, root2, imaginary);
The solution break;
(problem 2) case 0:
root1 = root2 = -b / (2 * a);
printf("Two equal real roots :
%.2f and %.2f", root1, root2);
break;
}
}
return 0;
}

You might also like