Learning C/C++ Step-By-Step - Page 4

04. Step-by-Step C/C++ --- C Programming - Conditional Statements

 
  1. Introduction to Conditional Statements:
  2. if..else
  3. switch

 

1. Introduction to Conditional Statements:

A computer is an electronic device which can perform arithmetic operations as well logical decisions.

At this point, computer is far away from an ordinary calculator which able to perform only arithmetic operations.

We can ask the biggest value from the given two values using conditional statements like if-else, switch.

 

2. if..else

It is a conditional statement to find the variance between two expressions.

 
Syntax:

if ( <condition> )
     { <St.block>; }
else
     { <St block>; }

Every if has a condition and two statement blocks. If the condition is true it executes the first st.block and vice versa.

  Eg.

If( a>b )
     printf(“A is big”);
else
     printf(“B is big”);

Note: No need of block for Single statements.

1. Program to find the biggest of 2 values

/* 12_if.c */
#include <stdio.h>
int main()
{      /* Begin */
     int a, b;      /* Declaration of Variables */
     printf("\nEnter A value : "); scanf("%d", &a);      /* Read value A */
     printf("\nEnter B value : "); scanf("%d", &b);      /* Read value B */      
     if( a>b )      /* Compare both */
          printf("A is big");
     else
          printf("B is big");      /* Print the result */
      return 0;
}      /* End */

This is a list of operators in the C++ and C programming languages. All the operators listed exist in C++ 

Ref:  http://en.wikipedia.org/wiki/Operators_in_C_and_C++

 

Arithmetic Operators

Operator Purpose
+ Addition
- Subtraction
* Multiplication
/ Division
% Remainder after integer division (modulus)

 

Unary Operators

Operator Purpose
- Minus (negative number)
++ Increment (increase by 1)
-- Decrement (decrease by 1)
sizeof Size, in bytes
(type) Cast

 

Relational Operators

Operator Purpose
< Less Than
<= Less Than Or Equal To
> Greater Than
>= Greater Than Or Equal To

 

Equality Operators

Operator Purpose
== Equal To
!= Not Equal To

 

Logical Operators

Operator Purpose
&& AND
|| OR
! NOT

 

Bit-Manipulating Operators

Operator Purpose
& AND
| OR
~ NOT
^ XOR
<< Shift Left
>> Shift Right

 

Operator Precedence Groups

Operator Category Operators Associativity
unary operators
- ++ -- ! sizeof (type)
R to L
arithmetic multiply, divide and remainder
* / %
L to R
arithmetic add and subtract
+ -
L to R
relational operators
< <= > >=
L to R
equality operators
== !=
L to R
logical operators
&& ||
L to R
conditional operators
? :
R to L
assignment operators
= += -= *= /= %=
R to L

 

More Excercises

The reason behind more exercises is to get acquainted with the learned statements, if you are confident you don’t have to run the following programs.

/* 01. Program to find the age of a person from the following details */
/* age <= 12 Child Age
     age >= 13 and age <= 19 Teen Age
     age >= 20 and age <= 35 Young Age
     age >= 36 and age < 50 Middle Age
     age >= 50 Old Age
*/

/* 13_age.c */
#include <stdio.h>
int main()
{
     char name[20];
     int age;
     clrscr();
     print "Enter U'r name "; input name;
     print "Enter U'r age "; input age;
      printf("\n%s U are in ");
      if ( age <= 12 ) printf("Child Age");
      if ( age >= 13 and age <= 19 ) printf("Teen Age");
      if ( age >= 20 and age <= 35 ) printf("Young Age");
      if ( age >= 36 and age < 50 ) printf("Middle Age");
      if ( age >= 50 ) printf("Old Age");
      return 0;
}

/* 02. Program to find the biggest of 3 Values */

/* 14_big3.c */
#include <stdio.h>
#include <conio.h>
int main()
{
     int a, b, c;
     clrscr();
     printf("Enter A value "); scanf("%d", &a);
     printf("Enter B value "); scanf("%d", &b);
     printf("Enter C value "); scanf("%d", &c);
     if( a > b && a > c ) printf( "A is big " );
     if( b > a && b > c ) printf( "B is big " );
     if( c > a && c > b ) printf( "C is big " );
     return 0;
}

/* 03. Program to find the biggest of 3 Values using if..else */

/* 15_big3.c */
#include <stdio.h>
#include <conio.h>
int main()
{
     int a, b, c;
     clrscr();
     printf("Enter A value "); scanf("%d", &a);
     printf("Enter B value "); scanf("%d", &b);
     printf("Enter C value "); scanf("%d", &c);
     if( a > b && a > c )
          printf( "A is big " );
     else
          if ( b > c )
               printf( "B is big " );
          else
               printf( "C is big " );
     return 0;
}

/* 04. Program to find the biggest of 3 Values using nested if */

/* 16_big3.c */
#include <stdio.h>
#include <conio.h>
int main()
{
     int a, b, c;
     clrscr();
     printf("Enter A value "); scanf("%d", &a);
     printf("Enter B value "); scanf("%d", &b);
     printf("Enter C value "); scanf("%d", &c);
     if( a > b )
          if( a > c )
               printf(" A is big ");
          else
               printf(" C is big ");
     else
          if( b > c )
               printf(" B is big ");
          else
               printf(" C is big ");
     return 0;
}

/* 05. To find the week day of the given number */

/* 17_week.c */
#include <stdio.h>
int main()
{
     int week;
     printf("Enter week number "); scanf("%d", &week);
     if (week == 1 ) printf ("Sunday");
     if (week == 2 ) printf ("Monday");
     if (week == 3 ) printf ("Tuesday");
     if (week == 4 ) printf ("Wednesday");
     if (week == 5 ) printf ("Thursday");
     if (week == 6 ) printf ("Friday");
     if (week == 7 ) printf ("Saturday");
     if ( week < 1 || week > 7 ) printf("Bad Day");
     return 0;
}

 

3. Switch

A multi-conditional st. has the ability to check the variance of more than one expression.

Syntax:

 

switch(<id>)
{
     case <expr.> : <st. block>; break;
     case <expr.> : <st. block>; break;
     ......
     Default : <st. block>;
}

Eg.

 

switch(week)
{
     case 1 : printf( “Sun Day”); break;
     case 2 : printf(“Mon Day”); break;
     .
     .
     case 7: printf(“Satur Day”); break;
     default : printf(“Wrong Entry”);
}

/* 06. To find the week day of the given number using switch statement */

/* 18_switch.c */
#include <stdio.h>
int main()
{
     int week;
     printf("Enter week number "); scanf("%d", &week);
     switch(week)
     {
          case 1 : printf ("Sunday"); break;
          case 2 : printf ("Monday"); break;
          case 3 : printf ("Tuesday"); break;
          case 4 : printf ("Wednesday"); break;
          case 5 : printf ("Thursday"); break;
          case 6 : printf ("Friday"); break;
          case 7 : printf ("Saturday"); break;
          default : printf("Wrong Entry");
     }
     return 0;
}

Share this page:

1 Comment(s)