Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. What is an If statement? Please put definition in own words. 2. What is the s

ID: 3666294 • Letter: 1

Question

1. What is an If statement? Please put definition in own words. 2. What is the syntax for an if statement? Hint it how the code will be written. 3. What is if/ else Statement? Please put definition in your own words. 4. What is the syntax for an if else statement? Hint it how the code will be written. 5. What is a nested if statement? Please put definition in your own words. 6. What is the syntax for a nested if statement? Hint it how the code will be written. 7. What is if/ else if statement? Please put definition in your own words. 8. What is the syntax for an if/ else if statement? Hint it how the code will be written. 9. What is a switch statement? Please put definition in your own words. 10. What is the syntax for switch statement?

Explanation / Answer

1.If statement

if statements will execute a set of instructions depend upon the boolean expression

Boolean expresion may be true, false, null , greater than, less than and equality

2. if Syntax
    if ( expression ) statement

Example
if ( i > 0 )
    y = x / i

3.If- Else Statement

if expression is true then statement is executed. If expression is false, then statement is ignored. then else part will get execute, the second statement is executed if expression is false

4. Syntax

if ( expression ) statement else statement

Example
if ( i > 0 )
    y = x / i;
else
{
    x = i;
    y = f( x );
}

5. Nested-if
Execute one if or else if statement inside another if or else if statement(s)

6.Syntax
if( boolean_expression 1) {

   /* Executes when the boolean expression 1 is true */
   if(boolean_expression 2) {
      /* Executes when the boolean expression 2 is true */
   }
}

Program
if( a == 100 ) {

      /* if condition is true then check the following */
      if( b == 200 ) {
         /* if condition is true then print the following */
         printf("Value of a is 100 and b is 200 " );
      }
   }

7.if/ else-if

it executes when we have more than two conditions

8. syntax of if/else if

if (condition) {
    code to be executed if this condition is true;
} elseif (condition) {
    code to be executed if this condition is true;
} else {
    code to be executed if all conditions are false;
}

Example

<?php
$t = date("H");

if ($t < "10") {
    echo "Have a good morning!";
} elseif ($t < "20") {
    echo "Have a good day!";
} else {
    echo "Have a good night!";
}
?>

9. Switch

A switch statement allows a variable to be tested which have list of values. Each value is called a case, and the variable being switched on is checked for each switch case

The expression is integral or enumerated type.

You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.

The constant-expression for a case must be the same data type as the variable in the switch, and it must be a constant or a literal.

When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.

When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.

Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.

A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case

10. Syntax

switch(expression) {

   case constant-expression :
      statement(s);
      break; /* optional */
  
   case constant-expression :
      statement(s);
      break; /* optional */

   /* you can have any number of case statements */
   default : /* Optional */
   statement(s);
}

Example
#include <stdio.h>

int main () {

   /* local variable definition */
   char grade = 'B';

   switch(grade) {
      case 'A' :
         printf("Excellent! " );
         break;
      case 'B' :
      case 'C' :
         printf("Well done " );
         break;
      case 'D' :
         printf("You passed " );
         break;
      case 'F' :
         printf("Better try again " );
         break;
      default :
         printf("Invalid grade " );
   }

   printf("Your grade is %c ", grade );

   return 0;
}

11.Scope

Scope is region of program where we can declare variables in the program

We can declare variables inside a function or block is called local variables

we can declare variables outside of the program that is global variables

12. cin.get()

user type in character using keyboard in command prompt whenever program getting input from user

cin.get(ch)
it reads the next character, even if it is a space, from the input and assigns it to the variable ch

skip character in the keyboard buffer

13.cin.ignore(n, c)

n is an integer and c is a character
it tell cin to skip n number of characters until the character c is encountered