Professional Documents
Culture Documents
Relational
Less than: Greater than: Less than or equal: Greater than or equal: < > <= >=
Equality
Equal: Not equal: == !=
! && ||
Logical
Negation: Logical and: Logical or:
==
&& || ?:
!=
Left to right
Left to right Left to right Right to left
+=
-=
,
*=
/=
etc.
Right to left
Left to right
!=
expr1!=expr2 TRUE or FALSE
Logical or:
||
EXAMPLE 4: int x=4, y=3; printf(%d %d, x&&y, x&&y&&y); //1 1 printf(%d %d, x||y, x||y||y); //1 1
CI R C U IT
expression1 || expression2
If expression1 is TRUE, No need to check the other, RESULT will be TRUE
The if statements
1
2
if(expression) statement; if(expression) statement_1; else statement_2; if(expression_1) else if (expression _2) else if (expression_3) . . else statement_n;
CS115 2010_2011 Senem KUMOVA METN
if without else
/* DOES NOT CARE ON THE ELSE CONDITION */
// MISSION : // CHECK IF USER ENTERS ZERO IF USER ENTERS ZERO EXIT
#include<stdio.h> #include<stdlib.h> // for exit() function main() { int x; printf(Type a number or Press 0 to exit ); scanf(%d, &x); if(x==0) { printf( exiting .....\n ); exit(0); } printf(%d,x); }
CS115 2010_2011 Senem KUMOVA METN
if without else
#include<stdio.h>
main() { int x; printf(Type a number); scanf(%d, &x); if(x==0) { printf(The neumber is zero);} printf(%d,x); }
if with else
//IF YOU HAVE 2 CASES (IF and ELSE ) // MISSION : CHECK IF A STUDENT FAILS OR PASSES main() { int x; printf(Enter your grade :\n);
scanf(%d, &x);
if(x>=60) { printf( Passed\n) ;} else { printf( Failed\n);} }
c=a+b; }
Nested if Statement
EXAMPLE 1: if(expression1) { if(expression2) { statement2 } statement1 } /* if expression1 is TRUE , statement1 will be evaluated whether expression 2 is TRUE or FALSE */
{ statement1 } { statement2 }
Example 1:
Find if x is a multiple of 2 and 5
#include <stdio.h> int main() { int a; printf("Input an integer and push return:\n"); scanf("%d", &a); if (a%2==0) { if(a%5==0) printf("%d is a multiple of 2 and 5\n", a); } else printf("%d is not a multiple of 2 and 5\n", a); return 0; }
Example 2:
Find if x is a multiple of 2 and 5
#include <stdio.h> int main() { int a; printf("Input an integer and push return:\n"); scanf("%d", &a);
if (a%2==0 && a%5==0) { /* Start of if block */ printf("%d is a multiple of 2 and 5\n", a); } else { /* This is the else branch */ printf("%d is not a multiple of both 2&5\n", a); } return 0;
}
CS115 2010_2011 Senem KUMOVA METN
switch and check if x equals to any case printf(x equals to A); break; printf(x equals to B); break; printf(x equals to C); break; prinf(x does not equal to A,B or C); }
#include <stdio.h> main() { int i; printf("Enter a positive integer : "); scanf("%d", &i); switch(i) { case 1: case 2: printf(A\n"); break; case 3: printf(B\n"); break; case 4: case 5: case 6: printf(C\n"); break; default: printf(D\n"); }} CS115 2010_2011 Senem KUMOVA METN
double x; while(1) // an infinite loop { scanf(%lf,&x); if(x<0.0) break; /* exit loop if x is negative */ printf(%f\n,fsqrt(x)); } // break jumps to here